Action on device failing to post to mqtt

I have a device that sometimes crashes but is still pingable. It posts to mqtt data on a regular basis when it’s working. Is there any way to watch for lack of publishes over time and act on that situation? The acting part is easy, just don’t know how to code something to watch.

Set up a timer and an automation that starts it when HA starts.

Now set up an automation that triggers with the MQTT topic of the device you want to watch.
The action of this automation will be to reset the timer.

A third automation that triggers when the timer finishes should send you a message or some other way to indicate the fault or: flash light.

I’ll have to figure out the second part. Currently, when the device stops reporting, in this case Watts, the number for the sensor just stays the same.

What we’re aiming to do here is set up a timer that counts down to zero.
When your device sends a MQTT message, the timer will reset and start counting down again.
When your device fails and doesn’t send a MQTT message, the timer will eventually reach zero and trigger a notification so you are made aware of the failure.

In your config.yaml create a timer

timer:
  mqtt_heartbeat:
    duration: 00:10:00 
    #time formatted as hh:mm:ss will need to be changed depending on how often the device fails

Now create an automation to start the timer when Home Assistant starts.
In your automations.yaml

- alias: HA Startup
  id: '12345'
  initial_state: true
  trigger:
  - platform: homeassistant
    event: start
  action:
  - service: timer.start
    entity_id: timer.mqtt_heartbeat

And now we want an automation that listens for your device’s MQTT messages and resets the timer

 - alias: MQTT Listener
      id: '54321'
      initial_state: true
      trigger:
      - platform: MQTT
        topic: path/to/your/devices/MQTT/publish/topic
      action:
      - service: timer.start # restarts timer at its initial value
        entity_id: timer.mqtt_heartbeat

Lastly an automation that triggers when no MQTT message is sent and the timer eventually times out

 - alias: MQTT Fail
      id: '54312'
      initial_state: true
      trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.mqtt_heartbeat
      action:
      - service: notify.dax333_your_notification_platform
        data:
          message: "Devices MQTT has stopped !"
1 Like

Thank you so much. That is perfect!

…but does it work ?

Works great, thank you.

  • platform: MQTT needed to be lowercase.