Detecting when an MQTT input disconnects

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.

Have the esp devices set up a last will and the broker will tell you when they drop off.

1 Like

Brilliant!

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

Cheers.

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

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

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.

1 Like