Template to count devices with low battery

Hi guys, this code is bringing back more than I think it should. I need to work out what is showing so I can put in some rejects.

Can anyone tell me how I can change it to list the entities it counts?

{{ states.sensor
            | selectattr('attributes.device_class', 'defined')
            | selectattr('attributes.device_class', 'eq', 'battery')
            | map(attribute='state')
            | reject('in', ['unknown', 'unavailable'])
            | map('int', -1) | select('le', 41)
            | select('ge', 0)
            | list | count
        }}

remote the | count

Unless you want entity_id’s then it would be an overhaul

{% set ns = namespace(items=[]) %}
{% for s in states.sensor
            | map(attribute='entity_id')
            | select('has_value')
            | select('is_state_attr', 'device_class', 'battery')
            %}
  {% if 0 <= states(s) | int <= 41 %}
    {% set ns.items = ns.items + [ s ] %}
  {% endif %}
{% endfor %}
{{ ns.items }}

Thanks that shows me the values - I can see a bunch of zeros oh well will just change the select i guess!

Ideally wanted the IDs to work out which were cuasing the issue but this short term fix worked

{{ states.sensor
            | selectattr('attributes.device_class', 'defined')
            | selectattr('attributes.device_class', 'eq', 'battery')
            | rejectattr('entity_id', 'search', '(iphone|ipad|disc|low)')
            | map(attribute='state')
            | reject('in', ['unknown', 'unavailable', ''])
            | map('int', -1) | select('>', 0)| select('<', 41)
            | select('ge', 0)
            | list 
        }}

see my edit :wink:

1 Like

AWESOME!!! thanks so much :slight_smile: Now I can properly sort it!

eureka!

{{ states.sensor
            | selectattr('attributes.device_class', 'defined')
            | selectattr('attributes.device_class', 'eq', 'battery')
            | selectattr('entity_id', 'search', 'battery')
            | rejectattr('entity_id', 'search', 'ipad|iphone')
            | map(attribute='state')
            | reject('in', ['unknown', 'unavailable'])
            | map('int', -1) | select('le', 41)
            | select('ge', 0)
            | list | count
        }}