MQTT Sensor offline state

Hello everone! I’m very new to HA and I’m trying to find a way of displaying an online/offline state from an MQTT sensor if there hasn’t been any updates in the respective topic in the last xx seconds. If that happens, I need to display an “offline” or similar on the frontend and fire an automation that notifies me. Maybe I’m missing the obvious but I can’t seem to find an easy way to set this up. Has anyone implemented anything similar?

Thanks!

You can use state.last_changed

I’m not sure that will work. I think last changed is the time of the last change, not the time of the last message. I’m not sure if there is a “last update” (or something similar) or not. But that always gets me because my MQTT temp sensors on the main display show a really old time even though they’ve been sending messages because the temperature was constant.

I’m not sure that will work

Well, just give it a try… I works for me. But don’t forget to add force_update: true to make sure sensor get’s updated even if the temperature didn’t change.

Pardon me, I get the idea of using state.last_changed in order to compare it to the actual time and see if it surpasses an xx time threshold but I’m very new to HA and Jinja2, could you please elaborate a simple example for me?

Thanks for the help!

@Torce search and you’ll find