Template error in Template Editor while it works fine as custom template sensor
See original GitHub issueThe 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
Issue Analytics
- State:
- Created a year ago
- Reactions:23
- Comments:24 (1 by maintainers)
Top Results From Across the Web
Is anyone else having problems with templates?
The template error you reported is merely a symptom of the, as yet unidentified, underlying problem.
Read more >Templates and Custom Sensors in Home Assistant - YouTube
Templates and Custom Sensors are a more advanced feature in Home Assistant. With Templates and Custom Sensors you can format and convert ...
Read more >Cisco DNA Center User Guide, Release 2.2.3
Step 1. In the Cisco DNA Center GUI, click the Menu icon ( ) and choose Tools > Template Editor. Step 2. In...
Read more >PRTG Manual: Create Device Template
To successfully create a device template, you must add sensors to the device (either manually or via the auto-discovery) and the sensors must...
Read more >Template not working as a sensor : r/homeassistant
You are using a deprecated way of defining template sensors. Use the new one. What does not work exactly?
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
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.