Time since last specific status

I have a rain sensor, set as a binary sensor (rain / no rain). I want to know how many time ago it was in “rain” value by the last time, and show it in my frontend. Example:

Last Rain: 3 days, 15 hour ago

Can be used in many other cases. For example, last motion, last time opened the gate…

You could get the last updated time using templates, for example:

{{ states.entity_id.last_updated }}

…which gives you the time the entity_id last updated. You could also use:

{{ ((as_timestamp(now())-as_timestamp(states.entity_id.last_updated)) / 60)|round }}

Which would give you the amount of minutes since last update. Playing around with this could give you the correct format you are looking for. You could then just turn thsi into a template sensor and out it beneath, but maybe thatäs not exactly what you are looking for?

1 Like

also https://www.home-assistant.io/components/sensor.history_stats/

Great! That helps a lot. But when I need to restart the HASSIO, it gives an “unknow” state for some seconds. I think that it will be a problem.


I Will try it.