Skip to content

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

Open

Description

@ryan-fessler

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.

Activity

  1. changed the title [-]Using generated stubs can cause an exception after restarting home assistant[/-] [+]Using generated stubs inside @state_trigger can cause an exception after restarting home assistant[/+] on Dec 10, 2025
  2. ALERTua commented on Dec 11, 2025

    @ALERTua
    Contributor

    could you try #785 ?

  3. dmamelin commented on Dec 11, 2025

    @dmamelin
    Contributor

    @ALERTua the file path indicates the bug is unrelated

  4. dmamelin commented on Dec 11, 2025

    @dmamelin
    Contributor

    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")

  5. ALERTua commented on Dec 11, 2025

    @ALERTua
    Contributor

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

  6. ryan-fessler commented on Dec 11, 2025

    @ryan-fessler
    Author

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

  7. dmamelin commented on Dec 11, 2025

    @dmamelin
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions