Call the secondary information of an entity

how can I call the secondary information of an entity such as this

  - entity: binary_sensor.10004998a5_0
    secondary_info: last-updated

from a template??

I have tried this and it doesn’t work, is there any other method?

  {{states.binary_sensor.10004998a5_0.last_updated}}

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:

{{ states.binary_sensor.a10004998a5_9.last_updated }}

This is another method

{{states('binary_sensor.10004998a5_9','last_updated')}}

@armedad is correct, it’s the fact that the object ID starts with a number. The best thing to do is to edit the object ID.

If that’s not an option, you can use bracket notation instead of dot notation:

{{ states.binary_sensor['10004998a5_9'].last_updated }}

When I tested, the object id numbers didn’t result in an error. Is there a reason to avoid this method?

@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.

1 Like

I agree the name change is the best solution and thanks for the clarification. Always learning something new.

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.

1 Like

Thank you! I saw my error after my post, but the additional context is much appreciated.

right! that worked! the problem was the name!

I liked this solution, it is quite simple

{{ states.binary_sensor['10004998a5_0'].last_updated }}

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…

but to address your specific case here… for this:

states.sensor['battery_158d00020f1b8c'].last_updated

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.

I was trying to do it for one with a simple name:

sensor.battery_158d00020f1b8
binary_sensor.switch_158d00020f1b8c

and then do it for the one that gives me problems:

binary_sensor.10004998a5_9

and I manage to replace this firts part

# 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)
}}

but not this other part

{% 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)