pyscript icon indicating copy to clipboard operation
pyscript copied to clipboard

Problem with Trigger Closures when stopping and starting PyScript

Open marcelhoogantink opened this issue 1 month ago • 2 comments

I have the folowing (simplyfied) pyscript code:

def state_trigger_factory(sensor_name):

    @state_trigger(sensor_name)
    def func_trig(value=None, var_name=None):
        log.info(f"func_trig: {var_name} is changed: {value}")

    return func_trig

f2 = state_trigger_factory("sensor.time")

which is giving the nice output;

2025-11-24 19:04:00.008 INFO (MainThread) [custom_components.pyscript.file.test_closure.func_trig] func_trig: sensor.time is changed: 19:04
2025-11-24 19:05:00.003 INFO (MainThread) [custom_components.pyscript.file.test_closure.func_trig] func_trig: sensor.time is changed: 19:05
2025-11-24 19:06:00.007 INFO (MainThread) [custom_components.pyscript.file.test_closure.func_trig] func_trig: sensor.time is changed: 19:06
2025-11-24 19:07:00.006 INFO (MainThread) [custom_components.pyscript.file.test_closure.func_trig] func_trig: sensor.time is changed: 19:07
2025-11-24 19:08:00.008 INFO (MainThread) [custom_components.pyscript.file.test_closure.func_trig] func_trig: sensor.time is changed: 19:08
2025-11-24 19:09:00.006 INFO (MainThread) [custom_components.pyscript.file.test_closure.func_trig] func_trig: sensor.time is changed: 19:09

But when pyscript is stopped and started with:

Image

and

Image

... the closure does not give any logs anymore .... ( no triggers)

After a HA restart it is working again:

2025-11-24 19:20:00.004 INFO (MainThread) [custom_components.pyscript.file.test_closure.func_trig] func_trig: sensor.time is changed: 19:20
2025-11-24 19:21:00.007 INFO (MainThread) [custom_components.pyscript.file.test_closure.func_trig] func_trig: sensor.time is changed: 19:21

In a bigger project I have some issues with this behavior.

marcelhoogantink avatar Nov 24 '25 18:11 marcelhoogantink

your answer seems to be here: https://hacs-pyscript.readthedocs.io/en/stable/reference.html#trigger-closures

Image

ALERTua avatar Nov 24 '25 20:11 ALERTua

It looks like the answer is here: https://github.com/custom-components/pyscript/issues/750

marcelhoogantink avatar Nov 24 '25 20:11 marcelhoogantink