Replies: 2 comments
-
Hey @jlkermit 👋 You have to preserve the reference to the inner function. Otherwise pyscript simply "garbage collects" it upon exit. reference = None
def settrigger():
log.info(f"*Python Test Script* Setting motion detection trigger")
@state_trigger("input_boolean.test_motion")
def motion_detected_trigger():
task.unique("input_boolean.test_motion")
log.info(f"*Python Test Script* Motion detected trigger fired")
# save reference so pyscript can call it when state changes
reference = motion_detected_trigger
settrigger() This is documented in the Trigger Closures section of the docs. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thank you |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to get a function within a function that has a state trigger decorator to fire with no luck. I have created a test script with no other code in it or imports. I have done two tests. The first with the state trigger decorator function on its own and the second with the state trigger decorator function within a function. I'm using Pyscript version 1.6.1 in HAOS 14.2 on a RPi 5.
Here is the script and log results.
The following state trigger decorator function fires as expected (HA was restarted prior to testing this)
Script:
Log entries:
2025-01-30 09:26:27.144 DEBUG (MainThread) [custom_components.pyscript.trigger] trigger file.test.motion_detected_trigger is active
2025-01-30 09:26:27.152 DEBUG (MainThread) [custom_components.pyscript.trigger] trigger file.test.motion_detected_trigger: watching vars {'input_boolean.test_motion'}
2025-01-30 09:26:27.152 DEBUG (MainThread) [custom_components.pyscript.trigger] trigger file.test.motion_detected_trigger waiting for state change or event
2025-01-30 09:32:26.458 DEBUG (MainThread) [custom_components.pyscript.eval] file.test.motion_detected_trigger: calling motion_detected_trigger(, {'trigger_type': 'state', 'var_name': 'input_boolean.test_motion', 'value': 'off', 'old_value': 'on', 'context': <homeassistant.core.Context object at 0x7f98dd9250>})
2025-01-30 09:32:26.459 DEBUG (MainThread) [custom_components.pyscript.eval] file.test.motion_detected_trigger: calling unique("input_boolean.test_motion", {})
2025-01-30 09:32:26.459 DEBUG (MainThread) [custom_components.pyscript.eval] file.test.motion_detected_trigger: calling info("Python Test Script Motion detected trigger fired", {})
2025-01-30 09:32:26.459 INFO (MainThread) [custom_components.pyscript.file.test.motion_detected_trigger] Python Test Script Motion detected trigger fired
The following state trigger decorator function within a function does not fire and the log DOES NOT have entries for the state trigger function being 'active' or 'watching vars'. (HA was restarted prior to testing this)
Script:
Log entries:
2025-01-30 09:44:18.022 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling settrigger(, {})
2025-01-30 09:44:18.022 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling info("Python Test Script Setting motion detection trigger", {})
2025-01-30 09:44:18.022 INFO (MainThread) [custom_components.pyscript.file.test] Python Test Script Setting motion detection trigger
Beta Was this translation helpful? Give feedback.
All reactions