How to get last state change timestamp

Looking at the demo:
image

I’d like to get something similar. I guess a new sensor was made to display the time? What I have at the moment is:
image

And the X hours ago does not correspond to the latest state change for open or closed, as when I check the history bar, the last state change happened 4 hours ago.

Card code is:

entities:

  • entity: binary_sensor.occupancy
    name: Living beweging
    secondary_info: last-changed
  • entity: binary_sensor.contact
    name: Kelder deur
    secondary_info: last-changed
    show_header_toggle: false
    title: Security
    type: entities

Is it not last_changed ? (Ie an underscore _ not - )

Doesn’t seem so, when using underscore instead of dash, the X hours ago is not displayed at all. I’m using 0.93.b2

However, I don’t mind that too much, I’d rather get the datetime of the last motion detected state change on the display

The duration can change if you restart your home-assistant. Last change date is not kept after a reboot.
Could it be the explanation ?

I did not reboot HA (wasn’t home when the time changed).

Please let’s focus on the actual question here which is how do I get that time as the displayed status for a given entity as is done on the HA Lovelace demo :slight_smile:

You will find a link to @arsaboo’s repo in the demo, and editing the UI shows a sensor sensor.ring_front_door_last_motion.
Take a look there.
I assume it’s a timestamp from the ring component.

It looks like it, I do not have a Ring platform however so no luck there. I guess it must be possible to check for example at what time of day Occupancy was last true or when the door state was last open. Any ideas?

Normally you’ll find it in the last_changed property of an entity.
For example:

{{ states.binary_sensor.ez_motion.last_changed }}

Returns a timestamp.
But i think that is the same where HA gets the ‘2 hours ago’ times from.
Maybe your timezone is off?

2 Likes

You should try something like that

platform: template
sensors:
  front_door_last_ding:
  friendly_name: Front Door Last Ding
  device_class: timestamp
  value_template: >-
    {{states.binary_sensor.occupancy.last_changed | timestamp_local}}
2 Likes

I’m getting the following error:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘x00158d00028ab3b7_occupancy’) for dictionary value @ data[‘sensors’][‘motion_living_lastseen’][‘value_template’]. Got ‘{{states.binary_sensor.0x00158d00028ab3b7_occupancy.occupancy.last_changed | timestamp_local}}’. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/

Maybe related to


If not, please post the code for the sensor properly formatted

It seems, something is broken. The same last_changed expression works for all domains, but binary_sensor (maybe others, too):

{{ states['light.dmaker_p45_5d10_indicator_light'] }}
{{ states['binary_sensor.0x00158d00076aaef9_contact'] }}

{{ states.light.dmaker_p45_5d10_indicator_light }}
{# {{ states.binary_sensor.0x00158d00076aaef9_contact }} #} {# ERROR #}

Exactly same construct works for light, doesn’t work for binary_sensor, despite it is also shown as an example on the referenced above page…

Have you tried this as in the docs

{{ states.binary_sensor[0x00158d00076aaef9_contact] }}