jms3000
(Martin)
February 10, 2022, 1:03pm
1
The following code gives me a list of datetimes when my automations have been triggered:
{{
states.automation|map(attribute="attributes")|map(attribute="last_triggered")|list
}}
But if I append “|sort”, it throws an error. ‘mappingproxy object’ has no attribute ‘last_triggered’
Why is this?
123
(Taras)
February 10, 2022, 1:18pm
2
You probably have one or more automations whose state is unavailable
(they don’t have a last_triggered
attribute).
Try this:
{{ states.automation
| selectattr('state', 'ne', 'unavailable')
| map(attribute='attributes.last_triggered')
| sort }}
You can make it more robust by including this before the map
filter:
| selectattr('attributes.last_triggered', 'ne', None)
jms3000
(Martin)
February 10, 2022, 1:48pm
3
Oh, thank you, the solution was so easy!
But why does it complain that there is no attribute “last_triggered”? If the list is already build and I append a sort, it should only sort the list.
123
(Taras)
February 10, 2022, 1:54pm
4
It’s a ‘mappingproxy object’
petro
(Petro)
February 10, 2022, 1:55pm
5
What Taras fixed, but omitted in his explanation is that last_triggered is an attribute, but you’re treating it like a property on the state object (which is a mappingproxy object).
You had (incorrect):
Taras had (correct)
You can read up on state objects here:
Ok, so this is where things get funky. You should familiarize yourself with home assistant state objects. This is what home assistant passes around when doing pretty much anything. These state objects contain 8 or so properties. Do not get these properties confused with the devices attributes.
These properties are:
Field
Description
state.state
String representation of the current state of the entity. Example off.
state.entity_id
Entity ID. Format: .<object_id>. Example: light.…
2 Likes
123
(Taras)
February 10, 2022, 2:01pm
6
Here’s what happens if I eliminate the second line of the template and it encounters the unavailable
automations on my test system:
jms3000
(Martin)
February 10, 2022, 3:32pm
7
Thank you.
Maybe you can change
So whenever you use this function:
state_attr(entity_id, ‘attribute’)
into
So whenever you use this function:
state_attr(entity_id, ‘<attributename>’)
in this article.