Sensor to monitor multiple sensors value

Hello Community

I am still configuring everything in Yaml, and no need to comment on that :wink: !

I searched the forums, but can’t find what I am after.

I have a lot of batteries that I am monitoring, and I would like a single sensor that monitors if any of those are under a specific value, so I can mark in my top menu bar with a color, that attention is required. As you can see from the screenshot I already have that in the particular view, I would just like to have that in the menu pane as well, so I dont have to check the view manually periodicly.

I’m sure this can be done, probably by templating, but haven’t done this before.

Any help is appreciated. Thank you !

Regards MichaelJ

Likely there is something more efficient but try this in developer tools > template

{% for x in (states.sensor
  |selectattr('attributes.device_class','eq', 'battery')
  |selectattr('attributes.unit_of_measurement','eq', '%')
  |selectattr('state','<', '4')
) %}
{{x.entity_id}}
{% endfor %}

That does not give the correct result. The battery state is a string, and when comparing against the string “4” what gets compared is only the first character, meaning a battery level of say “100” will be deemed as less than “4”.

Here is my attempt:

{% set threshold = 10 %}
{% set batteries = states.sensor
| selectattr('attributes.device_class', '==', 'battery')
| selectattr('attributes.unit_of_measurement', '==', '%') 
| rejectattr('state', '==', '-1') | list %}
{{ zip(batteries | map('attr', 'entity_id') | list,
batteries | map('attr', 'state') | map('float') | list)
| selectattr(1, '<=', threshold)
| map('first') | list }}
2 Likes

Yep … forgot, was late (for me) :slight_smile:

I use custom battery card in combination with auto entities. Then I have card showing overview of batteries requireing attention and can unfold all remaining ones. Here is the code:

type: custom:auto-entities
sort:
  method: state
  numeric: true
card:
  type: custom:battery-state-card
  title: Battery Levels
  colors:
    steps:
      - "#ff0000"
      - "#ffff00"
      - "#00ff00"
    gradient: true
  collapse:
    - name: Batteries OK
      min: 25
filter:
  include:
    - entity_id: "*battery_level*"

And here screenshots of folded:
Screenshot 2025-01-11 at 12.13.02

and unfolded card:

Hello all

Thank you for your replies. It didn’t match my needs 100%, but sent me in the right direction !

With your answers and Forum #379393 I ended up with:

Configuration.yaml:

      devices_with_low_battery:
        friendly_name: 'Devices with low battery'
        unit_of_measurement: devices
        value_template: >-
          {{ states.sensor
             | selectattr('attributes.device_class', 'eq', 'battery')
             | map(attribute='state')
             | reject('in', ['unknown', 'unavailable', 'Ok'])
             | map('int', -1) | select('le', 20)
             | list | count
          }}

And view.yaml :

  - type: "custom:button-card"
    card_mod:
      style: |
        ha-card {
        margin: 0.1rem 0.1rem;
        -webkit-box-shadow: 0px 3px 6px 3px rgba(0,0,0,0.8);
        -moz-box-shadow: 0px 3px 6px 3px rgba(0,0,0,0.8);
        box-shadow: 0px 3px 6px 3px rgba(0,0,0,0.8);
        border-radius: 10px;
        border: solid 3px rgb(80,120,80);
          --ha-card-background: rgba(0, 00, 0);
        color: rgb(181,142,49);
        }
    icon: mdi:battery-30
    show_label: false
    size: 50%
    color: auto
    tap_action:
      action: call-service
      service: script.menu_skift
      service_data:
        valgtmenu: '/lovelace/batteri'
    styles:
      card:
        - height: 90px
        - width: 100px
      icon:
        - color: >-
            [[[
              if (states["sensor.devices_with_low_battery"].state > 0) return "darkred";
              if (entity.state == "/lovelace/batteri") return "#5294E2";
              else return "gray";   
            ]]]


Thanks for your input. Highly appreciated.

/Michael J