Hi all !
I’m new to this home assistant community.
First of all, I’d like to thank everyone who has worked on this amazing project.
I built a water leak sensor around a EDP8266 that communicates with home assistant using MQTT.
It works fine but I would like HA to send an alert in the case the ESP stops to communicate. A kind of watchdog.
Any ideas ? Thanks in advance.
You might be looking for availability_topic:
availability_topic
(string) (Optional) The MQTT topic subscribed to receive availability (online/offline) updates.
I searched around to see if I could find a good example of this because I see a small issue. If you have your ESP8266 publish “online” or “available” to an availability topic, even if it goes offline, the last message received would still say “available”. I guess you would have to have an automation check for the last time a message was received on the availability topic, or have your ESP publish a number that auto increments or something and check to see if it gets “stuck” at a certain number.
MQTT supports something called Last Will and Testament, which is a message to be sent in the event that the client device stops communicating with the broker. I make use of it in this application here which you might find useful as an example.
In that example, I have a device which I create via MQTT Discovery with the device_type
of connectivity
. When that device connects to MQTT it sends “ON” to the connectivity device, and sets up a LWT to send OFF to that same device in the event that it loses connection to the broker. When the device goes offline (I power it off or it goes out of range), a few moments later the broker notices and sends the OFF command to Home Assistant.
LWT is a very handy function and it’s worth learning.
This is definitely not the same. I ping all my devices already, but sometimes they stop communicating with the MQTT broker, but still respond to pings
weird. the only time my esp devices stop talking to MQTT is when they are offline. But I wrote all my own firmware for them, so I guess my logic is different.
This sounds very useful. I do have a question though. In the case where a client connects (and gracefully disconnects) to the MQTT broker only periodically and something goes wrong in between connection attempts, what happens? (Meaning the client just never tries to reconnect because it has an issue).
If your ESP is always on the network (I see no reason why it shouldn’t be, the ESP is quite power hungry and operating on a battery is not that feasible) why not use the PING sensor?
Create an automation that fires when the sensor goes off and sends you a message your preferred way.
We already covered that earlier in the thread. I already use ping sensor. In a perfect world with perfect code, using ICMP should always work. The MQTT LWT seems a little more intelligent because in the end, you care that it is communicating with the MQTT broker, not that you can ping it. I am not trying to sound nit picky, but I am definitely interested in the MQTT method.
I had another thought after started talking about this. @cgtobi mentioned the availability topic. I think one could publish a timestamp on an availability topic and then write an HA automation using a template to compare the last timestamp to the current timestamp and if its greater than say… 10 minutes, you could trigger a notify or whatever.
You can have LWT publish the OFF message to that availability topic. LWT is the way to get this done for all the reasons you just specified, and it’s specifically why that function is part of the MQTT spec.
@luma
That sounds great, but I read some information about LWT and unless I read it wrong, it only “engages” the LWT if an active connection has been terminated improperly. Is that correct? What if an MQTT client only connects every 15 minutes and it disconnects cleanly, but never reconnects?
You read that correctly, it is strictly for unexpected disconnections. If you’re sleeping for power reasons, then for sure LWT isn’t going to help if the device doesn’t come back online.
Thanks for clarifying that. I do like LWT and until you mentioned it, I hadn’t heard of it. Always good to have knowledge about new things. Appreciate it!
Waow ! So many answers from you. Thanks a lot. I’ll try the LWT approach because it’s a fact that MQTT might stop working even if the ESP still pings. I’ll let you know !
If my firmware loses connection to the MQTT broker, it will stop it’s current loop to reconnect and as long as it is still connected to WiFi, it will reconnect.
You could publish a “I’m alive” message every 1 minutes, that reset a 5 minutes timer when received.
And add an automation that send a notification when the timer finish.
This way you’re sure that your esp is not down more than 5 minutes.
That’s pretty much what I do, let the esp send the rssi value once a minute. If that value doesn’t change for 5 minute homeassistant will send me a message.
Jkw
Yep, at first that was the solution I was thinking of but could you point me to an example as I have really no clue how I could do that in HA (the “automation” part) :S