#Display on Dashboard the last time + "xx:xx:xx minutes ago" that Ring sensor was opened/closed

23 messages · Page 1 of 1 (latest)

dusty stag
#

I have Ring-MQTT installed on HA and so I have access to the devices + entities of the various sensors, including their open/closed STATE (not time). We have these contact sensors on our doors/windows.

We're potty training a puppy and it'd be nice to be able to keep track of the last time either my wife or myself brought her outside. What would be the simplest way to set up some kind of display on my Dashboard so that I could see last time we opened one of these doors? The first way I can think of is creating + displaying a helper that stores a datetime of the last time the sensor was in Opened state, and then creating an automation that updates this helper each time the state changes to Opened.

hidden onyx
#

A markdown card with a template. Or a Mushroom Template Card. Both would work nicely with a template.

dusty stag
#

I'll look up templates then. Thanks

hidden onyx
#

This will get you started:

{% set entity = ‘binary_sensor.your_contact_sensor’ %}
{{ state_translated(entity)}} - {{ time_since(states[entity].last_changed }}

That will give you and output of Open - 5 minutes ago.

dusty stag
#

Cool! Lots of cool functions going on here

hidden onyx
#

I do something similar to what you want to do.

dusty stag
#

before I go down a rabbit hole, I was on a tutorial for Templates before seeing this and they use states(), vs your states[] - is there a quick explanation on the differences here?

#

I just did a CTRL + F in the HA documentation for Templates and nothing is popping up for states[

#

is it just because we're using a variable there?

hidden onyx
dusty stag
#

oh perfect, and then we're just returning its value at the end

#

good stuff, thank you

hidden onyx
#

Yup. That’s actually the only way (that I know of) to easily get the last_changed attribute, which is what you are looking for.

#

One thing to note, that time changes if HA restarts. So, if you restart your instance, it’ll display the amount of time since the last restart.

dusty stag
#

oh that's a bummer - that sounds like these metadata are simply copied to / updated within HA and not just read in real time from each sensor?

hidden onyx
#

Correct. Most sensors don’t store that information themselves.

dusty stag
#

Doesn't necessarily matter in this case - I don't restart it that often

#

gotcha

latent notch
#

I'm not sure why you need a template, seems like you can do exactly this with a stock tile card?

#

Using templates on the frontend should I think be considered a last resort, it's not good for performance.

hidden onyx
#

Oh… you know what, you’re 100% right… the card can do it itself now. I didn’t even think about that.

dusty stag
#

Hey, this works great as well. Thank you!