SAIC-iSmart-API / saic-python-mqtt-gateway

A service that queries the data from an MG iSMART account and publishes the data over MQTT and to other sources
MIT License
71 stars 21 forks source link

Q: LWT topic trigger time #120

Closed cybersmart-eu closed 9 months ago

cybersmart-eu commented 10 months ago

Hi,

there is an _internal/lwt topic but I did not manage to find out when the broker is requested to trigger it.

To my best understanding the gateway needs to tell the broker it‘s lwt parameters incl. the „timeout“ which will trigger the lwt-topic.

Where is this timeout defined? I shut down the gateway for some time waiting for the broker so send lwt, bit nothing happens even after several minutes?

Maybe someone can give me a hint where this timout is defined. Maybe it can be made „configurable“?

BR Uwe

tosate commented 9 months ago

Hi Uwe,

detailed information about LWT can be found here

Last Will and Testament (LWT) is a powerful feature in MQTT that allows clients to specify a message that will be automatically published by the broker on their behalf, if or when an unexpected disconnection occurs.

The last will is configured in the file mqtt_publisher.py

self.client.will_set(
            self.get_topic(mqtt_topics.INTERNAL_LWT, False).decode('utf8'),
            payload='offline',
            retain=True
        )

There is no timeout specified. On the website mentioned above you find the paragraph "When does the MQTT Broker Send the LWT Message?" that states the following:


According to the MQTT 3.1.1 specification, the broker sends a client’s Last Will and Testament (LWT) message in the following situations:

  1. I/O error or network failure: If the broker detects any issues with the input/output or network connection, it will distribute the LWT message.
  2. Failed communication within Keep Alive period: If the client fails to communicate with the broker within the specified Keep Alive period, the LWT message is sent. In Part-10 of our MQTT Essentials, we will explore the concept of MQTT Keep Alive time and delve into its significance it.
  3. Client closes connection without DISCONNECT: When the client terminates the network connection without sending a DISCONNECT packet, the broker ensures the LWT message is distributed.
  4. Broker closes connection due to protocol error: If the broker closes the network connection due to a protocol error, it will send the LWT message.

A keep alive period (case 2) is not implemented. The broker should publish the 'offline' message on behalf of the MQTT gateway if one of the other cases occurs.