Stop an automation mid-run via an input boolean?

My sprinkler system is integrated in HA. In order to run multiple sprinkler zones in succession, I use an input boolean which activates an automation. See below.

However, I want to configure it in a way where I can stop the entire automation mid-run if the input boolean is turned off. Is there a simple way to do that? Would I need a seperate automation? Any suggestions would be great.

alias: "Sprinklers: Frontyard (Right - Front)"
description: Runs Sprinkler Zones 1, 2 for 10 minutes
mode: single
triggers:
  - entity_id:
      - input_boolean.sprinklers_frontyard_right_front
    to: "on"
    trigger: state
conditions: []
actions:
  - action: switch.turn_on
    target:
      entity_id: switch.rain_bird_sprinkler_1
    data: {}
  - delay: "00:10:00"
  - action: switch.turn_off
    target:
      entity_id: switch.rain_bird_sprinkler_1
    data: {}
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - action: switch.turn_on
    target:
      entity_id: switch.rain_bird_sprinkler_2
    data: {}
  - delay: "00:10:00"
  - action: switch.turn_off
    data: {}
    target:
      entity_id: switch.rain_bird_sprinkler_2
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - action: input_boolean.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.sprinklers_frontyard_right_front

You could add a second trigger when the input Boolean is turned off and change the automation mode to restart. Use trigger ids and a choose block to run the actions. The off trigger block would turn off the sprinklers.

1 Like

So something like this?

alias: "Sprinklers: Frontyard (Right - Front)"
description: Runs Sprinkler Zones 1, 2 for 10 minutes unless cancelled
mode: restart
trigger:
  - id: start
    platform: state
    entity_id: input_boolean.sprinklers_frontyard_right_front
    to: "on"
  - id: stop
    platform: state
    entity_id: input_boolean.sprinklers_frontyard_right_front
    to: "off"

condition: []

action:
  - choose:
      - conditions:
          - condition: trigger
            id: start
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.rain_bird_sprinkler_1
          - delay: "00:10:00"
          - service: switch.turn_off
            target:
              entity_id: switch.rain_bird_sprinkler_1
          - delay: "00:00:30"
          - service: switch.turn_on
            target:
              entity_id: switch.rain_bird_sprinkler_2
          - delay: "00:10:00"
          - service: switch.turn_off
            target:
              entity_id: switch.rain_bird_sprinkler_2
          - delay: "00:00:30"
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.sprinklers_frontyard_right_front
      - conditions:
          - condition: trigger
            id: stop
        sequence:
          - service: switch.turn_off
            target:
              entity_id:
                - switch.rain_bird_sprinkler_1
                - switch.rain_bird_sprinkler_2

Yes. Looks good

Have you considered what may happen if Home Assistant is restarted during the first or second 10-minute delay?

The reason why I ask is because then Home Assistant is restarted, all active automations are immediately terminated. In other words, they do not run to completion but are stopped at whatever action they are currently executing (in the middle of a 10-minute delay) and do not execute their remaining actions (like turning off the sprinkler).

You may wish to test this potential failure scenario by restarting Home Assistant while the automation is executing its 10-minute delay.

I use the weekly schedule helper to time my irrigation. If I want to stop a run it is simply a matter of turning off the valve switch.

- id: 9249ec91-acda-4885-9d11-ecb51bf51416
  alias: Irrigation Zone 1 # Ferns
  triggers:
  - trigger: state
    entity_id: schedule.fern_tricklers_schedule
    from: 'off'
    to: 'on'
  conditions:
  - condition: state
    entity_id: binary_sensor.fern_moisture_limit
    state: 'off'
  actions:
  - action: switch.turn_on
    entity_id: switch.zone_1
  - action: notify.telegram_system_log
    data:
      title: '💧 <b>Z1 Irrigation on</b>'
      message: "Fern irrigation turned on."

- id: 4e688d37-95c5-4724-b950-bb0b40389c99
  alias: Irrigation Zone 1 Off # Ferns
  triggers:
  - trigger: state
    entity_id: schedule.fern_tricklers_schedule
    from: 'on'
    to: 'off'
  conditions:
  - condition: state
    entity_id: switch.zone_1
    state: 'on'
  actions:
  - action: switch.turn_off
    entity_id: switch.zone_1
  - action: notify.telegram_system_log
    data:
      title: '☑️ <b>Z1 Irrigation off</b>'
      message: "Fern irrigation turned off."

I also have a couple of failsafes:

- id: abb3d2f7-e567-44c3-930f-7d438910b62d
  alias: Irrigation Time Overrun
  triggers:
  - trigger: state
    entity_id: switch.zone_1
    to: 'on'
    for:
      minutes: "{{ states('input_number.max_run_time')|int }}"
    variables:
        name: Fern
  - trigger: state
    entity_id: switch.zone_2
    to: 'on'
    for:
      minutes: "{{ states('input_number.max_run_time')|int }}"
    variables:
      name: Rear garden
  - trigger: state
    entity_id: switch.zone_3
    to: 'on'
    for:
      minutes: "{{ states('input_number.max_run_time')|int }}"
    variables:
      name: Front garden
  actions:
  - action: notify.telegram_system_log
    data:
      title: '⚠️ <b>Irrigation Max Runtime</b>'
      message: "Turning off {{name}} irrigation due to max runtime ({{ states('input_number.max_run_time') }} min). Please check valve, controller, and schedule if this was not a manual activation."
  - action: switch.turn_off
    target:
      entity_id: "switch.zone_{{ trigger.id|int + 1}}"

- id: 575d6820-6e39-41bb-8186-db4215031fe2
  alias: 'Irrigation Connection Monitor'
  triggers:
  - trigger: state
    entity_id: binary_sensor.irrigation_status
    to: 'off'
    for:
      minutes: 5
  actions:
  - action: notify.telegram_alert
    data:
      title: '⚠️ <b>Irrigation lost comms</b>'
      message: "Communication lost to irrigation controller. Please check valves are not on."