Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

App Initialization #756

Answered by ALERTua
dbsdbs93 asked this question in General
Aug 30, 2025 · 1 comments · 2 replies
Discussion options

I've noticed that during HASS startup, __init__.py in a subdirectory of pyscript/apps/ can start running before other integrations have finished initializing. This makes sense, since I assume HASS dramatically improves its startup time by initializing integrations in parallel. I assume this is expected behavior.

In my __init__.py, I would like to sanity-check my app's configuration (whether that be in its YAML section or otherwise) by checking whether certain entity IDs referenced in the configuration actually exist, using, for example, state.exist(). This works if pyscript is reloading the app because the code has changed, but it won't work during HASS startup, because a given entity's integration might not have initialized the entity yet.

I can mitigate this issue for HASS startup specifically by putting the initialization code in a function decorated with @time_trigger("startup"). But then that function only runs when HASS is restarted. It won't run if pyscript merely reloads __init__.py because the Python code has changed.

Is there a way to get the best of both worlds--i.e., if HASS is starting up, have the function hold until everything has initialized, but otherwise to just run the function if the app has been reloaded?

I don't think I can wait-with-timeout for a start event from HASS, because the event probably won't be sent until pyscript finishes initializing, and that won't happen until my __init__.py finishes running. And that would be a lame solution anyway since it would impose a significant delay with each app reload. I've so happily ditched needing to reload YAML a million times when developing new code...

My fallback will be to delay handling nonexistent entities until they are actually used, but I was hoping to do a little better so that I can catch typos made when editing configuration when the typos are made, rather than some time possibly much later.

Thanks in advance for any ideas you consider sharing.

You must be logged in to vote

The better way is @time_trigger.

@time_trigger
def tryout(trigger_type=None, var_name=None, value=None, old_value=None, context=None, **kwargs):
 log.debug("this will trigger every time this file changes")

via https://hacs-pyscript.readthedocs.io/en/stable/reference.html#time-trigger

Replies: 1 comment 2 replies

Comment options

The better way is @time_trigger.

@time_trigger
def tryout(trigger_type=None, var_name=None, value=None, old_value=None, context=None, **kwargs):
 log.debug("this will trigger every time this file changes")

via https://hacs-pyscript.readthedocs.io/en/stable/reference.html#time-trigger
image

You must be logged in to vote
2 replies
Comment options

Thanks for this quick answer! I had missed that excerpt from the documentation, although I had tried @time_trigger("startup") and found that it was not causing the decorated function to run on reload (unless "reload" here means explicit reload of the integration, rather than an automatic reload of the script due to the code changing on disk).

Interestingly, just before I saw your response, I had noticed that the same reference doc says that @time_trigger("startup") is the same as @time_trigger("now()"), which also seemed to be at odds with what I was seeing.

Either way, I'll take a closer look and report back, either to report that this is the solution or to provide an example where I'm seeing different behavior.

Comment options

When I test this rigorously, I am seeing exactly what the documentation says--not a surprise!--so I can see that I have a different issue I need to chase down in my code.

Thank you again.

Answer selected by dbsdbs93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /