pyscript icon indicating copy to clipboard operation
pyscript copied to clipboard

(How) can I trigger a HA automation on pyscript reload?

Open bjornbak opened this issue 2 years ago • 3 comments

When a pyscript is updated the reload is logged as

2023-10-07 12:20:33.937 INFO (MainThread) [custom_components.pyscript.global_ctx] Reloaded /config/pyscript/sonnen_time_of_use_plan.py

in the log.

I would like to trigger a HA automation when I update a pyscript. How can I listen to/trigger based on the reloaded event?

bjornbak avatar Oct 07 '23 10:10 bjornbak

https://hacs-pyscript.readthedocs.io/en/stable/reference.html#time-trigger image I hope this helps. But please use the Discussions tab instead of Issues for questions <3

ALERTua avatar Oct 07 '23 10:10 ALERTua

Thank you, @ALERTua but I need the trigger a HA built-in automation on reload.

I want to create python scripts with no or minimum HA or pyscript specific code so I can unit test them outside HA. So I am creating HA automations that collects states and attributes and call the pyscript as a service. The pysript is returning it's output using the new response_variable feature. And the HA automation is then updating relevant entities..

As a workaround I use the folder_watcher sensor but it triggers too fast so I have to add an artificial delay to wait for the reload.

bjornbak avatar Oct 07 '23 11:10 bjornbak

Per @ALERTua 's suggestion - can't the pyscript call-on-reload function do something than can then trigger an HA built-in automation? Eg, it could set an entity or call a service?

craigbarratt avatar Oct 07 '23 15:10 craigbarratt