so i’m not 100% positive, but i’m suspecting that the code is looking at that entity name as the numeric id.
if you try that format with a binary_sensor that does not start with a number does it work?
if so then, can you try renaming that sensor (go to entities property page and change the name). have it start with a non-number… throw an “a” in front… after you do that, try this:
@LiQuid_cOOled , i think tht works when you’re specifying in that format for states and attributes, but if you want to access the last_updated value, i think you have to use the states.entity_id format instead of the states()/state_attr() method, which gets confused if it sees a number first. in your example, you got “off” the state value of your sensor, not the datetime value of last_updated
@Didgeridrew, thx for the other method. that’s a good point. @javi_fly, if you can rename it and just put a char in front, i think you’ll have fewer headaches… but if not, this alternative works.
The example you showed isn’t returning an error, but it also isn’t returning the value of the last_updated property which should be a datetime object.
Whenever possible you should use the states() and state_attr() functions. But there isn’t currently a similar dedicated function to get other properties of the state object like last_changed, last_updated, and last_reported. For properties without dedicated functions you use what you may see referred to as the “state object method” shown in the other posts.
but I wouldn’t know how to use that notation system in this expression.
in the upper part I put the usual notation that I use, and in the lower part I have tried to do it with the new one, it worked well in the first part but in the second part I have not been able to…
# 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 %} - {{ last.last_updated.strftime('%Y-%m-%d %H:%M:%S') }} ({{ (now() - last.last_updated).total_seconds() // 3600 }}h)
# 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 %} - {{ last.last_updated.strftime('%Y-%m-%d %H:%M:%S') }} ({{ (now() - last.last_updated).total_seconds() // 3600 }}h)
you’re hitting some of the reasons why i believe this is not the better solution. you will continue to hit complications like this where sometimes you need to do this and soemtimes you don’t (and maybe even can’t readily do so). the best thing to do is to edit the name of the entity_id…
is the actual name of this sensor.battery_158d00020f1b8c ? if so, you don’t need to do that for this entity. you can do states.sensor.battery_158d00020f1b8c.last_updated
you only need to do this for entities who’s entity_id starts with a number. don’t do it for those that start with a letter.
in other worse, i don’t see any of them in your posted yaml that needs to be converted.