Alarm if MQTT broker goes down

Hi!

So a couple of days ago my MQTT broker started to not work for some reason. I did some restarting and troubleshooting and got it to work again. But the problem is that I lost a couple of days of data from my sensors which I don’t want to happen again.

Can I make an alarm that check whether I receive mqtt data or not to HA?

/ Oskar

I suggest you consider using the expire_after option.

If it’s set to, say, 30 seconds, and fails to receive a new payload during that interval, it will set the sensor’s state to unknown (or unavailable, I can’t recall which one). Make an automation that is triggered when the sensor’s state changes to unknown (or unavailable) and sends you a notification.

Use whichever sensor you may currently have that normally gets updated regularly and frequently (i.e. faster than once every 30 seconds). If you have none that refresh that frequently, change expire_after’s interval to 60 seconds (or more). Worst case, create a dedicated MQTT Sensor and an automation that runs every 15 seconds and publishes a value to that sensor’s topic. Use that dedicated “watchdog” sensor to trigger a notification whenever its state changes to unknown.

2 Likes

Following up on Taras excellent advice and don’t have any that update in less then 30 seconds, if you have tasmota’s, you can choose one and enter ‘teleperiod 10’ in the tasmota console, so it send an update every 10 seconds.

1 Like

Great great idea!

The sensors state changes to “unkown” after 30 seconds now:

Great!

And here is my automation:

alias: Check if online
  trigger:
    platform: state
    entity_id: sensor.house_power_usage
    to: "unknown"
  action:
  - data:
      message: 'MQTT broker or a device is down!'
      target:
      - '#temp'
      title: Important message
    service: notify.temp

It sent a message to slack after 26 seconds from the moment I unplugged the USB-cable to that device!

Thank you very much! :slight_smile:

1 Like

Glad to hear you like the idea. Be sure to correct this typo in the automation, otherwise it will fail to trigger:

Change:

    to: "unkown"

to:

    to: "unknown"
1 Like

hehe yeah of course. Edited my post now! Thanks :slight_smile: