How to get timestamp_local ("Minutes ago...") in a glance card from custom sensor template?

Hi to all,
following this topic (Last-changed value of a sensor) I got my newest last transmission from my sensor.
But I get it (in a glance card, inside a custom vertical card) in timestamp UTC, but I would it in “human readable” format (e.g. 7 minutes ago, 2 days ago). I get it only on popup info, but I would in my card.

This is final result
final1

final2

This is some test I did:


- platform: template
  sensors:
    ingresso_pir_last_tx:
      value_template: >-
        {%- set values = [
          states.binary_sensor.pir_ingresso_myss_100_0.last_changed,
          states.sensor.pir_ingresso_myss_100_1.last_changed, ] -%}
        {{ (values | max) | timestamp_local }}
      device_class: timestamp


- platform: template
  sensors:
    ingresso_pir_last_tx:
      value_template: >-
        {%- set values = [
          states.binary_sensor.pir_ingresso_myss_100_0.last_changed,
          states.sensor.pir_ingresso_myss_100_1.last_changed, ] -%}
        {{ values | max }} | timestamp_local
      device_class: timestamp


- platform: template
  sensors:
    ingresso_pir_last_tx:
      value_template: >-
        {%- set values = [
          states.binary_sensor.pir_ingresso_myss_100_0.last_changed,
          states.sensor.pir_ingresso_myss_100_1.last_changed, ] -%}
        {{ values | max }}
      device_class: timestamp

I get same result with all 3 codes.

Thank you to all!

timestamp_local is causing the problem. Also, you need to use as_timestamp().

- platform: template
  sensors:
    ingresso_pir_last_tx:
      value_template: >-
        {%- set values = [
          states.binary_sensor.pir_ingresso_myss_100_0.last_changed,
          states.sensor.pir_ingresso_myss_100_1.last_changed, ] -%}
        {{ as_timestamp(values | max) }}
      device_class: timestamp

Timestamp local takes a timestamp and turns it into a string.
last_changed properties are datetime objects, not timestamps. In order to change a datetime object into a timestamp, you need to use the method as_timestamp.

Thank you.
I got the timestamp, with your method. I’m formatting the date now with

- platform: template
  sensors:
    ingresso_pir_last_tx:
      value_template: >-
        {%- set values = [
          states.binary_sensor.pir_ingresso_myss_100_0.last_changed,
          states.sensor.pir_ingresso_myss_100_1.last_changed, ] -%}
        {{ as_timestamp(values | max) | timestamp_custom("%d-%m %H:%M") }}
      device_class: timestamp

Thank you!

  - platform: template
    sensors:
      sensor_changed:
        friendly_name: 'Changed:'
        value_template: >
          {%- set values = [
            states.binary_sensor.backyard.last_changed, 
            states.binary_sensor.balcony.last_changed, 
            states.binary_sensor.bedroom.last_changed,
            states.binary_sensor.dining.last_changed,
            states.binary_sensor.garage.last_changed,
            states.binary_sensor.jardin.last_changed,
            states.binary_sensor.kitchen.last_changed,
            states.binary_sensor.living.last_changed,
            states.binary_sensor.office.last_changed,
            states.binary_sensor.stairway.last_changed, 
            states.sensor.pir_colmado.last_changed, 
            states.sensor.pir_front_door.last_changed,
            states.sensor.pir_garage.last_changed, 
            states.sensor.pir_stairway.last_changed] %}
           {{ as_timestamp(values | max) | timestamp_custom('%Y-%m-%dT%H:%M:%S') }}
        device_class: timestamp

Thanks for the examples. Now I have a time since last change

1 Like

It stopped working for me at some point…
Now this is (true, now it is in UTC timezone):

  • platform: template
    sensors:
    last_changed_timestamp:
    friendly_name: ‘last_changed_timestamp’
    value_template: >
    {%- set values = [
    states.binary_sensor.aq_magnet_bedroom_kv_contact.last_changed.isoformat(),
    states.binary_sensor.aq_magnet_kitchen_kv_contact.last_changed.isoformat(),
    states.binary_sensor.xi_magnet_hallroom_kv_contact.last_changed.isoformat(),
    states.binary_sensor.xi_motion_bedroom_kv_occupancy.last_changed.isoformat(),
    states.switch.sonoff_1000f27662.last_changed.isoformat(),
    states.switch.aq_switch_double_right_bedroom_kv.last_changed.isoformat(),
    states.switch.aq_switch_double_left_bedroom_kv.last_changed.isoformat(),
    states.switch.aq_switch_double_right_bathroom_kv.last_changed.isoformat(),
    states.switch.aq_switch_double_left_bathroom_kv.last_changed.isoformat(),
    states.switch.aq_switch_double_left_kitchenroom_kv.last_changed.isoformat(),
    states.switch.aq_switch_double_right_kitchenroom_kv.last_changed.isoformat()] %}
    {{ values|max }}
    device_class: timestamp