How can I cancel scripts with multiple delay actions?

Hello all,

It seems that you can not turn off a script that has delayed actions.

I’ve read all related topics on this but I have not found an answer yet. I have the following script to run a script that turns on a switch for 15 minutes, turns off it for 15 minutes, and repeats for 8 hours.

I ran the script multiple times at first to test and it was working. But then I got hit by all these delay actions in the same scripts triggered previously, which I believe multiple instances are running based on the switch behavior. I have nowhere to cancel the script. I ran a script to script.turn_off this script, which didn’t help.

A script once started doesn’t supply a script instance ID, which would’ve been helpful for cancellation later.

I could also improve the script design. All I need is this scripted action that can be turned on and canceled anytime. I’d like to run this solution by all of you experts. Would it be better to use a slider helper for the remaining time, which serves as a centralized state for the control, and have an automation run based on the slider? The automation run should decrease the remaining time represented by the slider which I can terminate early. How should I design the automation in this case? Thank you all.

repeat:
  count: 32
  sequence:
    - type: turn_on
      device_id: device_id
      entity_id: entity_id
      domain: switch
    - delay:
        hours: 0
        minutes: 15
        seconds: 0
        milliseconds: 0
    - type: turn_off
      device_id: device_id
      entity_id: entity_id
      domain: switch
    - delay:
        hours: 0
        minutes: 15
        seconds: 0
        milliseconds: 0

Hi Fjin,

Add a cancel in the repeat.

Have it check an helper boolean that you can set from developer or from a dashboard that will stop the repeat. You can probably just set the repeat number to 33 or stop it some other way.

Thanks. I got everything to work. I’m using a slider that controls the length of automation. It also serves as an on/off switch based on the value.

1 Like