OH3: How to plot Online/Offline thing status?

OH3 knows the status of a Thing, it you give it the correct MQTT message to monitor.

UID: mqtt:topic:d57a0d76ab:b9a82fa14f
label: Shelley1-AM2302
thingTypeUID: mqtt:topic
configuration:
  payloadNotAvailable: Offline
  availabilityTopic: tele/tasmota_ECEEDC/LWT       <=========
  payloadAvailable: Online
bridgeUID: mqtt:broker:d57a0d76ab

Screenshot 2021-02-14 092132 Screenshot 2021-02-14 092107

How can I Analyze (i.e. display history on a plot) of the online/offline status?

PS … I tried making a channel tied to tele/tasmota_ECEEDC/LWT but that always shows Online … even when it isn’t.

Create a Channel that subscribes to the LWT message on the Thing. Link that to a Switch and assuming you’ve not modified your persistence config it will start saving the states and you can generate a chart by clicking “Analyze”.

I can´t get the item to show OFFLINE. Does the offline condition when the device stops sending those LWT messages is triggered by the persistence service? Which persistence shall I be using for this?

No, it doesn’t work anything like that, or use persistence.
You device must send a special LWT message to the broker, when it starts up. The broker keeps that for later.
openHAB must subscribe to (listen to) the LWT topic.
If the MQTT broker decides the device has disappeared, it will then pass the LWT message to anyone subscribed for it (i.e.openHAB).
Does your device support LWT?

Thank you very much for the response.
I´ve programmed my device, which is a power generator (code made by myself) to send a LWT message at every minute, so it sends to the following topic:

stat/power/generator/LWT

Then I have the following at my things file:

Thing topic Generator “Gerador de energia” [ availabilityTopic=“stat/power/generator/LWT”, payloadNotAvailable=“Connection Lost”,payloadAvailable=“Connected”, keep_alive_time=30000, reconnect_time=60000]{

Channels:

    Type switch : GenReachable [ stateTopic="stat/power/generator/LWT", on="ON", off="OFF" ]

    Type switch : GenSwitch [ stateTopic="stat/power/generator/state", commandTopic="cmnd/power/generator/state", on="ON", off="OFF" ]

    Type switch : GenSleep [ stateTopic="stat/power/generator/sleep", commandTopic="cmnd/power/generator/sleep", on="ON", off="OFF" ]

    Type number : GenTemp [ stateTopic="stat/power/generator/temperature", commandTopic="cmnd/power/generator/temperature" ]

    Type number : GenSmoke [ stateTopic="stat/power/generator/smoke", commandTopic="cmnd/power/generator/smoke" ]

    Type number : GenFuel [ stateTopic="stat/power/generator/fuel", commandTopic="cmnd/power/generator/fuel" ]

    Type number : GenBattery [ stateTopic="stat/power/generator/battery", commandTopic="cmnd/power/generator/battery" ]

    Type switch : GenRunStatus [ stateTopic="stat/power/generator/runstatus", commandTopic="cmnd/power/generator/runstatus", on="ON", off="OFF" ]

    Type switch : GenCeeePhases [ stateTopic="stat/power/generator/phases", commandTopic="cmnd/power/generator/phases", on="ON", off="OFF" ]

    Type number : GenInputWord [ stateTopic="stat/power/generator/inputconditiontopic", commandTopic="cmnd/power/generator/inputconditiontopic" ]

    Type number : GenStateWord [ stateTopic="stat/power/generator/statemachinenumber", commandTopic="cmnd/power/generator/statemachinenumber" ]

    Type switch : GenFanStatus [ stateTopic="stat/power/generator/fan", commandTopic="cmnd/power/generator/fan" ]

}

And also mapped this to my items:

Switch GenReachable “Gerador conectado” (gGenerator) { channel=“mqtt:topic:MyBroker:Generator:GenReachable”}

If I start openhab, the thing goes from offline to online, but when I stop sending the LWT messages it does not go to offline.

Perhaps I did not completely understand this concept, but that is very hard to find examples online.

Thank you very much for helping with this.

Regards,

Luciano

Afraid so. When a device first connects to an MQTT broker, it sends an LWT message, just once. No-one else gets that. It’s like making a Will instruction - what to do in the event of my death.

The broker decides when the originator has died, and then passes the LWT message on to any subscribers.

Overview -

Thank you very much. I had a completely wrong idea of how this LWT worked. I changed my firmware and included the following for connecting to the broker:

if (mqttClient.connect("PowerGenerator", NULL, NULL, lwt_topic, 2, true, "offline"))
  {
    mqttClient.publish(lwt_topic,"online", true);
  ...
  }

Now the client sends a single message during initialization and, if it disconnects ungracefully, the broker publishes its offline state.

Finding this explanation was really difficult. Your help was key!

Regards,

Luciano

1 Like

Over the years I’ve found some best practices in using LWT in MQTT which you and/or future readers of this thread might find useful.

  1. After successfully connecting to the broker, the MQTT client should publish “ONLINE” to the LWT topic retained.
  2. When connecting, configure the broker to publish “OFFLINE” to the LWT topic also retained.

That way the LWT topic will always represent the online/offline status of the device. Even if a client subscribes to that topic after the message is published, they will know the device’s status.

In openHAB, the Generic MQTT Thing has some configuration (you need to click on “show advanced” where you can set the LWT topic and messages for that device. This will cause openHAB to mark that Thing as OFFLINE when the “OFFLINE” (or whatever) message is received.

These approaches make it very easy to get openHAB to reflect the actual ONLINE/OFFLINE status of devices as opposed to making all the Things follow the status of the MQTT Broker Thing.

It’s also handy to link the LWT topic to a Channel and create an Item in cases where you may need to do something special when the device is offline.