Last Activity Date and Time from a Sensor that have not

Hi,
I am new on HA and I am migrating from OpenHab.
I have lots of occupancy and contact sensors, and I would like to have in my dastboard an Entity with last activity of each single sensor.
My idea is to create a datetime helper for each sensor, an automation to update helper then show on Dashboard.
But this is a nightmare to implement and mantain. What I am looking for is a smarter and simple way to have Last Activity for each sensor without create lots of helper and lots of automation.

Many thanks in advance for help

Marco

Each and every HA entity has 2 semi-hidden attributes:

  • last_updated: time the state or any attribute of the entity last changed
  • last_changed: time the state only of the entity last changed

You can read them with, e.g.

{{ states.light.lampe_bureau.last_changed }}

Beware that {{ state_attr("light.lampe_bureau", "last_changed") }} won’t work, though, as those are not attributes proper.

Hi @koying
it work great. Using:

{{ states.binary_sensor.xiaomi_occupancy_ingresso_occupancy.last_changed }}

I got:

2023-08-09 04:57:29.773421+00:00

I am on CET timezone and now I am on Daylight saving, so my time is 2 hour ahed the value returned. HA is configured on my timezone, is there a way to get date on my timezone and formatting in italian way ( day/month/year hh:mm:ss)?

Many Thanks in advance

Fixed,
here is the code:

{{ states.binary_sensor.xiaomi_occupancy_ingresso_occupancy.last_changed.timestamp() |
  timestamp_custom('%d.%m.%Y') }} {{
  states.binary_sensor.xiaomi_occupancy_ingresso_occupancy.last_changed.timestamp() |
  timestamp_custom('%H:%M:%S', true) }}