Scripts: Dealing with early stop / handle early exit

If a script is manually stopped via the GUI, is there a way to run a line of script or fire an event to handle this early exit?

This would be used in the situation where exiting early may leave parts of HASS setup in states you don’t wish to have.

E.G.

Script:

switch.turn_on.....
delay...
switch.turn_off....

In the above example, exiting/stopping the script before it’s finished will leave the switch turned on.

Any ideas?

did you find a solution for this?

I just tried manually turning on a script (that takes a while) and then manually turning it off before it was finished. I noticed this event in the log (that doesn’t happen when the script finishes by itself):

2018-05-28 08:02:16 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=script, service=turn_off, service_call_id=1975739248-684, service_data=entity_id=['script.family_room_lamp_flash']>

Since this is an event on the event bus you could have a trigger in an automation that triggers on that event (and specifies the appropriate data that limits it to just turning off the script you care about.)

1 Like