How to display the last_updated field of an entity in the lovelace view

hello,

I have an entity called:
binary_sensor.natgas_sensor_158d000129cc6b

As you can see it has last_changed and last_updated attributes.

states.binary_sensor.natgas_sensor_158d0001002929cc6b.last_updated
states.binary_sensor.natgas_sensor_158d00012929cc6b.last_changed

How to show the date of the last_updated field in the lovelace view?

it depends on how you want it rendered, but one simple way is to use the mushroom title card, which allows you to do something like this:

{{ state_attr(‘binary_sensor.natgas_sensor_158d0001002929cc6b’, ‘last_updated’) }}
{{ state_attr(‘binary_sensor.natgas_sensor_158d00012929cc6b’, ‘last_changed’) }}

type: custom:mushroom-title-card
  {{ states.binary_sensor.natgas_sensor_158d000129cc6b.last_updated }}
  {{ states.binary_sensor.natgas_sensor_158d00012929cc6b.last_changed }}
alignment: center

get the mushroom card here:

EDITED:
spaced on using attr

I have tried the code you recommended and it doesn’t work, I have even added a sensor that received a change a few seconds ago and it doesn’t work:

type: custom:mushroom-title-card
title: |-
  {{ state_attr('binary_sensor.natgas_sensor_158d000129cc6b', 'last_updated') }}
  {{ state_attr('binary_sensor.natgas_sensor_158d000129cc6b', 'last_changed') }}
  {{ state_attr('binary_sensor.motion_sensor_158d000154a571', 'last_updated') }}
  {{ state_attr('binary_sensor.motion_sensor_158d000154a571', 'last_changed') }}
alignment: center

image

shoow i’m sorry. do your original format, just needed a card that accepted it. i had just grabbed a regular attribute sample from my setup. those aren’t under the attribute set (ie, they technically aren’t attributes of the entity).

type: custom:mushroom-title-card
title: |-
  {{ states.binary_sensor.natgas_sensor_158d000129cc6b.last_updated }}
  {{ states.binary_sensor.natgas_sensor_158d00012929cc6b.last_changed }}
alignment: center

sorry about that!

now it has worked perfectly! thank you very much.

I have tried with 2 sensors and this code works for me:

type: custom:mushroom-title-card
title: |-
  {{ states.binary_sensor.natgas_sensor_158d000129cc6b.last_updated }}
  {{ states.binary_sensor.natgas_sensor_158d000129cc6b.last_changed }}
  {{ states.binary_sensor.motion_sensor_158d000154a571.last_updated }}
  {{ states.binary_sensor.motion_sensor_158d000154a571.last_changed }}
subtitle: ''

as shown in the picture:

could you modify the date format so that it does not display
2024-05-15 09:18:01.122661+00:00
and only show
2024-05-15 09:18:01

thanks

sure, try this format:

type: custom:mushroom-title-card
title: |-
  {{ as_timestamp(states.binary_sensor.natgas_sensor_158d000129cc6b.last_updated) | timestamp_custom('%Y-%m-%d %H:%M:%S') }}
  {{ as_timestamp(states.binary_sensor.natgas_sensor_158d000129cc6b.last_changed) | timestamp_custom('%Y-%m-%d %H:%M:%S') }}
  {{ as_timestamp(states.binary_sensor.motion_sensor_158d000154a571.last_updated) | timestamp_custom('%Y-%m-%d %H:%M:%S') }}
  {{ as_timestamp(states.binary_sensor.motion_sensor_158d000154a571.last_changed) | timestamp_custom('%Y-%m-%d %H:%M:%S') }}
subtitle: ''

using the timestamp_custom, you can play around with the output format to do more than truncating it as you please.

It has turned out quite well, thanks.

Why did we have to download the piitaya/lovelace-mushroom? Can’t it be done with one of the default ones that come with Homme assistant?

i’m not positive that there isn’t a built in card that can handle arbitary jinja. this is the one i know that lets you essentally have a free form input box for you to do this.

the entity card allows you to pull entity and attributes, but last_changed and last_updated aren’t actually attributes.

so i don’t know for a fact that none of the default cards can do this, but i don’t know of any that can. someone else might correct me…

the truth is that the result has been very good, I was just asking if there was a default card to do it out of curiosity. the truth is that your solution has been very good.

I have a question with a template that I can’t do, I have a device that has 3 data:
humidity
temperature
battery

They are 3 different entities and each of them has its last_change and last_update. I have managed to show me FALSE or TRUE if it reaches a number of hours without sending me data from any of the 3 at once.

# Temp Ext Cochera #
{{
now() - states.sensor.battery_158d00010c37e3.last_updated >= timedelta (hours=4)
and
now() - states.sensor.humidity_158d00010c37e3.last_updated >= timedelta (hours=4)
and
now() - states.sensor.temperature_158d00010c37e3.last_updated >= timedelta (hours=4)
}}

That code that I have implemented works for me, but I also wanted to add that of the 3 I would show me the most recent of them so that the display is something like:

Kitchen sensor
without battery? True
Last data: 2024.05.05 13:22:56

I don’t know how to make a conditional that returns the most current of all of them… the one that is less than the NOW time.

hi… i’m happy to help with this new question. it is a different question than the original so to be helpful for the community, we tend to like to have 1 question per thread. would you mind marking the post i did with the code as solution and starting a new post with the new question?