MQTT Watchdog?

Is it possible to set up an automation to detect that your MQTT broker has failed? Be it that it never started or stopped for some reason?
I’d like to have an automation start/restart MQTT if ever needed because it is currently my single point of failure.

Is it running on the same machine? Could create a sensor to track status I guess?

binary_sensor:
- platform:  command_line
  name: MQTT Status
  command: 'systemctl is-active mosquitto || true'
  # Returns true if value is active. False otherwise. 
  value_template: "{{ value == 'active' }}"

If this ever turns off, mosquitto service is no longer active…

But if you did this, I’m pretty sure you can just have your mosquitto service start itself.

Not sure if this is what OP meant, but I actually have a smart plug, and was thinking of turning it into a watchdog with ESPHome.

The idea was to have a script with a loop, within that loop there’d be a delay at first, and then a relay toggle.

Upon an mqtt message, or something similar TBD, it would stop and execute again the ESPHome automation script, thus resetting the delay.

I created a watchdog in the past when I had two instances running. It ensured that both instances were running, automations were executing and MQTT was working. Maybe you can use the concept to create something with what you have running.

I created a MQTT switch in both instances that watched the same topic.

Both instances had an automation to turn on/off the switch on a set interval.

Then an automation watched if that switch stayed in a certain state for to long. (eg. if instance one turned the switch off, it watched for the switch to be off for too long. )

The automation would then send a notification that the opposite instance was offline or there was a MQTT issue.

Yes, I’m running the Mosquitto MQTT addon in my HassIO instance.
This requests sort of extends to all add-ons available to HA, being able to make sure that things that are supposed to be running are in fact running.

As I understand it, HA and all the addons run as containers, so I’m unsure if the systemctl service would actually have proper access to all of them.

I’ll look into that solution however.

If you just want to know if the add-ons are running you can do the following. This doesn’t ensure they are functioning as intended though.

Create a sensor to pull the state.

Optional to create a switch.

Create an automation to monitor the sensor or switch.

Or monitor a sensor that depends on that add-on

I use VerneMQ for my MQTT broker in bridge mode. This allows me to run a cluster of MQTT servers (2 currently) and keep them synced. This also allows me to do remote MQTT brokers (should I ever need them).

I use Phil Hawthornes hadockermon https://github.com/philhawthorne/ha-dockermon and Ludeuus’ https://github.com/custom-components/binary_sensor.hadockermon (which he has archived now)

It allows me to have this:
image

Which I could use in an automation to restart a container that fails. I do use docker-compose to start Phils container - it’s not a hassio addon.

Ludeuus depreciated his switch in favor of a different integration which is far less useful that his component with Phils docker container unfortunately. (and incomprehensible to setup with hassio - not that I’ve invested that much time into it)

EDIT:
Just looking at it again now, maybe it’s not as difficult as I expected/thought. I might give it a try…

An old thread, but I’m using this to keep an eye on particular mqtt topics (I’m running this script in a docker container) kmwpub / mqtt-watchdog · GitLab