Get last change value as hours minutes

Hi everybody.
How can I get the last change the door sensor as a timestamp to calculate how much has passed since that moment?
I need this value:
Снимок экрана 2024-01-16 233432
I’m trying to just get timestamp the last modified , but I can’t seem to get it:

{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.0x00158d00019943cf_contact.last_changed ) | timestamp_custom("%Hh %Mm", false)}}

when testing the template, I get this:

TemplateSyntaxError: expected token ',', got 'ontact'

My sensor

Your binary sensor entity id starts with a number. This is problematic for Home Assistant (and Python variables in general). You need to use square bracket notation in this case, see: https://www.home-assistant.io/docs/configuration/templating/#entity_id-that-begins-with-a-number

So like this:

{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor['0x00158d00019943cf_contact'].last_changed ) | timestamp_custom("%Hh %Mm", false)}}

A better idea would be to rename your entity id to something meaningful, like binary_sensor.front_door_contact

1 Like

Thank you very much, yesterday I tried to write in various syntax but it didn’t work. This is what I needed!

You made a mistake in the parenthesis, you need a second closing one after .last_changed.

Not me :wink: I just copied your original template and added the square brackets.

1 Like