Receive last-changed as displayed natively, not as %H:%M (or similar)

Hi everybody,

Green is my custom sensor that I’d like to display in the same style as red, which is generated natively by Home Assistant.

So RED is simply secondary_info: last-changed.

GREEN is


 name: >
          {{ states("sensor.unten_arbeitszimmer_esp32_temperatur") }}°C {% set
          temp = now() -
          states.sensor.unten_arbeitszimmer_esp32_temperatur.last_changed %} {{
          ((temp | string).split(".")[0]).split(":")[0:2] | join(":") }}

Please note, these two are based on different sensors; so it is purely coincidental that green is 0:02 and red is 2 Stunden; green is supposed to be 2 minutes, red is 2 hours.

Thing is, sensor.unten_arbeitszimmer_esp32_temperatur doesn’t have a last-changed attribute that I can just grab; so I have to help myself by utilizing states.sensor.unten_arbeitszimmer_esp32_temperatur.last_changed and then do math on it

The format just doesn’t look that nice. If it’s four minutes, I’d like “4 minutes”, not “0:04”; if it’s two hours 20 minutes, it’s totally fine to round it down to “2 hours”, as Home Assistant does natively in most cards with last-changed.

In this case, I am using type: custom:paper-buttons-row for the esp32_temperatur and type: custom:mini-climate for the card that states “2 Stunden”.

Thanks for your ideas :slight_smile:

There are lots of time based jinja macros here GitHub - Petro31/easy-time-jinja: Easy Time calculations for Home Assistant templates

Take a look.

Or GitHub - TheFes/relative-time-plus: Relative Time Macro with additional options

1 Like

Exactly what I was looking for, thank you.