I recently discovered template switches and am trying to simplify my messy automations to instead use these.
I am hitting a problem with one switch, which when turned on/off and then back on reports that the script associated to its turn_on is already running. I guess this is because I have put in a delay in the turn_on function, to allow the switch to turn itself off automatically after some time. The error I get in the log is
Logger: homeassistant.helpers.script.shower_cozifier
Source: helpers/script.py:1365
First occurred: 17:03:37 (1 occurrences)
Last logged: 17:03:37
Shower Cozifier: Already running
Does switch.turn_off kill the running turn_on script automatically? Or do I need to kill it by hand somehow e.g.by putting in some command to kill it in the turn_off script?? Or am I doing something quite unforgivable and this auto-off should be done differently. I used to use an external automation, but I have ended up with way too many such auxilliary scripts and am trying to clean up.
I could in principle move the delay to the end of the script script.cozify_on – I don’t always want to have it, but I could condition on something I guess. Would it be enough then to script.turn_off to interrupt the delay?
The scripts the switch calls do some climate shenanigans such as increasing target temperature or turning on electric heaters, but I do not think that is the issue (and the off part seems to execute fine). It really is the fact that I cannot call the turn_on script again.
Change the way you call the script and see if that helps. Be advised it may cause other issues (script.cozy_on may still be running when you call script.cozy_off).
The cozify_on script complete in around 10 seconds and I make sure to wait longer than that before turning the swithc to on again. I think it’s this delay inside the turn_on part and I would have to turn_off the switch’s internal turn_on script – but I have no idea how to address that – it’s not one of my entities.
So I’ve created a minimal example that behaves in the same manner. All it does it flip an input_boolean toggle and has a delay before toggling it back.
Logger: homeassistant.helpers.script.testin_testin_123
Source: helpers/script.py:1365
First occurred: 10:00:27 (1 occurrences)
Last logged: 10:00:27
Testin testin 123: Already running
Should this be considered a bug or it works as designed?
I’ve solved it for myself by moving the delay into an automation, where I wait for a delayed state trigger before flipping the switch off, but it would be nice to have this auto-turn-off feature in the switch script.
You can see the minimal example that it’s not the script that’s the issue but really the turn_on function of the switch. Now if there were a way to add an execution mode to that, then hat should help, but then you might end up with some turn_on script running with the switch seemingly already turned off, so maybe this is by design to aim predictability.
Your minimal example doesn’t transfer control of the turn_on function to a separate script whose execution mode is restart. Unless you test that arrangement, it may be premature to conclude the Template Switch’s turn_on service blocks all subsequent calls.
The minimal example doesn’t have the script and does block itself from running because of the delay command. Why would adding something on top (handing over the execution to a script) help here?
Now it may well be that transferring the delay command to the script and running in a different execution mode will allow me to run concurrent scripts, but I don’t actually want to do that since I call this script in other instances where the delay is not wanted.
Or am I not really understanding something at all here?
When I turn on switch.test_switch, one minute later it turns itself off.
If I turn it on then promptly turn it off and back on, it turns on without generating a warning message in Logs.
It’s the inclusion of mode:restart that permits the script to be restarted, without complaint, when it’s called while still running. Plus the use of service: script.turn_on instead of calling the script directly.