Let’s step back a moment and explain how LWT works and how the LWT settings on an openHAB MQTT Thing work.
The LWT message is something a client tells the MQTT Broker to publish when the MQTT Broker thinks that the client has lost the connection. Seems simple enough. However, that by itself isn’t all that useful. All the clients that care about the online status of a device would have to be actively connected to the broker and subscribed to the LWT topic at the time that the LWT message is received. If your device goes offline and then later openHAB connects to the MQTT broker, it would have no idea what state the device is in. It wasn’t there to receive the LWT message so it doesn’t know it’s offline. And there is nothing else it can check to see if it’s online.
Now, if you set willRetain
to true
than any client will receive that LWT message even if it wasn’t connected when the device went offline. But it will also get that LWT message even after the device comes online.
So what you want to do is when you’ve successfully connected to the broker from the Arduino, publish a retained message to the LWT topic to indicate that it’s online. Register a LWT with retained set to true
and when the broker see’s it’s disconnected it will publish the offline message. Once you do this, it is always possible to know the online/offline status of the device even if the client wasn’t connected when the online/offline messages were sent.
The openHAB Thing requires this behavior. If you don’t publish an online message to the LWT topic it will default to showing the Thing as OFFLINE. If you don’t publish the messages as retained, openHAB will assume the device is OFFLINE until it receives a message telling it otherwise.
Next, you are sending the offline message as X
. It would be a lot more self documenting if you use ONLINE/OFFLINE
or ON/OFF
as the message. Your topic should also be specific to that device. lwtTopic
is probably too generic.
Now look at the documentation under the “Payload available” and “Payload not available”. What that is trying to tell you is that by default it is looking for an “ON” message to indicate that the device is online and an “OFF” message to indicate that the device is offline. If you want to override the default, you just need to type in the message that will be published to the LWT topic to indicate it’s online and the message that indicates it’s offline (X
given your Arduino code above).
Finally, once you get all that set up, all that it will change is the Thing’s status. When the offline message is received the binding will mark that Thing as OFFLINE and all the Items linked to it’s Channels will be set to UNDEF
I believe. You can trigger rules based on Thing status and you can query the Thing’s status from Rules as well. But if you want an Item to represent the online status of the device, as mentioned by others, you will need to create a Channel for that.
“site:community.openhab.org blah blah blah” will work from most search engines until the search gets fixed.