AldoQ
(Aldo Quispel)
May 18, 2022, 7:34pm
1
I want to count a few things like the number of open doors and open windows. These are binary sensors and the device_class is ‘door’ for example. So I have been trying to count the number of open doors like this:
{{ states.binary_sensor | selectattr('state', 'eq', 'on')
|selectattr('attributes.device_class', 'eq', 'door') | list | count }}
When I try this I get the error
UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object'
has no attribute 'device_class'
Looking at my binary_sensors there are multiple ones that indeed do not have a device_class. I however cannot figure out how to fix the script above to ignore those sensors that do not even have the attribute. Or perhaps how to I can manually give those sensors a device_class. Looking at it, it’s they are binary sensors added by things like Philips Hue. Not sensors I manually added.
pedolsky
(Pedolsky)
May 18, 2022, 7:39pm
2
AldoQ:
{{ states.binary_sensor | selectattr('state', 'eq', 'on')
|selectattr('attributes.device_class', 'eq', 'door') | list | count }}
That’s a bug (hopefully fixed with the next update). As a workaround, copy your code into a Markdown Card.
You can exclude entities without device class this way:
{{ states.binary_sensor
|rejectattr('attributes.device_class', 'defined')
|selectattr('state', 'eq', 'on')
|list
|count }}
„Custom“ device classes (or other attributes) can be manually added in your customize.yaml.
# Example
cover.tradfri_blind:
friendly_name: Rollo
device_class: shade
sensor.hue_bewegungsmelder_flur_battery:
battery_type: CR2032
2 Likes
AldoQ
(Aldo Quispel)
May 18, 2022, 7:52pm
3
Cheers!
I think you ment ‘undefined’?. Updated the door one to the one bellow and the error is now gone and the count seems good.
{{ states.binary_sensor
|rejectattr('attributes.device_class', 'undefined')
|selectattr('attributes.device_class', 'eq', 'door')
|selectattr('state', 'eq', 'on')
|list
|count }}
Also good to know how I can add device classes if I want to update those. I found out of all the binary sensors its three Hue sensors (media areas) and some Sonos sensors (if the mic is on). Ideally Hue and Sonos would send the right info. But now know how to add it if needed.
2 Likes
pedolsky
(Pedolsky)
May 18, 2022, 7:57pm
4
AldoQ:
undefined
Ah, I’ve misunderstood you
1 Like
AldoQ
(Aldo Quispel)
May 18, 2022, 8:00pm
5
No worries. It worked perfectly like this. I had no luck googling it or searching it. So very thankful you posted this solution!
1 Like
e-raser
September 26, 2022, 6:41pm
6
Here’s the related issue - which is still not fixed yet:
opened 05:35PM - 05 May 22 UTC
integration: template
### 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
```yaml
{% 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_