I have some templates where I need to extract the last_updated from a templated entity. Any clues?
This is the entity template I want to extract the last_updated from:
light.{{ trigger.entity_id.split(".")[1] }}
But I’ve not been able to figure out how to get the state of this entity; and need to use it in a condition template to check the time between last update and now.
What I want to do is add a condition that if the light was turned on (last_updated) over X minutes ago; so I know I can use the last_update for this but don’t know how to get that from the mashed up light entity above.
So, what you want is the last updated attribute of light.X when binary_sensor.X trips? I don’t think you can nest the brackets of the templates so I don’t think it’s going to be easy.
Nope. I can get to the state with my example; and I can get to real attributes using state_attr. But the two last_X values seem to be somewhere else I can’t figure out a way to access.
Nope, you can’t have the {{ nested inside the {% and it throws an error.
Same reason for the entity setup I use a dict and the join as using light_{{ trigger.entity_id.split(".")[1]}} would also throw an error due to the nesting.
But using a join to get states.light.mudroom.last_changed doesn’t work
It really seems these live somewhere between state and state attributes and some trick must exist to get to them.
{% set domain, object_id = trigger.entity_id.split('.') %}
{{ states[domain][object_id].last_updated }}
None of the methods, i.e is_state(), states(), state_attr() return a states object. The only way to access a states object is via the states machine which typically looks like states.domain.object_id (i.e. states.light.livingroom). This gives you the state object and you will now have access to all properties of the state object. The other way to access states machine objects is states[domain][object_id] or in the example states['light']['livingroom']. You can mix and match too, i.e. states.light['livingroom'] or states['light'].livingroom.