MQTT connection sensor?

Is there anyway to create a sensor that monitors the connection between HA and Mosquito? I recently had an issue were my Siren wasn’t getting updates from HA and it turns out HA wasn’t actually comminicating with my instance of Mosquito. I know I can delve into the logs but having a simple status icon on the front end would be great.

You could probably do a command line sensor. Assuming Linux just run ps -A | grep mosquitto and set your value based on output

MTA: for S&G’s I threw this into my config. It shows the current process for mosquitto. It’s pretty safe to assume that if it’s missing then mosquitto is down.

If you just want to monitor the connection to mosquitto from HA, you could run another MQTT client and listen for an LWT message for HA.

The client could then update a sensor in HA via the web api, or even send an alert directly.

That sounds like a good solution but I’m running Mosquitto in a separate docker to my HA install.

Would that not introduce yet another potential point of failure? Trying to minimise problems

you should still be able to do it via ssh command ssh user@host 'ps -A | grep mosquitto'

1 Like

Any code you add is going to be another potential point of failure. Even ps -A | grep mosquitto will give a false positive if mosquitto_sub is running at the same time.

You are correct though, that you need to make sure anything you add makes the system more reliable, not less.

The first thing you have to establish is exactly want you want to check for. Is it the mosquitto process running in the docker container? Or do you trust docker to report that the process has failed so you can report the docker status? Or do you want to check for the actual connection between HA and mosquitto?

I have a command line sensor that checks the systemd status of the mosquito service, which is fine for me. I haven’t had any problems recently of HA disconnecting when mosquitto is available.

I did have something else running on a NodeMCU that sent a notification when it lost connection to mosquitto, but I have since changed notificiations services and haven’t updated the NodeMCU because it just didn’t seem necessary.

I’m pretty confident the docker is running ok and it’s pretty stable as I can see the connection in MQTTfx between Mosquitto and my Sonoff. The issue is on this one occasion the connection between HA and Mosquitto failed and I had no idea (it was resolved with a reboot in HA).

I suppose this ‘ask’ goes for all services that HA makes to external services, the ability to get a ‘connection status’ that can be exposed in the front end would give you a better idea as to what’s happening without having to trawl through the logs.

EDIT:

Looking in MQTTfx you can make a connection to Mosquitto which shows status (version, uptime etc) is there anyways to harness this information within HA?

Mosquitto publishes information about itself to certain topics, but you have to make a connection to mosquitto to get them :unamused:

HA did have a problem with this a while ago, but it has been working fine for me several months now. What release are you running?

I was running 0.69, I try to keep as upto date as possible, taking into account breaking changes.

Maybe that can be the check? If there is no connection/data then let HA know. That would obviously appear in the logs, is there a was a monitor logs for certain events and trigger something if it’s found? Maybe a component?

There is the file sensor component for monitoring this. But it does rely on HA noticing that it has lost the connection.

1 Like

As a hack, you could have Home Assistant publish an MQTT message on a topic every few seconds (say 10s) and have an automation that listens for this message.

Every time you receive a message, you reset and setup a timer that will trigger an automation that will turn off an input_boolean after a defined timeout (like 60s).

Upside of this would be that it really monitors the actual connection - but it’s quite ugly.

4 Likes

That does sound like a solution. Do you think there’s scope to have a ‘connection status’ feature adding to a future version of HA? Not just for Mosquitto but for all connections to ‘external’ services? That way you I can see at a glance if a connection is ‘up’.

I think a generic up sensor would not be possible. Every protocol has a different way of knowing if a service is up or not. For example, with MQTT an uptime sensor needs to know how the MQTT protocol works.

That being said, I guess a good way would be to make use of docker’s container healthy feature to at least make this feature possible for HassIO users.

1 Like

You could do a full loop around with NodeRed, HA and MQTT. Passing messages back and forth.

1 Like