Closed FredericMa closed 3 years ago
I'll try and reproduce here. Thanks for reporting.
It's by design. status
and info
are only published on first connect, not on reconnects. heating_active
and tapwater_active
are only published on change.
status
ist always published with retain flag, because it is used as last-will, the others as configured in user-settings.
Afaik retains means, that the broker should keep the message. If the broker is stopped the message is lost.
We can generate a new info messages with "event": "reconnect"
.
I can't reproduce. I set the MQTT retain to true in EMS-ESP. MQTT Clean session off. Start EMS-ESP. All the topics coming in have the retained flag. Re-start the MQTT broker and the messages are still there. This is what retain does, it makes them sticky.
It's specifically the status
message that triggered my attention since the ems-esp information was displayed as unavailable.
I'm using regular MQTT, not the HA discovery.
So if the broker is restarted, entities like this will remain unavailable forever until ems-esp is restarted:
- platform: mqtt
state_topic: 'ems-esp/heartbeat'
name: 'heating_ems_esp_wifi'
unit_of_measurement: '%'
value_template: '{{ value_json.rssi }}'
availability_topic: 'ems-esp/status'
payload_available: "online"
payload_not_available: "offline"
The exact order of events in my case is like this:
status
message since the ems-esp only reconnected and didn't connect for the first time.The only way to solve this in my case is to reboot ems-esp after every server reboot.
the status
topic is always retained. It'll never disappear unless you purge the MQTT broker.
Do you have persistence to disk enabled on your broker? I'm using mosquitto and by default persistence to disk is disabled. I never changed it so persistence is disabled. That's probably the reason why you are seeing the messages after a broker restart since they are loaded from disk and in my case mosquitto is starting clean.
that's correct. my mosquitto config has
persistence true
persistence_location /mosquitto/data/
Is there any disadvantage why status
(and maybe others like info
) should not be published on reconnect?
Another reason why I'm not a big fan of enabling persistence on the broker is the fact that you don't know if the data is still accurate. Let's say the broker goes offline and comes up again after 2 hours but it's possible that in the meantime the ems-esp went offline. In this case you get a false positive that the device is online while it is actually not. Eventually the will message will kick in I suppose. So it will solve itself after a while but it might have already triggered automations in your home automation system which might not give the expected result.
Actually no reason. I'll move status and info to the MQTT reconnect code.
Great thanks!
At the moment I'm not using heating_active
, tapwater_active
so no issue for me but won't they suffer the same issue?
If they would have the same issue, it is possible that for example HA thinks tapwater is active while it might not be active and won't notice it until it changes. So in that case you won't even notice it has been not active.
Let's say the broker goes offline and comes up again after 2 hours but it's possible that in the meantime the ems-esp went offline. In this case you get a false positive that the device is online while it is actually not.
For this case there is the last-will and the broker sets offline
to the retained status if ems-esp is not connected.
Without saving there are a lot of other publishes lost: The HA-config is publishes once with retain on device registering. The device-messages seems to publish on reconnect, but this is only the queued messages while offline are send out. If you have large intervalls you have to wait for the next message.
also true, having the retained flag set and telling the broker not to persist the messages sounds counter-intuitive?
Yes, you're right, in that case it is covered if you configure availability for all entities.
I enabled the retain flag so when HA starts it immediately uses the latest received data, otherwise they show up as unavailable until the next update. OK, it will only take 10 seconds before they get updated to the correct state instead of unavailable but I think it is nicer to use the latest retained messages.
the status topic is published on MQTT re-connect. The others don't make sense to add.
Thanks! Would it make sense for the other topics to follow the retain flag setting? I can imagine that it would be confusing for people that have the retain flag enabled but not all topics are actually retained. Like I said, no issue for me since I don't use them at the moment.
if the retain flag is set, all topics are sent with the retained flag set. It's always been like this. It's up to the broker how it retains them, so if it doesn't persist to disk it'll never be able to recall the messages when the broker restarts. The change I made is to send out the status topic on every MQTT re-connect.
Correct, sorry, brainfart from my side.
Bug description If ems-esp reconnects after the broker has been restarted, not all messages that should be retained are being retained. I'm missing
status
,heating_active
,tapwater_active
andinfo
. If I reboot ems-esp afterwards, the missing retained messages are available. I noticed this since the ems-esp status showed up as unknown after a server restart (including broker). I connected to the broker and saw that the retainedstatus
message was missing. I restarted ems-esp and all was fine afterwards.Steps to reproduce I can simulate this by restarting the broker and checking the retained messages. The mentioned messages above are missing. I restart ems-esp and check again and the missing messages will show up as retained.
Expected behavior Retained messages should also be available after it reconnects to the broker if it is restarted.
Screenshots After broker restart:
After ems-esp restart:
Device information