Toggle (or turn off) a running script?

I have a script that runs a handful of fans and an AC on boost mode for 20 minutes, to rapidly decrease temperature. I have a Lovelace button that calls this script.

What I’d like is an ability to a) cancel running the script and b) return to the device states before running the script. B) is optional.

How can i do this? I was looking into script.toggle and script.turn_off but I’m not sure what these even do, they do not seem to do what I want. Thanks!

Runs the script if not running. Stops the script if running.

Cancels a running script.

Yes but what do these actually mean? They do not effect the running devices (the actual fans and AC at all). I understand those cancel the script so steps in the future do not trigger but how do I actually stop what’s happening and return to the pre-run state?

Exactly what I said. They stop or start a running script.

So you do understand. Why ask ask that first question?

By using the script.turn_off service.

That would require you storing the pre-run state. See creating scenes on the fly.

Thanks, so the Lovelace “toggle script” button would actually call scene.turn_on and script.turn_off services?

There is a caveat to stopping a running script. You can only stop or cancel a script which has a delay or wait in it. If your script is just a bunch of service calls the script will just execute and won’t give you a chance to cancel it.

Your pseudo code would be something like this:

- script:
  - name: cooling boost
  - scene.create
    - entity_id: climate.hvac
    - name: normal_cooling
  - climate.set_target
    - temperature: 
    - mode:
  - delay:
    - minutes: 20

- script:
  - name: resume schedule
  - script.turn_off
    - entity_id: script.cooling_boost
  - scene.restore
    - name: normal_cooling
2 Likes

Thanks, I’ll do it like this.

Hi folks, this is a really interesting moment with HA. Sure, as @tom_l says, script off makes sense and as @squirtbrnr says, if you trigger a bunch of services or turn on button helpers, these are different entities living their triggered state. What a blow on a head when designing a logic in your HA. So, the solution is to stop the script from executing next steps in the sequence AND (in my situation) if you do not want to have your house flooded, you must immediately shut down the valves that were triggered. Understandably…

Lovely! It’s better if the logic is so straightforward and do not presume our expectations…

1 Like

I know this is an old thread, but I could use some help and this is my exact problem. My experience has been that script.turn_off just doesn’t work.

I have this wonderful little script that sweeps through my backyard lights changing the color each time. It’s cool. But if I set it to run like 200 times, I can’t stop it. I made a separate script that calls script.turn_off action against the script that I’m trying to stop, and it doesn’t seem to do anything.

I don’t believe you can stop a for loop. A for loop is treated as an action in the sequence. You can only stop a sequence during a delay. It would be helpful if you posted your code in your own thread for better visibility since it’s specific to your issue.

Of course, forgive me.

alias: Rotating Rainbow Backlight Sequence
mode: single
sequence:
  - repeat:
      count: 100
      sequence:
        - target:
            entity_id: |
              {% set lights = [
                'light.spot_1',
                'light.spot_2',
                'light.spot_3',
                'light.spot_4',
                'light.spot_5'
              ] %} {{ lights[repeat.index % lights|length] }}
          data:
            transition: 0.1
            hs_color:
              - "{{ (repeat.index * (360 // 20)) % 360 }}"
              - 100
          action: light.turn_on
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 5
        - target:
            entity_id: |
              {% set lights = [
                'light.spot_1',
                'light.spot_2',
                'light.spot_3',
                'light.spot_4',
                'light.spot_5'
              ] %} {{ lights[repeat.index % lights|length] }}
          action: light.turn_off
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 5
description: ""

I am a little new to HA’s script capability. After I posted this, I looked a little closer and realized I was not actually targeting the right script in the first place. I updated it and it works fine. The script does have the required delay commands, So… PEBKAC