Create a Card on Lovelace that shows me the last time a door sensor changed it state

Hello everyone,

I want to create a card that shows me the last time my door sensor changed its state.
Everything I tried so far gave me only the state of the contact sensor (Closed \ Open).

I want to see the hour when it’s closed or opened, like in HA Demo:
Screenshot 2023-04-17 114704

I think you will need to use a template to extract this information as its not part of the entities attributes.

The template structure would be something like this:

{{ as_timestamp(states.binary_sensor.front_door_last_ding.last_changed)
| timestamp_custom ('%H:%M' ) }}

Changing the XXX part to marry your entity
states.binary_sensor.XXX_XXX_XXX.last_changed

However note that this method (and I dont know of any other, but there might be) will show the last changed OR the last time HA was started so wont always be an accurate representation.

To use this on the frontend you could either use the above template in a card that supports templates like markdown card for example or you could create a template sensor in your yaml files something like this maybe:

template:
  - sensor:
      - name: front door last changed time 
        state: >-
           {{ as_timestamp(states.binary_sensor.front_door_last_ding.last_changed) | 
           timestamp_custom ('%H:%M' ) }}
1 Like

You can use secondary-info to show the last changed time under the entity state.

https://www.home-assistant.io/dashboards/entities/#secondary_info

2 Likes

Yeah, I tried it, but this is not what I need.
I need to see the time (HH:MM).
:smiley:

Thank you! It works :slight_smile:

1 Like