pyscript icon indicating copy to clipboard operation
pyscript copied to clipboard

Using generated stubs inside @state_trigger can cause an exception after restarting home assistant

Open ryan-fessler opened this issue 3 weeks ago • 4 comments

After restarting home assistant, I'm getting the following exception when it tries to load one of my scripts that use the generated stubs:

Exception in </config/pyscript/scripts/home/smoke_alarm_low_battery_notifications.py> line 25:
    @state_trigger(get_str_expr(sensor.basement_staircase_smoke_co_alarm_battery_level))
                                ^
NameError: name 'sensor.basement_staircase_smoke_co_alarm_battery_level' is not defined

This is the first script that pyscript attempts to load. My other scripts do not seem to have this same problem. If I save the file again in my IDE without making any changes, it reloads successfully without any errors. Is this a timing issue of some sort?

Update:

I no longer get that exception if I change my @state_trigger to:

@state_trigger('float(sensor.basement_closet_smoke_co_alarm_battery_level) <= 80.0')

And I don't get the NameError when I use sensor.basement_closet_smoke_co_alarm_battery_level in the function.

It seems there's just an issue with it being used inside @state_trigger, and only after restarting home assistant, not after forcing a reload by saving the file.

ryan-fessler avatar Dec 10 '25 20:12 ryan-fessler

could you try https://github.com/custom-components/pyscript/pull/785 ?

ALERTua avatar Dec 11 '25 08:12 ALERTua

@ALERTua the file path indicates the bug is unrelated

dmamelin avatar Dec 11 '25 13:12 dmamelin

Stubs don’t affect Pyscript runtime at all. They are designed only for IDE development and exist only in the IDE.

At HA startup, sensor.basement_staircase_smoke_co_alarm_battery_level may not yet be available, so:

  • @state_trigger(get_str_expr(sensor.basement_staircase_smoke_co_alarm_battery_level)) tries to read the sensor value immediately, and if the sensor isn’t available yet, it raises an error.
  • @state_trigger("sensor.basement_closet_smoke_co_alarm_battery_level") does not try to read the sensor at startup. It simply waits for state-change events, including the sensor’s first appearance after HA starts.

PS: this is better @state_trigger("sensor.basement_closet_smoke_co_alarm_battery_level.as_float(default=100) <= 80.0")

dmamelin avatar Dec 11 '25 13:12 dmamelin

sorry, didn't notice the sensor entity id is not a string.

ALERTua avatar Dec 11 '25 14:12 ALERTua

@dmamelin Ah, thank you for the clarification! That makes sense. Is there a way to have my script wait for entities to be available?

ryan-fessler avatar Dec 11 '25 16:12 ryan-fessler

Is there a way to have my script wait for entities to be available?

@state_trigger("sensor.basement_closet_smoke_co_alarm_battery_level.as_float(default=100) <= 80.0") is safe. If the sensor is unavailable, .as_float(default=100) returns 100, so the condition won’t trigger. If the sensor is available and its value can be converted to float it will return that value, and if it’s below 80, the trigger will fire.

If you want to trigger on the sensor appearing, then @state_trigger("sensor.basement_closet_smoke_co_alarm_battery_level.has_value()") should work.

However, if Pyscript starts after the sensor appears, the trigger will not fire. You can use the state_check_now flag to handle that case.

dmamelin avatar Dec 11 '25 17:12 dmamelin