allangood / rtlamr2mqtt

Docker container to send rtlamr readings to a mqtt broker
MIT License
292 stars 47 forks source link

MQTT LWT (last will and testament) doesn't work #185

Closed melyux closed 1 year ago

melyux commented 1 year ago

I found that if I ungracefully stop rtlamr2mqtt somehow (like pulling the plug or disconnecting the internet), the status will remain "online". It only becomes "offline" if rtlamr2mqtt gracefully shuts down. I noticed this when my rtlamr2mqtt went down for days but never showed an indication (especially insidious because rtlamr2mqtt publishes asynchronously, so right now there is no way to ever know whether rtlamr2mqtt is down or no electricity/water/gas has been used since the last MQTT update).

Not sure from a look at the code why the will doesn't work. But I've never used the publish.single() method of setting the will with Paho, I just use the client.will_set() in my projects, so I'm not experienced with the method used here.

allangood commented 1 year ago

This project doesn´t keep a permanent connection with MQTT. This means that the LWT will only be set to "offline" if the container dies during a status update. If the container dies during a sleep time, then the only way to know if it is working is using the "last_updated" attribute from the sensor.

melyux commented 1 year ago

That's the point of the LWT functionality in MQTT. It automatically sets the status to "offline" if your application loses connection to MQTT for longer than a set amount of time. It's not just for consciously setting it to "offline" manually.

allangood commented 1 year ago

If you read my comment, you will see that this project will not keep a constant connection with MQTT. It connects, send the payload and then disconnects. Keeping an constant connection is out of scope for this project as it is possible to catch this using the last_updated attribute in HA.

melyux commented 1 year ago

In that case the status topic is misleading. Why have it at all if it's purposefully not reliable? Or you could have the MQTT connection be persistent for LWT to work normally like it does with other projects that use MQTT

This would be the better way to go about it even if last updated were reliably updated, but it doesn't seem like last updated does a constant heartbeat anyway, so it can't be used to keep track of a disgracefully closed connection vs a sensor that just hasn't increased in a while.

melyux commented 1 year ago

Mind re-opening this?

allangood commented 1 year ago

I am rewriting the whole project and this will be in the new version!

melyux commented 3 months ago

Hey Allan, were you able to get this working?