tasmota / docs

Documentation for Tasmota (https://github.com/arendst/Tasmota)
https://tasmota.github.io/docs
156 stars 744 forks source link

LWT usage at TASMOTA compared to MQTT default #987

Closed stefanbode closed 2 years ago

stefanbode commented 2 years ago

Hi gents, the latest change let me think about the LWT. It is true that normally this is only for unexpected disconnects, but my experience with TASMOTA is, that we use the same topic also for normal disconnects that are intended by the client. E.g. Firmware update, executed by: WifiShutdown();

barbudor commented 2 years ago

the latest change let me think about the LWT

What do you mean by latest change ?

From: https://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament/

In MQTT, you use the Last Will and Testament (LWT) feature to notify other clients about an ungracefully disconnected client. Each client can specify its last will message when it connects to a broker. The last will message is a normal MQTT message with a topic, retained message flag, QoS, and payload. The broker stores the message until it detects that the client has disconnected ungracefully. In response to the ungraceful disconnect, the broker sends the last-will message to all subscribed clients of the last-will message topic. If the client disconnects gracefully with a correct DISCONNECT message, the broker discards the stored LWT message.

Indeed, on a basic restart 1 we can see the LWT been sent.

MQTT disconnect seems to be handled by xdrv_02_9_mqtt.ino:MqttDisconnect() which calls PubSubClient::disconnect()

The reason might be in PubSubClient::disconnect(bool disconnect_package = false) As Tasmota calls PubSubClient::disconnect() without arguments, the value is false and my understanding of the code is that with false, the gracefull MQTT DISCONNECT message is not sent and the TCP link is broken abruptly. https://github.com/arendst/Tasmota/blob/9b750f8b70c389c5a7e00cab3510c7b10a90003b/lib/default/pubsubclient-2.8.13/src/PubSubClient.cpp#L772-L793

Not sure it was your question as you posted in Docs

stefanbode commented 2 years ago

Thanks for adding more information. Question regarding Tasmota: if the LwT offline also on all gracefully disconnects. I see in deepsleep and firmware updates also a corresponding LWT.

stefanbode commented 2 years ago

For me it is fine that on any disconnect with Tasmota we send the offline. Just enhance the docs

ascillato commented 2 years ago

Hi gents, the latest change let me think about the LWT. It is true that normally this is only for unexpected disconnects, but my experience with TASMOTA is, that we use the same topic also for normal disconnects that are intended by the client. E.g. Firmware update, executed by: WifiShutdown();

Yes, exactly. Every time that Tasmota restarts normally, for any reason, the clean close connection is sent, hence the LWT OFFLINE is shown in the broker. This is in fact an intended behavior due to the fact that sometimes, depending on changes on the configuration (like Topic), if you don't send the "clean close", the device is kept as ONLINE in the broker.

There is an old issue and PR about this with the whole explanation. I will try to find it.