# MQTT: Using LWT to check which nodes are online

**URL:** <https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499>\
**Category:** General\
**Created:** [21 January 2021 14:03 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499 "2021-01-21T14:03:45Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![bth](https://avatars.discourse-cdn.com/v4/letter/b/a8b319/32.png) [@bth](https://discourse.nodered.org/u/bth)\
**Post date:** [21 January 2021 14:03 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499/1 "2021-01-21T14:03:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 January 2021 14:18 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499/2 "2021-01-21T14:18:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bth](https://avatars.discourse-cdn.com/v4/letter/b/a8b319/32.png) [@bth](https://discourse.nodered.org/u/bth)\
**Post date:** [21 January 2021 14:45 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499/3 "2021-01-21T14:45:18Z")

</div>

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 🙃 ![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e5dc2aff0563420833531843330b68b4e318017a.png)

(image from someone else's previous thread on the subject of LWT)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 January 2021 14:49 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499/4 "2021-01-21T14:49:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 January 2021 15:20 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499/5 "2021-01-21T15:20:49Z")

</div>

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  
.

---

<div class="post-metadata">

**Author:** ![bth](https://avatars.discourse-cdn.com/v4/letter/b/a8b319/32.png) [@bth](https://discourse.nodered.org/u/bth)\
**Post date:** [22 January 2021 09:26 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499/6 "2021-01-22T09:26:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 January 2021 09:59 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499/7 "2021-01-22T09:59:28Z")

</div>

> [@bth](#):
>
> 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).

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](https://pubsubclient.knolleary.net/api#connect)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 January 2021 10:12 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499/8 "2021-01-22T10:12:01Z")

</div>

> [@knolleary](#):
>
> The PubSubClient does let you set LWT details as part of the call to connect

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.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 January 2021 10:13 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499/9 "2021-01-22T10:13:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 January 2021 10:14 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499/10 "2021-01-22T10:14:31Z")

</div>

OK, that makes sense. Thanks.

---

<div class="post-metadata">

**Author:** ![bth](https://avatars.discourse-cdn.com/v4/letter/b/a8b319/32.png) [@bth](https://discourse.nodered.org/u/bth)\
**Post date:** [22 January 2021 10:17 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499/11 "2021-01-22T10:17:47Z")

</div>

Thanks! That answers my problem. YES!!

---

<div class="post-metadata">

**Author:** ![bth](https://avatars.discourse-cdn.com/v4/letter/b/a8b319/32.png) [@bth](https://discourse.nodered.org/u/bth)\
**Post date:** [22 January 2021 10:19 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499/12 "2021-01-22T10:19:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bth](https://avatars.discourse-cdn.com/v4/letter/b/a8b319/32.png) [@bth](https://discourse.nodered.org/u/bth)\
**Post date:** [24 January 2021 13:18 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499/13 "2021-01-24T13:18:48Z")

</div>

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 🙂 , 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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [7 February 2021 13:19 UTC](https://discourse.nodered.org/t/mqtt-using-lwt-to-check-which-nodes-are-online/39499/14 "2021-02-07T13:19:45Z")

</div>

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