Template for last changed displayed as "x hours/minutes ago"

Hello,
I am working with templates for the first time so forgive me if this seems easy. I usually find the solution by reading the other posts and experimenting with the developer tools but had no luck with this.

I am trying to have the “last changed” information displayed as “x hours/minutes ago” to use it in a mushroom template card.

I found out this template that works for me

{{(as_timestamp(states.binary_sensor.salotto.last_changed) | timestamp_custom('%I:%M %p'))}}

but returns:

12:20 PM

Instead, I need something like this:
image
where “3 ore fa” is the output that I would like to have

I appreciate any help or suggestion, thank you in advance!

1 Like

Your sensor must be a timestamp with the timestamp class

device_class: timestamp

I read about this in another thread but I thought it was something different in my case.
How can I put the device_class in the template string that I wrote above? Is it possible to have everything in one string?
Thank you for the feedback

  - platform: template
    sensors:
      salotto:
        device_class: timestamp
        value_template: "{{ states['binary_sensor.salotto'].last_changed }}"

Developer Tools > States

In a card:

image

Isn’t this done automatically if you specify secondary_info: last-changed in the entity card? Or do you need a different format to that?

I added this to my configuration.yaml. Is this the right way or am I making a mistake? Because I don’t have the entity in the developer tools area after the reboot. I tried sensor: and template: with no luck

sensor:
  - platform: template
    sensors:
      salotto:
        device_class: timestamp
        value_template: "{{ states['binary_sensor.salotto'].last_changed }}"

@Troon I am using the template card because I need to configure the icon and color with a template, that’s why I cannot use secondary_info in this card and was hoping to get the info with a template

This is from mine and it is working

sensor:
  - platform: template
    sensors:
      last_changed:
        device_class: timestamp
        value_template: "{{ states['binary_sensor.bbox3_ping'].last_changed }}"


image

EDIT Anything in your log?

Thanks, the template works now (I was looking for the wrong name in the dev tools, I realized that thanks to the code that you shared), it is now visible in the developer tools.

Still, I have the problem about having the template in my card (it is a different entity but same problem, I added the code in the yaml file as suggested before to this entity too):

type: custom:mushroom-template-card
primary: Bagno
secondary: >-
  {{(as_timestamp(states.sensor.bagno_ping_last_changed.last_changed) |
  timestamp_custom('%I:%M %p'))}}
icon: mdi:toilet
icon_color: |2-
            {% if is_state('binary_sensor.luce_bagno_ping', 'off') %}
              green
            {% else %}
              red
            {% endif %}
layout: vertical
hold_action:
  action: none
double_tap_action:
  action: none
tap_action:
  action: none

That now results in:
image

Is there a way to write a string that has the result that I want so that I can put it in the mushroom card?
Clearly this: {{(as_timestamp(states.sensor.bagno_ping_last_changed.last_changed) | timestamp_custom('%I:%M %p'))}} does not work properly because I have the time as a result and not “x minutes ago” like it was with the original entity

I’m not using mushroom, I can’t help you

But, as an exemple, here is a configuration that works with a glance card and card-mod

type: glance
entities:
  - entity: sensor.last_changed
    icon: mdi:toilet
    name: Bagno
    card_mod:
      style: |
        state-badge {
          background:
          {% if is_state('binary_sensor.bbox3_ping', 'on') %}
            rgba(0,255,0,0.1)
          {% else %}
            rgba(255,0,0,0.1)
          {% endif %};
          color: 
          {% if is_state('binary_sensor.bbox3_ping', 'on') %}
            rgba(0,255,0,1)
          {% else %}
            rgba(255,0,0,1)
          {% endif %};
        }

That gives:

binary sensor is on:
image

binary_sensor is off
image

EDIT: change card_mod to be more like in your exemple

EDIT 2 What if you replace the template with the one I gave earlier?

{{ states['binary_sensor.salotto'].last_changed }}

Thanks, however this card is next to many other mushroom cards and it would be very different to the others resulting in a bad layout. I hope someone that uses mushroom cards can point me in the right direction. I am trying to make it work with a time calculation but I am finding it difficult to edit the templates that I find in other posts to suit my needs. I don’t have enough experience with templates :neutral_face:

If you mean using the string in the mushroom card I get this:
image

Ok
But I don’t understand, if you use the sensor we have build together, the salotto, how does it display out-of-the-box in a mushroom card?

If I use a normal mushroom entity card, the state of the sensor that we created is correctly “x hours ago”.
image

But if I could use a normal entity card, I wouldn’t have a problem because I could just use secondary_info: last-changed with the original entity without needing to create the sensor. The problem is that the mushroom template card requires a template to have the last-changed info in the secondary info spot.

I managed to make it work in the end by editing some code that another forum member shared. Took a while with the dev tools but now it works.

This is the template that works, I leave it here in case anybody else needs it in the future:

{% set sec = as_timestamp(now()) - as_timestamp(states('sensor.bagno_ping_last_changed')) %}
{%set hr = (sec / 3600) | int %}
{%set min = sec / 60 - hr * 60 %}
{% if hr > 1 %}
  {{"%d" % (hr)}} ore fa
{% elif hr > 0 and min > 2 and min <30 %}
  {{"%d" % (hr)}} ora e {{"%2d" % (min)}} minuti fa
{% elif hr > 0 and min > 30 %}
  {{"%d" % (hr)}} ora e mezza fa
{% elif hr > 0 and min > 1 and min <30 %}
  {{"%d" % (hr)}} ora e {{"%2d" % (min)}} minuto fa
{% elif min > 2 %} 
  {{min | int}} minuti fa
{% elif min > 1 %}
  {{min | int}} minuto fa
{% elif sec > 0 %}
  {{sec | int}} secondi fa
{% endif %}

@Olivier1974 Thank you for your patience and for your help, your suggestion about the sensor at the beginning was crucial to make it work!

Computing the text of a timestamp by hand is cumbersome.
It works but the developer of mushroom should consider to correct the behaviour to display timestamp sensors everywhere like in the entity card.

Anyway, I’m glad that I could help somehow.

1 Like

If it’s helpful, this can be achieved with a simpler template like so:
{{ relative_time(states["sensor.example"].last_changed) }} ago