Is it possible to cancel/stop a running Automation?

I’m working on a Sprinkler controller and run an on_time automation from the Time component.

The on_time section checks every minute if a scheduled time matches the current time, and if so I enable a relay and delay for a specified amount of time and disable the relay. I continue with the rest of the relays.

I was wondering is it possible to stop execution remotely (without a physical button/switch) of the on_time code? I want to stop the execution in case of an emergency or just switching from a scheduled watering to manual if I have to check a specific relay.

Here is a snippet of code:

# Time based automations.
time:
  # Program 1 Schedule
  - platform: homeassistant
    id: homeassistant_time
    on_time:
      - seconds: 0
        minutes: /1
        then:
          # Check if Program 1 Start Time is equal to the current time
          - if:
              condition:
                lambda: 'return scheduled_runtime(id(ha_program1_start_time).state.c_str());'
              then:
                # Check if Program 1 is enabled
                - if:
                    condition:
                      lambda: 'return id(program1_state) == 1;'
                    then:
                      # Check if Program 1 Run Day is enabled
                      - if:
                          condition:
                            lambda: 'return id(program1_run_days)[id(homeassistant_time).now().day_of_week - 1] == 1;'
                          then:
                            # Change Program 1 Status from 'Not Running' to 'Running'
                            - homeassistant.service:
                                service: input_boolean.turn_on
                                data:
                                  entity_id: input_boolean.sprinkler_program1_status
                            # Check if Station 1 duration is greater than 0
                            - if:
                                condition:
                                  lambda: 'return atoi(id(ha_sprinkler_program1_station1_run_time).state.c_str()) > 0;'
                                then:
                                  - switch.turn_on: station1
                                  - delay: !lambda 'return atoi(id(ha_sprinkler_program1_station1_run_time).state.c_str()) * 1000 * 60;'
                                  - switch.turn_off: station1

The only thing I can think of is to use the Restart Switch as the sprinkler will reboot with all the relays will be off but with a side effect of a 10 second or so delay since the esp device will lose network connectivity.

switch:
  - platform: restart
    name: "Sprinkler Controller Stop"

You can disable and enable automation using:

  - service: automation.turn_off
    data:
      entity_id: automation.your_automation_entity_id

This will stop it in its tracks and cancel any associated timers and not execute any further actions. You can turn it on using automation.turn_on.

Is that what you were asking or am I off base?

2 Likes

@CO_4X4 I’ll look into that today. Thank you! I saw script on/off I must have missed the automation on/off.

I think @CO_4X4 is thinking of stopping automations in HA, not ESPhome. I don’t think ESPhome has an automation stop command, only stop script as you mentioned. If you put your automation into an ESPhome script and have the automation call the script it will give you a way to then stop it using the stop script command.

1 Like

@sparkydave Oh ok I see. That should be doable. Thanks!