How to list all sensors with attribute = ‘battery_level’

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 %}

What I am doing wrong?

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.

1 Like

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”

I now the “auto-entities”. But I want learn more about jinj2 and want to use it in a daily briefing/notifications.

{% for state in states.sensor | selectattr('attributes.battery_level', 'defined') %}
{{ state.name }} - {{ state.state }}
{% endfor %}
2 Likes

This works, thx :ok_hand:

1 Like

Next level :see_no_evil: below I get the sensors with the battery_level state.
But how can I filter to get only the one that are below 20%?

{% for state in states.sensor | selectattr('attributes.battery_level','defined') %}
{{ state.name }} = {{ state_attr(state.entity_id, 'battery_level') }}%
{% endfor %}

I have try it with < 20 add the end of selectattr. And | reject(<, '20')
But that does not work.

1 Like
{% for state in states.sensor | selectattr('attributes.battery_level', 'defined') | selectattr('attributes.battery_level', 'lt', 20) %}
{{ state.name }} - {{ state.state }}
{% endfor %}

Tnx alot again. Works perfect. I was close But had the ‘ ‘ wrong.
But what means ‘lt’? Lower than?

Yes.

eq ==
ne !=
lt <
le <=
gt >
ge >=
1 Like

Thank you.

how to just check (true/false)* in a nice way if there is a sensor with attribute battery_level with value lt 20?
* to use in binary_sensor

In the template, use states.binary_sensor instead of states.sensor.

How can I use this script? Can I use it e.g. in an entity on the dashboard?

I have used it in a markdown card. You can also used something like this as a template for a new sensor.

I used it in a markdown card. I entered:

{% for state in states.sensor | selectattr('attributes.battery_level', 'defined') | selectattr('attributes.battery_level', 'gt', 20) %}
{{ state.name }} - {{ state.state }}
{% endfor %}

All of my battery sensors have almost 100% battery, but none appeared in the card.

Do I miss something?

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” ?

Apparently, my Aqara temperature/humidity sensors don’t follow that rule… Have to find what’s going on…

can you take a picture of the sensor under Developer Tool, States