-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lost Sensors after HA Restart with Auto-discovery Enabled #105
Comments
This happens when you are using the built-in homeassistant MQTT broker, when you restart home assistant the retained zigbee2mqtt configuration messages are lost. Possible solution: enable Home Assistant MQTT birth and last will message, listen for them in zigbee2mqtt and send configuration again on Do you indeed use the builtin home assistant broker? EDIT: Another solution would be to just send all configuration messages when zigbee2mqtt reconnects to the MQTT server (even easier). |
Yep, using the builtin mqtt broker. Great, sending all config messages after a reconnect is just what I was hoping for (I suggested this in the OP) - thanks! |
Sorry, a few title typo's there... :) |
If we get auto discovery to send data back, would there be a chance to include sensor updates if data is in cache on the same birth/last messages? |
I've checked with temperature sensor and cube and seams to work 👍 Notes:
Update 1: Actually read the code and figured out that discovery was only for mqtt reconnect which makes sense. Update 2: @Koenkk Unsure how this was implemented, but will this also send actions back? (click, rotate, etc.) It shouldn't send those as you might trigger many automations if you do. Unsure on best approach to only get this for useful things like binary sensors or temperature sensors. Update 3: Maybe it was just a coincidence but my sensor sent data on HA restart. Code seems to only do it on mqtt reconnect - Even on reconnect we should not send clicks & other actions. |
@Koenkk updated and tested - works brilliantly, thank you!! I have the integrated mqtt broker and can confirm my Xiaomi Aqara door/window sensors are automatically re-posted.
|
@ciotlosm these are not resent as these are not cached: https://github.com/Koenkk/zigbee2mqtt/blob/master/lib/controller.js#L291. Messages which contain one of the following keys are not cached: |
I've just tested the cached status and confirm that works as well:
|
sorry, just spotted a problem with the cached state (that open sensor still reports as off in HA), let me do some digging |
Steps followed:
zigbee2mqtt log:
HA log:
I'll enable debug for MQTT on HA and see if that reports anything more. |
The last value your sensor reported is
|
Yeah, all looks great from zigbee2mqtt's perspective, so I wonder if the cached entries are posted too soon/quickly for HA to process them. Perhaps a 5 second delay is needed before the cached payload is sent to HA? I'm just trying to decipher the debug logs to see if it sees the message. Should be in here somewhere. :) |
Yes, probably, can you set |
Either mqtt broker started before HA could process, or HA processed correctly but restored value from recorder once sensor was setup. Best to try retain flag, but most likely not a zigbee2mqtt issue. |
I by no means know much about MQTT, so here is my best guess at what's happening (see comments in the log)
|
@jarrah31 have you set |
I'm not sure I fully understand what this option does. On the wiki it says I have this set to false at the moment. EDIT - testing it with true now... |
Retained messages will be resend when a client subscribes to that topic (while non retained are not). This is controlled by the MQTT broker. So when setting
|
Thanks @Koenkk, setting Really sorry for my misunderstanding of this option and for taking up your time this evening. If you don't mind could you copy your excellent explanation above into the wiki for this option please so that others don't make the same mistake? Thanks again for fixing the original problem in this issue! |
I could reproduce this and got it fixed by setting
Can you confirm? EDIT: this however seems inconsistent with the documentation: https://www.home-assistant.io/docs/mqtt/birth_will/ (as it should be true by default). |
I'm afraid it hasn't worked on my setup for some reason. I have added the retain lines on my HA configuration.yaml mqtt entry as shown above, restarted HA (tried 3 times now, with the 3rd enabling debug), opened the sensors when the connection was lost, and waited for HA to finish booting. In each case, both sensors remained as 'off' within the states page. Logs: https://hastebin.com/edazepuniz.pl The old method of having retain: true in Z2M configuration.yaml no longer works either (set to true in both sets of yaml files). |
It seems that something strange is going on with your HA installation, zigbee2mqtt receives the
|
Yeah, I know what you mean. I'm curious to know if anyone else has the same issue so would you mind if I asked others to test this out on the forum please? On a side note, I've got it working again by commenting out the |
Feel free to ask around! |
@ciotlosm yes, if this is fixed, 0.1 is ready. @jarrah31 can you try increasing the timeout after which the states are send: https://github.com/Koenkk/zigbee2mqtt/blob/master/lib/controller.js#L333. Change Rationale: I tried on my mac which is ofcourse much faster than a pi, perhaps the pi needs more time to get the MQTT broker/home assistant up-and-running. |
That appears to have done the trick! In my case I had to increase the timeout value to 20000 until it worked, so perhaps this could be a configurable option in configuration.yaml? (I just went to 20 straight from 10 so a lower value may work, but it doesn't seem to be an issue with with this length of delay as it took another minute before the GUI was ready) |
oops, sorry wrong button. :) |
This sucks. I think this should be a bug/issue logged in Home Assistant. Ideally when you get hass/online you should have both MQTT and HA state machine up and running, regardless of UI. You shouldn't need a timeout to publish a state to be recorded by the state machine. |
@ciotlosm I completely agree but for now this is the only solution. Would you mind filling a bug at home assistant? |
Hey @Koenkk, sorry to dig up an old thread, but is it possible to move this timeout value to a configuratation variable? This issue has been driving me nuts running Z2M and HA in docker on a RPi4 and every reboot all the sensors report Unknown value. @ciotlosm - Did you happen to raise a HA bug for this? My workaround is re-publishing the birth message to MQTT on HA Start with a delay.
|
@jeremywillans would 30 seconds be a good value? (in that case we can make it the default) |
Happy to try it at 30 seconds ! |
Done, please try with the latest dev branch. |
Tested and works perfectly! Thanks! 👍 |
I would like to find out if this is normal behaviour and whether there's a solution please?
I have auto-discovery enabled on HA and Z2M (zigbee2mqtt - quicker to type!) ;)
Z2M:
homeassistant: true
HA:
If I restart Z2M the sensors automatically appear in HA which works well. However, if I restart HA (after doing some yaml config), the sensors disappear! I then have to also restart Z2M to add them back into HA.
If this is expected behaviour (do others see this too?), perhaps a solution is to publish the sensors back to HA when Z2M detects a restart. For example, I notice in Z2M logs the following occurs during a HA reboot:
At the point where it says
INFO Connected to MQTT server
, could the sensors be published back to HA (it may need a delay if HA isn't ready just then)? e.g. like this:The text was updated successfully, but these errors were encountered: