Template for showing empty batteries not working

Hey community,

I’m currently working on a dashboard and want to show some chips there for critical states of devices (e.g. windows open/lights on/batteries of devices empty).

This is the frontend coding for it:

- type: custom:mushroom-chips-card
  chips:
  - type: conditional
    conditions:
      - entity: binary_sensor.helfer_leere_akkus
        state: 'on'
    chip:
      type: entity
      entity: sensor.helfer_anzahl_leerer_akkus
      icon_color: red
      tap_action:
          action: fire-dom-event
          browser_mod:
            service: browser_mod.popup
            data:
              title: Akkus austauschen/laden
              content:
                type: custom:auto-entities
                filter:
                  include:
                    - domain: sensor
                      attributes:
                        device_class: battery
                      state: "<= 25"
                show_empty: false
                card:
                  type: entities
  alignment: center

The popup is working well, the auto-entities is showing the correct entities. But unfortunately the helper-sensor for showing the number of empty batteries is not working.

This is the template for the helper entity:

{{ states.sensor | selectattr('attributes.device_class', 'eq', 'battery') | selectattr('state', 'lt', '25') | list | count }}

If I remove the count it shows the following list:

[<template TemplateState(<state sensor.nicole_smartphone_battery_level=100; unit_of_measurement=%, device_class=battery, icon=mdi:battery, friendly_name=Nicoles Smartphone @ 2023-11-14T21:45:49.134681+01:00>)>]

That makes absolutly no sense for me, since the state of the device is “100”. Why is it showing up? If I replace the “lt” in the template with “gt”, it’s showing up all devices with full battery, but not this one anymore. How is that working?

Thanks for your advice.

BR

Because states are strings, and "100" is less than "25" in the same way that "apple" is less than "banana".

If you are only interested in the number of low batteries:

{{ states.sensor
   | selectattr('attributes.device_class', 'eq', 'battery')
   | map(attribute='state')
   | map('float', default=0)
   | select('lt', 25)
   | list
   | count }}

It looks like the card is doing the string-to-float conversion for you; but you have to do it yourself in the sensor.

Thanks for that snippet, it’s working :slight_smile:

Yes I know, that the states are strings, but why is it working for all other devices then? They are all strings.

What do you mean? Post an example.

I mean, when I turn around the statement:

{{ states.sensor
  | selectattr('attributes.device_class', 'eq', 'battery')
  | selectattr('state', 'gt', '25')
  | list
  | count
}}

Then it shows all the devices with more than 25% (except that device from the initial post, which is at 100%, but is shown when <= 25).

It’s doing a string comparison still. String comparisons work like digit comparisons when the strings have the same number of digits.

If you had a device at 8% ("8"), it would list that as being greater than "25".

That makes totally sense, thank you :slight_smile:

All the other devices are in the area of 50-80 % (which will be greater then 25 in string comparison). Only that single device has 100 %, which is less than 25 in string comparison, because it’s starting with 1.

1 Like