I’ve been diving into the world of smart irrigation systems, and I must say, it’s been quite an adventure!
I recently set up an irrigation cycle script in Home Assistant, and while the basics were straightforward, I ran into a bit of a puzzle when trying to implement a conditional delay. Let me walk you through my journey and how I cracked the code!
So, here’s the deal: I wanted my irrigation system to delay watering based on whether a specific zone was included or not. If zone 1 was active, the delay should match the duration set in an input_number entity. If not, it should default to a 15-second delay. Sounds simple enough, right? Well, my initial attempts didn’t quite hit the mark, and I ended up doing a lot of head-scratching. ![]()
After some digging on the forum, I came across mentions that conditional delays might require comparing trigger times and desired delays. It felt a bit cumbersome, but I was determined to make it work. I started experimenting with service_template and if statements, tweaking the logic to ensure it would handle both scenarios seamlessly.
Here’s a snippet of my final script setup:
yaml
irrigation_cycle:
alias: “Irrigation cycle”
sequence:
# Open valve
- service: homeassistant.turn_on
entity_id: switch.test_sonoff_basic
# If included in cycle, delay for 'zone 1 duration'
# else delay for 15 seconds
- service_template: >
{% if is_state('input_boolean.water_zone1', 'on') %}
delay: '00:{{ state('input_number.zone1_duration') }}:00'
{% else %}
delay: '00:00:15'
{% endif %}
# Close valve
- service: homeassistant.turn_off
entity_id: switch.test_sonoff_basic
# Final 15-second delay
- delay: '00:00:15'
It wasn’t without its hiccups, but after several iterations, the script started behaving exactly as I envisioned!
The conditional delay now switches seamlessly between the zone-specific duration and the default 15 seconds, ensuring my plants get just the right amount of water without any unnecessary delays.
Reflecting on this experience, I’m reminded of how powerful Home Assistant can be with a bit of scripting and creativity. It also highlighted the importance of community resources—without those forum threads, I might still be stuck in a loop! ![]()
If anyone has tips or alternative approaches to handle conditional delays, I’d love to hear them! Let’s continue to grow and learn together in the smart home journey! ![]()