Next level 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
123
(Taras)
August 6, 2022, 12:56pm
9
{% 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?
Vex
(Marcin Bielecki)
November 23, 2022, 9:28am
13
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
123
(Taras)
November 23, 2022, 11:59am
14
Vex:
In the template, use states.binary_sensor
instead of states.sensor
.
JohnPlou
(John)
January 13, 2023, 10:34am
15
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.
JohnPlou
(John)
January 13, 2023, 10:22pm
17
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” ?
JohnPlou
(John)
January 14, 2023, 9:41am
19
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
JohnPlou
(John)
January 14, 2023, 10:22am
21
Although they appear in Developer Tools/States, the script don’t work for them…
JohnPlou
(John)
January 14, 2023, 10:24am
22
All of the temperature / humidity sensors
My battery state is not a sensor, but a attribute of a sensor. That’s why I didn’t work for you.
Maybe this is a solution: 🔋 Lovelace: Battery state card
1 Like
JohnPlou
(John)
January 14, 2023, 11:29am
24
Thank you. I’ll try to integrate it.
Vex
(Marcin Bielecki)
January 31, 2024, 12:17pm
25
hi, how to fix this?
{{ states.sensor
| selectattr('attributes.device_class', 'defined')
| selectattr('attributes.device_class', 'eq', 'battery')
| selectattr('state', 'lt', 25) # <- this will not work: TypeError: '<' not supported between instances of 'str' and 'int'
| map(attribute='attributes.friendly_name' )
| list }}
I think that some states are a ‘int’ and some ‘str’
Maybe you can use the auto-entities card .
Hello,
even as this seems to be solved, you can use auto-entities card as @LocoLoco proposed.
On the card, you can use code like this:
type: custom:auto-entities
card:
type: entities
filter:
include:
- entity_id: '*battery_level*'
exclude: []
sort:
method: entity_id
This an easier way for new and unexperienced users and every device with this battery entities will be added automatically.