[Solved] Add last changed time as X hours/minutes ago

Hi,

I like to add a last changed to a custom:mushroom-template card.
It’s for a simple open/closed sensor.

I did found a one-liner that will include the absolute time of the last change.

{{ as_timestamp(states.binary_sensor.front_door.last_changed,
  default=0)|timestamp_custom('%d.%m.%Y %H:%M', default=0) }}

But I’m looking for “x minutes/hours ago”.

As far as I know I need to use this template card the make the battery badge icon work.
If I use a entity-card the badge will not show up.

Here a image what I have now:
image

Hope someone can help me with this.

Bug thanks in advance.

The full code:

type: custom:mushroom-template-card
entity: binary_sensor.sliding_door
primary: Sliding door
secondary: >-
  {{ as_timestamp(states.binary_sensor.front_door.last_changed,
  default=0)|timestamp_custom('%d.%m.%Y %H:%M', default=0) }}
icon: |-
  {% if is_state("binary_sensor.sliding_door", "on") %}
    mdi:door-open
  {% elif is_state('binary_sensor.sliding_door', 'off') %}
    mdi:door-closed
  {% endif %}
badge_icon: |-
  {% if is_state('sensor.contact_sensor_battery_state', 'high') %}
    mdi:battery
  {% elif is_state('sensor.contact_sensor_battery_state', 'medium') %}
    mdi:battery-50
  {% elif is_state('sensor.contact_sensor_battery_state', 'low') %}
    mdi:battery-10
  {% endif %}
badge_color: |-
  {% if is_state('sensor.contact_sensor_battery_state', 'high') %}
    green
  {% elif is_state('sensor.contact_sensor_battery_state', 'medium') %}
    orange
  {% elif is_state('sensor.contact_sensor_battery_state', 'low') %}
    red
  {% endif %}
tap_action:
  action: more-info
hold_action:
  action: none
double_tap_action:
  action: none
icon_color: |-
  {% if is_state('binary_sensor.sliding_door', 'on') %}
    blue
  {% endif %}
1 Like

Here is mine for my letterbox, just replace the variables with yours.

{{ relative_time(state_attr('automation.letterbox','last_triggered')) }} ago
1 Like

Did not work right away, but could make it work.
Thanks for sharing.

1 Like

Would you mind posting your solution or what is wrong with the above, so others can find and use it in the future? :slight_smile:

Thanks a lot! :slight_smile:

Sure,

I think the line Neil_Brownlee
could have worked but for some reason it didn’t.

But his line gave me a idea to try and after some googling I found this and changed it to fit my variables.

{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.front_door.last_changed | default(0)) | int ) | timestamp_custom("%Hh %Mm", false)}}

It looks like this now
image

3 Likes

{{ relative_time(states.input_boolean.pulizia_cucina.last_updated) }}

also this above worked for me.

2 Likes