Mqtt last will and testament

Hello,

I’m new to Openhab (v 2.5.9-1) on a RPI 4 and trying to implement Last will and Testament. I’ve a Thing (Arduino Yun with PubSubClient) connected to Openhab.

relevant arduino code

  byte willQoS = 1;
  const char *willTopic = "lwtTopic";
  const char *willMsg = "X";
  boolean willRetain = false;
  while (!client.connected())
  {
    if (client.connect("ArduinoYun", willTopic, willQoS, willRetain, willMsg)) //Attempt to connect
    {

But in OH log viewer I get warning when I disconnect my ArduinoYun Thing and I don’t know why

2020-10-30 16:26:03.727 [WARN ] [ab.binding.mqtt.generic.ChannelState] - Command 'X' not supported by type 'OnOffValue': No enum constant org.eclipse.smarthome.core.library.types.OnOffType.X

My configuration

I want to make a channel that shows if my ArduinoYun Thing is disconnected or not. How can I achieve that ? I’m also not sure what I should fill in in the 3 LWT configuration parameters of the mqtt ArduinoYun Thing.

Any help greatly appreciated
Leo

General comment; when experimenting with MQT work with string type channels and Items to begin with. Then you can see if you’re getting payloads that need transforming for other types.

Your screenshot shows configuration for your Thing. It is valid to enter LWT details here, but if

is actually what you’re after then you need to configure a new Channel within your Thing. There’s a few examples on this forum, but search is currently broken. This post I wrote has details on how to configure a Channel using files rather than PaperUI, but the concept is the same.

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.

1 Like

I use config files and this is how I have mine setup.

Bridge mqtt:broker:myMQTTBroker [ host ="192.168.1.148", secure =false,  clientID ="myMQTTClient" ]
{
 
    Thing topic fan1 "Dining FAN" [ availabilityTopic="IFANdining/tele/LWT", payloadAvailable="Online", payloadNotAvailable="Offline"] {
    Channels:
        Type switch : Power1   "Dining Light "  [ stateTopic = "IFANdining/stat/POWER", commandTopic = "IFANdining/cmnd/POWER", on="ON", off="OFF" ]
        Type dimmer : fanspeed "Fan Speed"      [ stateTopic = "IFANdining/stat/FANSPEED", transformationPattern = "JSONPATH:$.FanSpeed", commandTopic ="IFANdining/cmnd/FANSPEED", 0="OFF", 1="LOW", 2="MED", 3="HIGH", 2=100  ]
        Type switch : reachable "Reachable"     [ stateTopic = "IFANdining/tele/LWT",	on="Online",	off="Offline" ]
      }
}
1 Like

Thank you all very much for the very fast help and guidelines.
This helped me a lot.

Very much appreciated !!

Best wishes
Leo