How can I set callback function for the cancel event of a script?

When a script is cancelled, is there a way I can set some action for it?

I’m unaware of a service call to cancel a running script so under what circumstances are your scripts being cancelled?

Manually, and I need to sort things properly when I decided to cancel the script (for example, I started a timer in the script, and need to cancel the timer).

Currently I am using an automation (triggered by script state change) to do the work, but I don’t know what to put int the [from] domain and the [to] domain, it will be triggered whether I turn on or turn off the script if I left them empty. I have tried [Turned on/ Turned off] [turned on/ turned off] [on/ off] [On/ Off] and none of them work…

It appears I may have misunderstood your request.

When you run a script, its state value changes from off to on. When the script finishes executing its actions, its state value changes from on to off.

If you want to detect when a script has finished executing, you can use a State Trigger to detect the transition from on to off.

alias: example
  - platform: state
    entity_id: script.your_script
    from: 'on'
    to: 'off'
condition: []
  #... your actions go here ...

Oh it works, thank you. I have been mislead by the visualizer editor to input the attribute domain as ‘current’… Didn’t know this can be ignored.

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title signaling to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.