Set a none deviceclass for all binary sensors without a device class

Hi,

I have a sensor to gather the binary_sensors of a certain device_class.

{{ states.binary_sensor | selectattr('state','eq','on') | selectattr('attributes.device_class', 'eq', motion') | list | count}}

However this filter fails if the selections has binary sensors without the device_class attribute.
I’m looking for a way to update all these sensors with a none device_class.

Does anyone have any idea on how to do this? I was looking at customization, but this would not allow me to only update sensors without a device class.

Why not just filter for entities with a defined device class? That way you don’t have to remember to alter device classes for any new binary sensors.

{{ states.binary_sensor 
| selectattr('attributes.device_class', 'defined')
| selectattr('attributes.device_class', 'eq', 'motion')
| selectattr('state','eq','on') | list | count }}
2 Likes

Even better. Thank you @Didgeridrew, works like a charm.