I’d like a way to break a script out of a Repeat loop if the script is called again.
It’s not Restart
Sure, you can use Restart, but that’s global. I want to use Parallel with Restart based on the passed-in fields. If the fields are the same as before, stop the script. I’d need some way to group the criteria that’d cause a restart.
An Example Script
I have a script that looks like this:
I need it to run in Parallel because there are different sets of lights that might turn on and need to respond to lighting changes. At the same time, I’ll run into memory leaks if I never turn off the lights.
If, for instance, I press a Zigbee button 30 times to “turn on lights”, it will activate this script 30 times, flooding my Parallel queue until I turn off the lights.
Possible Solution
I was thinking of listening to call_service
making sure to check for my script name and set of lights, but I’m not sure how to do that.
I can do something like this:
trigger:
- platform: event
event_type: call_service
event_data:
domain: script
service: adaptive_lighting
But I’m not even sure if this will work, nor how to narrow it down to only these lights.
In the Wait For, I can wait for this event to trigger regardless of which lights. That’ll trigger the Repeat Until condition, and I can add something there for:
{% set called_lights = trigger.event.data.service_data.lights | sort %}
{% set expected_lights = lights | sort %}
{{ called_lights == expected_lights }}
This template will error unless a trigger occurs, so I might need to say something like if trigger && trigger.event && trigger.event.data
etc unless there’s an easier way to do that.
Horrible Solution
Another idea is to enable a toggle before calling a script that, if true, stops other running scripts. Then I call the script.
Since there’s no bulk-adding of Helpers in Home Assistant, and since it’s a maintenance nightmare, I’d like to avoid this method. I have hundreds of lights and 20 lighting rooms.