core: 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
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 27
- Comments: 49 (1 by maintainers)
This can be solved by first checking if the attributes are defined for the entity.
For instance:
Now you won’t get this error anymore
Still hoping to hear of a code update in the works to bring the template editor in line with the back end implementation of the templating language. (or vice-versa)
It would be really useful to know that what we ‘design’ in the template editor was going to function identically in a YAML template.
I thought that was its purpose
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.
It apparently is a waste of time to report this issue, as it is still present in Home Assistant 2023.6.1 Supervisor 2023.06.2 Operating System 10.2 Frontend 20230608.0 - latest
The work arounds are probably obvious to those already familiar with jinja, but for a beginner -trying to get to know the massive area of templating- this template tool is very much needed to work correctly and as easily as possible.
This is sloppy at best, poorly managed at worst-- but why should users have to fix what is clearly a bug or poor design choice by the author of the template module in Developer tools? Does HA have an arbitor who can make this simple judgement? Or is this truly just a wild west of anybody can do anything they want and have it included in Home Assistant? Are reporting bugs in Github a waste of time (as it appears to be)?
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.
Still an issue in 2022.11.1
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.
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.
I refer to (2); that was (is?) an absolutely annoying behaviour in Developer Tools. Tested it again with
which return 0 and with
which returns [].
You are stating the obvious. Try understanding why so many people are saying this isn’t fixed. I’ll say it again, as you already said it: “The use of templates should be consistent everywhere.” Use of templates is NOT consistent everywhere. Leading to obvious confusion when creating a template that sometimes works, sometimes doesn’t, and developer tools is NOT consistent with HA execution. Clear?? Either agree with your own statement or pull it back.
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???
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_classisdefined, 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