MQTT: Using LWT to check which nodes are online

Hi,

I'm working on getting the LWT (Last will testament) feature to work how i want it to in my system. So right now i have added messages to the broker settings:

connected --> send "online" on topic: topic/devicestatus
disconnect --> send "offline" on topic: topic/devicestatus
unexpected disconnect --> send "failed" on topic: topic/devicestatus

I then for a start created a mqtt in node connected to a debug node. The debug node instantly read "online", which is great because there are a client connected.

-- But i would like to know the ID of the client that provoked a LWT. Is this possible, or do i need an individual broker for each client to see that?

You need to include the client identifier in either the topic name or payload of the LWT message

But how do i do individually for each of my clients, because where i have done it is on the broker side, and i do not see how i can do it individually for each clients. Maybe i just don't see the obvious :upside_down_face: (not my image)

From that image, you are publishing to the topic devicestatus/kitchencheer - so is kitchencheer not the information you need here?

That image you show is the client configuration for connection to the broker. If you have another node red or device it will have a different configuration
.

Okay. Thank you for trying to making me understand this.

So maybe i'm doing this the wrong way around. I'm using a RPI running nodered + mqtt (mosquito) as a controller, and i have esp32 nodes sending and receiving mqtt messages.

I do have a hunch, for this to make sense, that i need to do the lwt settings on the nodes, using the pubsubclient (Arduino).

Because where i've made the setting now is under "Edit MQTT-broker node" in nodered on my RPI. And what i think that does is only to tell if the node is connected to the mosqiutto service.

I would like to know if the esp32 devices are online, and a notification if they are not.

Yes, your hunch is correct.

The node settings only apply for the Node-RED client connection to the broker. If you want to know about other clients, you'll need to configure them with their own LWT settings.

The PubSubClient does let you set LWT details as part of the call to connect - Arduino Client for MQTT

1 Like

Just a note that my interpretation of that library is that it only supports a disconnect LWT message. I had to generate my own reconnect message in the arduino code when MQTT connected (which is very easy of course). Perhaps I missed something.

1 Like

Right - a reconnect message is not a standard feature of an MQTT Client and easy enough to implement using the PubSubClient. The Node-RED node offers it as a built-in configuration because it would be quite hard to do without it being built in.

1 Like

OK, that makes sense. Thanks.

1 Like

Thanks! That answers my problem. YES!!

I was just about to ask this, coming from the documentation. You guys are great :slight_smile:

Now i've got the system up and running for a few days. I ended up adding an email notification to report on the client status. It is working great! I now have to solve a connection issue. In the last few days I've gotten over 600 messages of the status of the client :slight_smile: , might be a bit too much, around 1 to 10 minutes between each reconnect. Still, i can see it is mostly online.

But, it is really nice to know this as before i was in total darkness, as it is set up in a remote location that only visit from time to time.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.