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

Delay app startup until mqtt is available #579

Answered by FredericG-BE
FredericG-BE asked this question in Q&A
Discussion options

I have an app that registers on mqtt events. At startup the app fails as the registering on the mqtt events fail.

What would be the best way to delay the startup of the app until the mqtt service is started?

Thanks!

You must be logged in to vote

For the record, I added
"dependencies": ["mqtt"]
to the manfest.json a few months ago and have never seen the issue again

BTW, still loving pyscript a lot, I use it for all my automations :)

Replies: 4 comments 2 replies

Comment options

Can you post some code? One hack would be to have a @startup function that includes the mqtt event trigger function in a try... except... loop that tries every few seconds until it succeeds.

You must be logged in to vote
1 reply
Comment options

Yes, I suppose I will try that. From what I have now, I will make some simplified code to test this approach.
Thanks a lot

Comment options

How do you deploy your stack? If you use docker you can set dependencies, and make sure mqtt is running before home assistant starts.

You must be logged in to vote
1 reply
Comment options

I use a standard HA OS on a PI, so no docker I suppose (this is still a bit of a blind spot for me to be honest). Are you referring to the mqtt broker that could be started first? I think it is the HA mqtt integration not being ready.
Thanks a lot

Comment options

I made some simplified code for testing. What I found out:

  • In most cases the registration on the mqtt events works fine when the app is started while HA/pyscript starts. So, I need to try multiple times to reproduce. I suppose that unrelated changes to the code or configuration can make the issue appear or disappear, the nice thing about race conditions
  • There does not seem to be exception raised. There is a message in the log after the trigger was created, see log below
  • When this failure occurred during startup, reloading the code (and so registering to the mqtt messages again) seems to fail silently; no error messages but no reception oven event either.
  • When the registration failed at startup, there is an exception happening when restarting HA as pyscript tries to unregister (or this is what I think it is), see also below
2024年01月31日 08:07:22.102 WARNING (SyncWorker_2) [homeassistant.loader] We found a custom integration openhasp which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
 ....
2024年01月31日 08:07:37.379 INFO (MainThread) [custom_components.pyscript.global_ctx] Loaded /config/pyscript/modules/haspTest.py
2024年01月31日 08:07:37.380 INFO (MainThread) [custom_components.pyscript.apps.mqttTest] HaspManager.__init__(plateName=plate_test) ref=1706684857.3804057
==> 2024年01月31日 08:07:37.381 INFO (MainThread) [custom_components.pyscript.apps.mqttTest] >>> Setting up trigger - Done trigFunc=<custom_components.pyscript.eval.EvalFuncVar object at 0x7f8739c2d0>
==> 2024年01月31日 08:07:37.381 INFO (MainThread) [custom_components.pyscript.global_ctx] Reloaded /config/pyscript/apps/mqttTest.py
==> 2024年01月31日 08:07:37.397 ERROR (MainThread) [custom_components.pyscript.trigger] modules.haspTest.trigFunc: Cannot subscribe to topic 'hasp/discovery/#', make sure MQTT is set up correctly
2024年01月31日 08:07:37.427 INFO (MainThread) [custom_components.pyscript.global_ctx] Loaded /config/pyscript/modules/fgLib.py
2024年01月31日 08:07:37.432 INFO (MainThread) [custom_components.pyscript.global_ctx] Loaded /config/pyscript/dobissinterface.py
2024年01月31日 08:07:37.442 INFO (MainThread) [custom_components.pyscript.global_ctx] Loaded /config/pyscript/example.py
2024年01月31日 08:07:38.284 INFO (MainThread) [custom_components.pyscript.global_ctx] Loaded /config/pyscript/modules/openhasp.py
2024年01月31日 08:07:38.285 INFO (MainThread) [custom_components.pyscript.global_ctx] Loaded /config/pyscript/haspTests.py
2024年01月31日 08:07:38.286 INFO (MainThread) [custom_components.pyscript.global_ctx] Loaded /config/pyscript/louvres.py

While shutting down HA

Traceback (most recent call last): 
 File "/config/custom_components/pyscript/__init__.py", line 347, in hass_stop 
 await unload_scripts(unload_all=True) 
 File "/config/custom_components/pyscript/__init__.py", line 395, in unload_scripts 
 global_ctx.stop() 
 File "/config/custom_components/pyscript/global_ctx.py", line 81, in stop 
 func.trigger_stop() 
 File "/config/custom_components/pyscript/eval.py", line 575, in trigger_stop 
 trigger.stop() 
 File "/config/custom_components/pyscript/trigger.py", line 922, in stop 
 Mqtt.notify_del(self.mqtt_trigger[0], self.notify_q) 
 File "/config/custom_components/pyscript/mqtt.py", line 79, in notify_del 
 cls.notify_remove[topic]() 
 ~~~~~~~~~~~~~~~~~^^^^^^^ 
KeyError: 'hasp/discovery/#' 

The code I used:

modules/hapsTest.py:

import time # if only we could do this in real life...
def trigFuncFactory_mqtt(topic, func):
 # log.info(">>> Setting up trigger")
 try:
 @mqtt_trigger(topic)
 def trigFunc(topic, payload):
 func(topic, payload)
 except:
 log.info(">>> Setting up trigger - EXCEPTION")
 log.info(f">>> Setting up trigger - Done trigFunc={trigFunc}")
 return trigFunc
class HaspManager(object):
 def __init__(self, plateName):
 self.ref = time.time()
 log.info(f"HaspManager.__init__(plateName={plateName}) ref={self.ref}")
 self.plateName = plateName
 self.trigFunc = trigFuncFactory_mqtt("hasp/discovery/#", self.onDiscovery)
 def onDiscovery(self, topic, payload):
 log.info(f"HaspManager.onDiscovery(topic=\"{topic}\") ref={self.ref}")

apps/mqttTest.py:

from haspTest import *
hm = HaspManager("plate_test")

Thanks a lot for your help

You must be logged in to vote
0 replies
Comment options

For the record, I added
"dependencies": ["mqtt"]
to the manfest.json a few months ago and have never seen the issue again

BTW, still loving pyscript a lot, I use it for all my automations :)

You must be logged in to vote
0 replies
Answer selected by FredericG-BE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

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