Reliable trigger for when MQTT messages are not received

I have a program that generates an MQTT message once a minute, or more often if the signal it is monitoring moves quickly. I configured this signal as a sensor in HA and it works great, use it to trigger an automation no problem.

The issue I have is: sometimes if there is a problem with the network, my program stops publishing - it’s still running, apparently it doesn’t think there’s a problem, but no more sensor updates reach HA. Now, I have a restart script that pkills the existing process and relaunches it, and when that is done it reconnects to mosquitto and everything is good again.

What I would like to have is a trigger in HA that notices that no data has been received from the sensor in, say, 5 minutes, and at that point fire the command to run the restart script. I have seen references to triggering on every sensor data point received and starting the actions with a delay which will restart every time a new data point is received. For whatever reasons, this approach has been unreliable for me.

Is there anything else that will reliably trigger an automation based on a missing “heartbeat” sensor signal?

That is what mqtt’s LWT is for

Not publishing or not receiving?
Use MQTT Explorer to verify that the device is publishing the message or not. There is an add-on for MQTT Explorer, but I recommend that you install it on your computer separate from Home Assistant.

I’m going to go with not publishing. The thing that fixes it stops the publishing program and restarts it.

How do I access LWT as a trigger for an automation?

Depends on how the LWT is configured in your program.

The sensor generates data from 0.0 → 100.0 values over 10.0 trigger an automation. I suppose, since I have control of the sensor publishing code, I could send -1.0 for the LWT, but that seems… clumsy.

LWT is set in it’s own topic, so you look there.

1 Like