Is there a way in Home Assistant scripts to ensure that a specific command is executed even if the script is interrupted from outside?

Is there a way in Home Assistant scripts to ensure that a specific command is executed even if the script is interrupted from outside?

What I want to achieve: In a script, I want to turn on a switch and turn it off again after a specified delay. If the script is interrupted from outside (using script.turn_off), the switch should still be turned off.

Is such a configuration possible in a script?

Best regards,
Jürgen

No. If the script is interrupted then nothing further happens.

Tell us what problem you are trying to solve rather than your attempted solution.

https://xyproblem.info/

It must be ensured that if the switch is turned on by the script, it will definitely be turned off again. This is an electronically controlled water valve that should only remain open for a specific period of time.
Maybe there is another solution.

Don’t do both in the same script. Have an automation triggered by the switch being on for x minutes to turn the valve off.

In general, it’s not a good idea to have scripts or automations pause for more than a very short time - all sorts of things can go wrong. :smile:

3 Likes

No, at the moment I have a script where you pass a switch entity as a parameter and a time duration for how long it should be on. The script then includes a turn_on , a delay , and a turn_off . Right now, it is only being called from the dashboard, but later it might also be triggered by an automation. How is this usually done in Home Assistant? I’m still quite new to it.

Yes. You asked about a potential solution without asking if it’s the right way to do it. Tom’s point is absolutely accurate.

See Jack’s response. Because of how HA does stuff ALWAYS avoid long waits in automation or script. Think I triggers

When this happens… (your trigger) I want xxx (action)

In your case when the switch is turned on (no matter what did it) you want it off (action) in x minutes (also part of your trigger).

Thats a trigger (switch.foo : state = on for more than xxx seconds) and an automation that includes an action for Switch, turn_off (sorry I’m not in front of a machine for syntax)

This way it always works no matter how the switch turns on and avoids a wait.

2 Likes