core
core copied to clipboard
Template error in Template Editor while it works fine as custom template sensor
The problem
I hope this is the right place to report this.
I use this code in a template sensor which works fine in that template sensor. However, when I use this code in the Developer Tools->Template Editor I get an error.
The code is: {% set sensors = states.sensor | selectattr('attributes.device_class', '==', 'battery') | selectattr('attributes.state_class', '==', 'measurement') | rejectattr('state', 'in', ['unavailable', 'unknown']) | list %}
The error is: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'device_class'
What version of Home Assistant Core has the issue?
Core 2022.5.0
What was the last working version of Home Assistant Core?
core-2022.4.6
What type of installation are you running?
Home Assistant Supervised
Integration causing the issue
No response
Link to integration documentation on our website
No response
Diagnostics information
No response
Example YAML snippet
{% set sensors = states.sensor
| selectattr('attributes.device_class', '==', 'battery')
| selectattr('attributes.state_class', '==', 'measurement')
| rejectattr('state', 'in', ['unavailable', 'unknown'])
| list %}
Anything in the logs that might be useful for us?
No response
Additional information
No response
Same problem here:
{{ states.media_player | selectattr('attributes.last_called', 'eq', True) | map(attribute='entity_id') | first }}
Template sensor working fine, returning the entity_id of last called alexa media player.
Developer tools return the error:
UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'last_called'
Also not working in mushroom template card anymore. I know, thats not HA/core! Just for info!
Also not working ... {{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'door') | list | count }}
Same Problem here...
Version | core-2022.5.4 Home Assistant Supervised
Same Issue with my RFS Fire Listings in Template & custom:mushroom-template-card
ADVICE
{% set x = states
| selectattr('attributes.type','search','Fire')
| selectattr('attributes.category','eq','Advice')
| list -%}
{%- set ns = namespace(result=[]) -%}
{%- for s in x -%}
{%- set ns.result = ns.result + [ s.state+'km',s.attributes.status,s.attributes.friendly_name+'\n' ] -%}
{%- endfor -%}
{{ ns.result |list | join(' - ') }}
UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'type'
Same issue here
Home Assistant Core 2022.5.5 Home Assistant Supervisor 2022.05.2 Home Assistant OS 8.0
This can be solved by first checking if the attributes are defined for the entity.
For instance:
{% set sensors = states.sensor
| selectattr('attributes.device_class', 'defined')
| selectattr('attributes.state_class', 'defined')
| selectattr('attributes.device_class', '==', 'battery')
| selectattr('attributes.state_class', '==', 'measurement')
| rejectattr('state', 'in', ['unavailable', 'unknown'])
| list %}
Now you won't get this error anymore
This can be solved by first checking if the attributes are defined for the entity.
For instance:
{% set sensors = states.sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.state_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') | selectattr('attributes.state_class', '==', 'measurement') | rejectattr('state', 'in', ['unavailable', 'unknown']) | list %}Now you won't get this error anymore
Well, it seems much better to do that, and it works!!
However, the behaviour in the template editor and in the custom template sensor should be the same.
I had to remove state_class defined & state_class measurement to show my iphone battery, but other than that it now works for me.
{% set result = namespace(sensors=[]) %}
{% for state in states.sensor '
| selectattr('attributes.device_class', 'defined')
| selectattr('attributes.device_class', '==', 'battery')
| rejectattr('state', 'in', ['unavailable', 'unknown'])
%}
{% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ '%)'] %}
{% endfor %}
BATTERIES
{{result.sensors|join('\n')}}
BATTERIES iPhone Battery Level (100%) Sensor03 Battery Level (58%)
Same issue here running 2022.6
The issue it seems is that you need to check if the attribute actually “is defined” before you try & test it’s value.
The issue it seems is that you need to check if the attribute actually “is defined” before you try & test it’s value.
That is correct. See my code above.
I don't see the bug in this issue report? The given template is iterating over all entities and tries to access the device_class on state objects that don't have a device_class attribute. 🤷
I don't see the bug in this issue report? The given template is iterating over all entities and tries to access the
device_classon state objects that don't have adevice_classattribute. 🤷
We agree that the behaviour may be correct.
The issue, in my opinion, is that the template editor has a different behaviour than the template sensors.
Same problem here:
{{ states.media_player | selectattr('attributes.last_called', 'eq', True) | map(attribute='entity_id') | first }}Template sensor working fine, returning the entity_id of last called alexa media player.
Developer tools return the error:
UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'last_called'
If one of the entities doesn't have the specified attribute, the sensor will works fine (ignoring that entity and filtering the others), but the template editor will fail to render.
In previous versions, both rendered without errors. Anyway, it is good to check if the attribute exists before using it for filtering.
template documentation template source (message by IssueLinks)
Hey there @phracturedblue, @tetienne, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)
There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.
This is still an issue on 2022.8.2.
Why does this frontend error in the dev-tools templating section even exist?
Every time trying to access attributes.* this error is shown and template rendering fails. While template sensors instead are working fine.
Instead of forcing everyone over the world to add a pre-check if e. g. device_class is defined, why not just fix this by removing this unnecessary warning from the frontend?
Or are there plans all templates (template sensors) will fail in future so the pre-check will become mandatory?
Came here because of
{{ states.binary_sensor | selectattr('attributes.device_class','eq','vibration') | selectattr('state','eq','on') | list | length }}
Still an issue in 2022.9.7
Still an issue in 2022.10.3
Still an issue in 2022.11.1
In my case, it gives a fatal error…
Still an issue in version 2022.12.8
Why has this not been fixed? Developer tools is useless to test loops with "selectattr"! UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'device_class' In fact it doesn't work with ANY and ALL attributes, not just 'device_class'
Still an issue in version 2023.1.1.
Still an issue in version 2023.2.5
Looks like neither developer accepts this as their problem, it’s the other guys fault. It seems obvious the template editor by definition should match the behavior of actual templates. So can you please fix this???
Still an issue with current version.
The "Sensors" code in this blueprint works, but not in the template editor.
100% agree with @starman2k01, what works (or fails) in one, should work (or fail) in the other.
I agree that this should be fixed, but you should be able to use this work-around for the time being (at least this is what I am using):
{{ device.attributes.device_class | default('whatever-you-want') }}
Above is just an example, you might need to adjust it depending on what it is you're trying to do.