I’m not sure that’s possible, and it woudn’t be very reliable (not sure where the script would stop).
What is you script doing?
Maybe change it so that home-assistant run it every x minutes/seconds via an automation, and when you don’t need the script to run, disable the automation on state change.
Here is the code that runs in a while loop:
hass.services.call(‘homeassistant’, ‘turn_on’, {‘entity_id’: entity_id})
time.sleep(on_time)
hass.services.call(‘homeassistant’, ‘turn_off’, {‘entity_id’: entity_id})
If I knew how to check state of an entity from the python script, I would be able to end it from within…
Well, I don’t know how to do the following in hass (did not see any loop statements):
If there is time T and and an entity that cannot be ON for longer than L and requires D seconds to rest.
Can you show me how to do that?
Well, Python is not my language and I’m still learning, but being able to write a proper function is easier for me than trying to create something out of a limited set of in-built commands.
Yaml is good for its goals too (most of the time) if one uses a proper editor, otherwise it’s a nightmare… :
Anyway, thanks for the tip
Now I have a reason to learn one more concept.
FWIW you can get the state of an entity in a Python script with hass.states.get(entity_id).state. Also I think you loop in a YAML script by having it call itself. But I’m new, so…
sorry, it means “T is the total amount of time I need the entity to be turned on”
I need it during execution as an indicator that it’s time to stop the script.
Already did that by passing appropriate entity_id and desired state to the script.
Thanks for the example.
Will have a thought about it as the concept is slightly different from straightforward scripting
LOL, that post starts by quoting the same example from the docs. Wow, what does it take to get that fixed? How long has it been leading people astray I wonder.
The “Examples for flashing lights” docs page that I (and others) referenced shows a script turning itself on at the end in order to implement a looping technique, but that doesn’t work (or at least, doesn’t work anymore since that was written.) @PhyberApex was nice enough to submit a fix to that doc page.