I’d like an overview of all sensors that have a attribute “battery_level”. And show the sensor name with the value of the battery_level in a list.
With this I get alle my sensors is a list with name and state.
{% set output = namespace(sensors=[]) %}
{% for state in states.sensor %}
{{ state.name }} - {{ state.state }}
{% endfor %}
with this I get a error
“UndefinedError: ‘homeassistant.util.read_only_dict.ReadOnlyDict object’ has no attribute 'battery_level”
But the attribute ‘battery_level’ exist.
{% set output = namespace(sensors=[]) %}
{% for state in states.sensor | selectattr('attributes.battery_level') %}
{{ state.name }} - {{ state.state }}
{% endfor %}
you are listing all sensors, and calling for all of them the attribute battery. It seems like if there is one sensor without that attribute, it throws an error. You should check before, for each sensor in the list, if the attribute battery exists.
Hi,
If it’s a list of battery states you are after then I recommend to look at the “auto-entities” HACS Lovelace card.
It’s magic for this kinds of thing.
Then I think thats the problem. Not all sensors have that attribute. But what i want is just select/filter all the sensors that have that attribute “battery_level”
To make or test the results of the code is to go in HA to: Developer Tools, Template.
I test your code and its OK. So I think that your sensors are different.
Have your sensors a attributes “battery_level” ?