Build automation Trigger off of time since last update of MQTT?

I have some MQTT sensors that pull in temperature for my aquariums. I have the aquarium heaters on some smart switches that kill power to the heater if the temperature goes above a specified temperature and then back on when it goes below a specific temperature. It works really well, but sometimes one of my mqtt temp sensors will go offline and stop reporting temperature. It’s rare, but happens. I would like to build an automation that starts if no new mqtt publish after 15 minutes just resets the power on the switch the esp mqtt temp sensor is plugged into. This reboot should bring my mqtt sensor back online and everything will go on as normal. Not sure how to build the trigger for this automation. Create one automation that starts a timer every time a message is published? Then if that timer changes from active to inactive it flips the switch?

What you want to use is an MQTT Sensor’s expire_after option.

expire_after

(integer)(Optional)
Defines the number of seconds after the value expires if it’s not updated.
Default value:
0

Set it to 15 minutes x 60 seconds/minute = 900 seconds. This means if the sensor doesn’t receive an update within 900 seconds of its last update, its current value will expire and its state will be set to unknown.

Create an automation that is triggered when the sensor’s state changes to unknown. Use the for: option and set it to something like 20 seconds. In other words, we want the automation to trigger only when the sensor’s state changes to unknown for at least 20 seconds (to avoid triggering on any transients or potentially on startup). The automation’s action simply power-cycles the sensor.

Let me know if you need more help creating the automation.

3 Likes

Exactly what I was looking for. Thank you @123!