Show the most recent last_change of 3 entities in a lovelace view

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. i am using the mushroom card: GitHub - piitaya/lovelace-mushroom: Build a beautiful Home Assistant dashboard easily

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.

@armedad

this one’s a little more fun :slight_smile:

try this:

{% set recent_sensor = 
{
'binary_sensor.entity1': states.binary_sensor.entity1.last_updated ,
'binary_sensor.entity2': states.binary_sensor.entity2.last_updated ,
'binary_sensor.entity3': states.binary_sensor.entity3.last_updated 
}   | dictsort(false, 'value', reverse=true) | first | first
%}
{{ recent_sensor }}
{{ state_attr(recent_sensor,'friendly_name') }}
Last updated {{ states[recent_sensor].last_updated}}

replace entity1,entity2,entity3 (don’t miss 2 occurances of each)
you didn’t give enough info on how to access the inf you want to print out, so i just picked… let me know if you need help with that part too. but the key thing is that the id of the entity is now in recent_sensor

Thanks the code is good! I have modified it a bit:

# Botón del pánico #
{{
now() - states.sensor.battery_158d00020f1b8c.last_updated >= timedelta (hours=240)
and
now() - states.binary_sensor.switch_158d00020f1b8c.last_updated >= timedelta (hours=240)
}}{% set recent_sensor = 
{
'sensor.battery_158d00020f1b8c': states.sensor.battery_158d00020f1b8c.last_updated ,
'binary_sensor.switch_158d00020f1b8c': states.binary_sensor.switch_158d00020f1b8c.last_updated ,
}   | dictsort(false, 'value', reverse=true) | first | first
%} - Última actualización {{ as_timestamp(states[recent_sensor].last_updated) | timestamp_custom('%Y-%m-%d %H:%M:%S') }}

As a last improvement, next to the time you could put “(4h 32min ago)” i.e. subtract the current time from [recent_sensor].last_updated
to calculate the time since its last update?

something like this?

# Botón del pánico #
{{
now() - states.sensor.battery_158d00020f1b8c.last_updated >= timedelta (hours=240)
and
now() - states.binary_sensor.switch_158d00020f1b8c.last_updated >= timedelta (hours=240)
}}{% set recent_sensor = 
{
'sensor.battery_158d00020f1b8c': states.sensor.battery_158d00020f1b8c.last_updated ,
'binary_sensor.switch_158d00020f1b8c': states.binary_sensor.switch_158d00020f1b8c.last_updated ,
}   | dictsort(false, 'value', reverse=true) | first | first
%}
{% set delta = now() - states[recent_sensor].last_updated %} 
{% set time_temp = today_at('0:0') + delta %}
- Última actualización {{ as_timestamp(states[recent_sensor].last_updated) | timestamp_custom('%Y-%m-%d %H:%M:%S') }}  ({{ time_temp.strftime('%Hh %Mmin') }} ago)

the code is not good, I have modified it a little but it is still not good.

# Botón del pánico # (24h)
{{
now() - states.sensor.battery_158d00020f1b8c.last_updated >= timedelta (hours=24)
and
now() - states.binary_sensor.switch_158d00020f1b8c.last_updated >= timedelta (hours=24)
}}{% set recent_sensor = 
{
'sensor.battery_158d00020f1b8c': states.sensor.battery_158d00020f1b8c.last_updated ,
'binary_sensor.switch_158d00020f1b8c': states.binary_sensor.switch_158d00020f1b8c.last_updated ,
}   | dictsort(false, 'value', reverse=true) | first | first%}{% set delta = now() - states[recent_sensor].last_updated %} {% set time_temp = today_at('0:0') + delta %}- Último dato {{ as_timestamp(states[recent_sensor].last_updated) | timestamp_custom('%Y-%m-%d %H:%M') }} ({{ time_temp.strftime('%Hh') }})

show:

Botón del pánico # (24h)
True - Último dato 2024-05-17 19:06 (21h)

but from today 2024-05-20 17:03 until 2024-05-17 19:06 is not 21h.
It must show 70h.

ah, i made the erroneous presumption that they were in the same day. sorry about that. try this:

# Botón del pánico #
{{
now() - states.sensor.battery_158d00020f1b8c.last_updated >= timedelta (hours=240)
and
now() - states.binary_sensor.switch_158d00020f1b8c.last_updated >= timedelta (hours=240)
}}{% set recent_sensor = 
{
'sensor.battery_158d00020f1b8c': states.sensor.battery_158d00020f1b8c.last_updated ,
'binary_sensor.switch_158d00020f1b8c': states.binary_sensor.switch_158d00020f1b8c.last_updated ,
}   | dictsort(false, 'value', reverse=true) | first | first
%}
{% set delta = now() - states[recent_sensor].last_updated %} 
{% set total_seconds = delta.total_seconds() | int%}
{% set minutes = ((total_seconds / 60) % 60 ) | int %}
{% set hours = (total_seconds / 3600) | int %}
- Última actualización {{ as_timestamp(states[recent_sensor].last_updated) | timestamp_custom('%Y-%m-%d %H:%M:%S') }}  ({{ hours}}h {{minutes}}min ago)

thank you very much for your quick response and your great help. you are a crack! But that code does not show me anything:

{% set entities = ['sensor.battery_158d00020f1b8c', 'binary_sensor.switch_158d00020f1b8c'] %}
{% set last = entities | expand | sort(attribute='last_updated', reverse=True) | first %}
- Última actualización {{ last.last_updated.strftime('%Y-%m-%d %H:%M:%S') }} ({{ relative_time(last.last_updated) }} ago)
1 Like

ah thank you @petro! relative_time()… learning something new every day!

1 Like

Thank you that code is working fine:

# Botón del pánico #
{{
now() - states.sensor.battery_158d00020f1b8c.last_updated >= timedelta (hours=240)
and
now() - states.binary_sensor.switch_158d00020f1b8c.last_updated >= timedelta (hours=240)
}}{% set entities = ['sensor.battery_158d00020f1b8c', 'binary_sensor.switch_158d00020f1b8c'] %}{% set last = entities | expand | sort(attribute='last_updated', reverse=True) | first %} - Última actualización {{ last.last_updated.strftime('%Y-%m-%d %H:%M:%S') }} ({{ relative_time(last.last_updated) }} ago)

show:

Botón del pánico
False - Última actualización 2024-05-17 17:06:45 (3 days ago)

could you define 72h to be displayed instead of 3 days?

is it possible? @petro or @armedad thanks! :slight_smile:

you might need to go back to mine then… again, unless petro can school me again.

# Botón del pánico #
{{
now() - states.sensor.battery_158d00020f1b8c.last_updated >= timedelta (hours=240)
and
now() - states.binary_sensor.switch_158d00020f1b8c.last_updated >= timedelta (hours=240)
}}{% set recent_sensor = 
{
'sensor.battery_158d00020f1b8c': states.sensor.battery_158d00020f1b8c.last_updated ,
'binary_sensor.switch_158d00020f1b8c': states.binary_sensor.switch_158d00020f1b8c.last_updated ,
}   | dictsort(false, 'value', reverse=true) | first | first
%}
{% set delta = now() - states[recent_sensor].last_updated %} 
{% set total_seconds = delta.total_seconds() | int%}
{% set minutes = ((total_seconds / 60) % 60 ) | int %}
{% set hours = (total_seconds / 3600) | int %}
- Última actualización {{ as_timestamp(states[recent_sensor].last_updated) | timestamp_custom('%Y-%m-%d %H:%M:%S') }}  ({{ hours}}h {{minutes}}min ago)

note that the error before that you showed was due to a typo.

original was:

{{ hours}h

should have been

{{ hours}}h

or you can also mix and match use petro’s more compact way to find the last updated mixed with how i pulled the hours and minutes out. but i was too lazy to do that and just fixed the missing } instead.

change

to
{{ (now() - last.last_updated).total_seconds() // 3600 }} hours ago

perfect! The solution is good and fast! I have tested it and this is what I got.

Botón del pánico # (240h)
False - Última actualización 2024-05-17 17:06:45 (124.0h)

Is it possible to specify that the format of the number is in whole numbers without decimals?

from (124.0h) to (124h)

the complete actual code is:

# Botón del pánico # (240h)
{{
now() - states.sensor.battery_158d00020f1b8c.last_updated >= timedelta (hours=240)
and
now() - states.binary_sensor.switch_158d00020f1b8c.last_updated >= timedelta (hours=240)
}}{% set entities = ['sensor.battery_158d00020f1b8c', 'binary_sensor.switch_158d00020f1b8c'] %}{% set last = entities | expand | sort(attribute='last_updated', reverse=True) | first %} - Última actualización {{ last.last_updated.strftime('%Y-%m-%d %H:%M:%S') }} ({{ (now() - last.last_updated).total_seconds() // 3600 }}h)