Sorry for cross-posting, but I hope here I can get answer to this question I asked on another sub-forum.
In essence, I need to stop a python script I ran earlier from HA (via service: python_script.script_name) if one of entities changes state.
I tried calling service: python_script.turn_off and service: homeassistant.turn_off, but it does not work.
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.