# Detecting when an MQTT input disconnects

**URL:** <https://discourse.nodered.org/t/detecting-when-an-mqtt-input-disconnects/84132>\
**Category:** Dashboard\
**Created:** [30 December 2023 14:29 UTC](https://discourse.nodered.org/t/detecting-when-an-mqtt-input-disconnects/84132 "2023-12-30T14:29:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![NPT](https://avatars.discourse-cdn.com/v4/letter/n/9de053/32.png) [@NPT](https://discourse.nodered.org/u/NPT)\
**Post date:** [30 December 2023 14:29 UTC](https://discourse.nodered.org/t/detecting-when-an-mqtt-input-disconnects/84132/1 "2023-12-30T14:29:52Z")

</div>

I am running NR on a Pi 3B+ which also has Mosquitto version 2.0.11 installed locally. Various temperature sensors feed in using ESP32-based modules over Wi-Fi and they all work absolutely brilliantly, polling the sensors every 10 seconds and reporting.

However, if a module disconnects or sensor fails, NR still shows the last value received. I am seeking a method of detecting failure of the remote MQTT Client, but so far have failed miserably. This is further complicated because the MQTT-In node will always show 'connected' as long as it is talking to the local MQTT Broker. What it doesn't show is that the transmission **in** has failed and the displayed data will remain unchanged.

I would be very grateful if someone can point me in the right direction or advise me if I am missing something very obvious.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 December 2023 14:32 UTC](https://discourse.nodered.org/t/detecting-when-an-mqtt-input-disconnects/84132/2 "2023-12-30T14:32:00Z")

</div>

Have the esp devices set up a [last will](https://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament/) and the broker will tell you when they drop off.

---

<div class="post-metadata">

**Author:** ![NPT](https://avatars.discourse-cdn.com/v4/letter/n/9de053/32.png) [@NPT](https://discourse.nodered.org/u/NPT)\
**Post date:** [30 December 2023 15:30 UTC](https://discourse.nodered.org/t/detecting-when-an-mqtt-input-disconnects/84132/3 "2023-12-30T15:30:49Z")

</div>

Brilliant!

Thanks very much, Steve - I'm on the case! Checking out the docs for the AsyncMqttClient library on GitHub now.

Cheers.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [30 December 2023 16:07 UTC](https://discourse.nodered.org/t/detecting-when-an-mqtt-input-disconnects/84132/4 "2023-12-30T16:07:57Z")

</div>

I always choose a standard topic for device online/offline. When you connect, send "online" to the topic, then define the will to use the same topic but send "offline" - or use one/zero or whatever suits best. (numeric 1/0 is good if you are wanting to record changes in InfluxDB).

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0d09334e6378b504b5758c871f59edc238db647b.png)

---

<div class="post-metadata">

**Author:** ![NPT](https://avatars.discourse-cdn.com/v4/letter/n/9de053/32.png) [@NPT](https://discourse.nodered.org/u/NPT)\
**Post date:** [1 January 2024 11:33 UTC](https://discourse.nodered.org/t/detecting-when-an-mqtt-input-disconnects/84132/5 "2024-01-01T11:33:22Z")

</div>

Sincere thanks for the excellent guidance and pointers which quickly led to a brilliant result.

For the benefit of anyone stumbling across this post in the future, the solution was extremely simple in my case.

Using the AsyncMqttClient library within a sketch for the ESP32 [or whatever device]

**Set the LWT topic and message**

```auto
const char* lwtTopic = "your/lwt/topic"; [or whatever]
const char* lwtMessage = "module disconnected"; [ditto]

```

Within **setup()**

// Set the Last Will and Testament  
` mqttClient.setWill(lwtTopic, 0, true, lwtMessage);`

Just add an MQTT Node-In to NR & monitor lwtTopic and manage the output to suit.

I hope that helps.
