Display how long ago a sensor state changed

Hello community,
I am using aqara button WXKG11LM with zigbee2mqtt.
I would like to display on a badge or in a card the last time the button was clicked (any click type).
Does anyone have a suggestion?

Thanks in advance!

Does this button create an entity? If so, does the entity have an attribute for when it was last pressed? If so, you can create a template sensor to capture that time as its state, or use a method (probably custom card or row) to display secondary info.

If not, I assume it creates an event when it’s pressed? If so, you’d need to create an automation to capture the current time ‘now()’ as the state of a template sensor.

Basically I’d like to get the information “Il y a 1 heure” (FR) below (screen shot from the journal) in a markdown card or in a badge
Preferably without adding automation or a new sensor. I believe that it is possible as the journal is displaying it.

Capture d’écran 2020-04-26 à 21.18.31

OK, can you take a screenshot of the entity attributes in developer tools -> states?

It doesn’t contain time related attribute, but I was hoping I could get the information from the journal for instance

Have you tried something like this:

{{ relative_time(states.sensor.0x00158....last_changed | timestamp_local) }}

Works only if recorder is enabled for this entity.

Next you might need to run an update automation - something like this:

- id: 'update_template_sensors'
  alias: Update Template Sensors
  description: ''
  - platform: time_pattern
    seconds: /2
  - data:
      - sensor.t_motion_1
    service: homeassistant.update_entity

This should help you get started. Paste this template into the Template Editor and it should report how long ago the sensor was updated (hours:minutes:seconds).

{{ (now().timestamp() - states.sensor.0x00158d00027bf7f5_click.last_updated.timestamp()) | timestamp_custom('%H:%M:%S', False) }}
  - entities:
      - entity: binary_sensor.porte
        secondary_info: last-changed

should display what you’re looking for.

1 Like

I had the impression the requirement was to display only the ‘last time changed’ value. Nevertheless, your solution is the easiest way to get the job done.

Screenshot from 2020-04-26 16-13-11

indeed simple way to display it

I got this working as well, with the [’’] as my sensor name starts with a number, home-assistant wouldn’t allow your suggested syntax
Thanks for the lead anyway!

{{ (as_timestamp(now()) - as_timestamp(states.sensor['0x00158d00027bf7f5_click'].last_changed)) | timestamp_custom('%H:%M:%S', False) }}

Thank You!
Can you please explain to me what “False” is doing ? :smiley:

The second option determines if the time should be displayed as-is or converted to local time (the default is True which converts it to local time).

Templating - Time

Filter timestamp_custom(format_string, local_time=True) converts an UNIX timestamp to its string representation based on a custom format, the use of a local timezone is default. Supports the standard Python time formatting options.

Sorry to re-open this thread but seemed like a relevant place to ask a follow-up question: Is there any way to get the actual last state change time that survives a home assistant re-start?
When I look at the historical value of the state I can see the actual times so I am hoping there is a way to retrieve it from the recorded values.