Have a sensor default to unavailable if not responding for some time

Hi,

I have a garage door cover that periodically outputs the door state.
Garage door control unit uses MQTT and is coded by me. I can add/remove/change any published messages if needed.

I would like to make Home Assistant put that particular device to unavailable if it haven’t received either a door status message or any other published message from the sensor.

I don’t know how to do it.

So what I understand of this is that I create a binary sensor that acts as the availability topic for my garage door cover.

That binary sensor is constantly rearmed by the garage door cover mqtt “Keep Alive” publication until the garage door goes silent.

Is that it?

Can the garage door cover take a binary sensor input as it’s availability_topic?

The usual method of deciding the availability of a device is with the Last Will and Testament (LWT) mechanism. You need to set the will message on your garage client to send an Offline message, then on startup send an ‘Online’ message to the same topic. There is a good write up about LWT here

I understand the mechanism of Birth and LWT messages but it’s not what I want.

I want to have something in Home Assistant that puts the device as “unavailable” if for some reason the garage door control unit unexpectedly becomes unresponsive (power/network issue, software crash or other).

As of now, if the garage door control unit communicates at least once with Home Assistant, it will show up as if it is available for as long as Home Assistant is running, wether the unit is actually available or not. I want to have some sort of timer that puts it as “unavailable” automatically if it hasn’t received some sort of message for some time, as the garage door control unit broadcast its status periodically.

This is precisely what the MQTT broker is doing when it is deciding whether the client is still connected. It has a heartbeat mechanism (set by default to 60s) and when the client does not respond it sends out the will messages. I think you are trying to re-invent the wheel here.

Oh I didn’t knew it was the broker that was doing this. I thought Birth and LWT messages were coming off the MQTT client device!

Well it does seem my broker doesn’t do all of those. I’m running mosquitto with pretty much default settings (changed user:pass). Do I normally need to configure it in a particular way in order for it to ouput LWT messages?

I coded my own garage door control unit. For now it only publishes the door status, should I add another publication dedicated to the broker?

Thanks!

If you are using the usual pubsub client, you set the will message as part of the parameters to the connect function (I think, going from memory here).

You then have to send a message on start up to the same topic, indicating that you are back online.

The will message and startup message need to match the availability topic in HA.

Create an Mqtt sensor from the message your device is sending out and use this in the config for the sensor. If the sensor is not updated for 300 seconds it will change to ‘unknown’

expire_after: 300

You could also use a value template in an automation to detect last changed state

value_template: "{{as_timestamp(now()) - as_timestamp(states.sensor.garage_door.last_updated) > 300}}"

Thanks alot. It’s all good now. I have a LWT setup in my device and it’s doing exactly what I wanted. It signals “offline” to Home Assistant after a while.

I learned new things :smiley:

I couldn’t believe MQTT & Home Assistant didn’t already have a mechanism to automatically put devices back as unavailable.

Thank you all for your help!

2 Likes

Glad you sorted it, LWT is the best method, the other methods with the expiry times are good where the connection stays but the data stops or doesn’t change. Such as a faulty temp sensor on an Arduino.