Help by sensor template

hello,

i had a template that shows me the count of devices with low battery

- platform: template
    sensors:
      devices_with_low_battery:
        friendly_name: 'Geräte mit schwacher Batterie'
        unit_of_measurement: devices
        value_template: >-
          {{ states.sensor
            | selectattr('attributes.device_class', 'eq', 'battery')
            | map(attribute='state')
            | reject('in', ['unknown', 'unavailable'])
            | map('int', -1) | select('le', 10)
            |select('ge', 0)
            | list | count
          }}
        icon_template: >-
          {% if is_state('sensor.devices_with_low_battery', '0') %}
            mdi:check-circle
          {% else %}
            mdi:battery-alert
          {% endif %}      

is it now possible to show me indeed of the count of devices the name and the icon?

i want to make a popup where he shows me the names and icons of the devices with low battery?

Thank You

Use a conditional card, conditioned on the battery state.

can you give me a little example?

There I can only use state is or state is not ?

I must use battery level is under for example 20%

I think I must have been thinking of a custom card. I’ll see if I can find it.

This battery card allows filtering by state.

Alternatively you could use a loop in your value template like


            {% for state in states.sensor  
            |selectattr('attributes.device_class', '==', 'battery')
            |selectattr('entity_id', 'search', 'battery_level') -%}
            {% if states(state.entity_id) |int(100) <= 20 %}
              {{ state.attributes.friendly_name |replace(' Battery Level', ' ') }}({{ states(state.entity_id) }} %)
            {% endif %}
            {%- endfor -%}

first i look at your idea, when i use your code i get

Heizung Keller Battery(10 %)
            
              Heizung SchlafZimmer Battery(0 %)

as result, is it possible to call the entries separatly in this sensor? i want to make a custom:button-card for the entries in the sensor.

You asked for a list of battery sensors, now you want one entity per custom card, is this right, or am I misunderstanding you?

Hi

yes sorry my english is not the best. I thought I could then pull individual values ​​from the list. but that’s probably not how it works.

I would like it so that I would have individual entities that meet the criteria (battery status below 20%) so that I can then display them in a popup with custom:button:cards.

so yes exactly what you said.

@pedolsky

Have you perhaps a idea ?

It’s probably easier to maintain a group of low battery sensors with an automation. Keeping your current sensor, add the following automation:

- alias: Update Low Battery Group
  trigger:
  - platform: state
    entity_id: sensor.devices_with_low_battery
  action:
  - service: group.set
    data:
      object_id: devices_with_low_battery
      name: Devices with Low Battery
      entities: >
        {% set ns = namespace(ret=[]) %}
        {% for s in states.sensor 
                    | selectattr('attributes.device_class', 'eq', 'battery') 
                    if s.state | is_number and 0 <= s.state | int <= 10 %}
           {% set ns.ret = ns.ret + [ s.entity_id ] %}
        {% endfor %}
        {{ ns.ret }}

Then you’ll have a group named group.devices_with_low_battery with the listed entities that you can do whatever you want with.

1 Like

okay that work i think

entity_id:
  - sensor.heizung_keller_battery_level
  - sensor.heizung_schlafzimmer_battery_level
order: 3
auto: true
friendly_name: Devices with Low Battery

can you please say me how i can request the entities in the group in the lovelance?

The choice is yours at this point. Personally, I’d use auto entities which is a custom card.

Perhaps you misunderstand me, I mean how can I request in the lovelance in a custom:button:card for example the first entry in the group ?

well, you can use auto entities to just list out your button cards with a configuration. So you feed auto-entities the button card configuration, and you filter it based on the group and then it will do the work for you so you don’t have to call out the first entry in the group. It’ll just link to all entiries in the group dynamically.

But, in order to get the first entry, you’d have to use a JS template

entity: |
  [[[
     return entity.attributes.entity_id[0];
  ]]]

okay, but i dont understand the exact usage of the card with my group, can you perhaps give me a example?

i installed the card and testet it, but i dont understand the usage for my problem.

i want to use my own card, code below, i think this dont work

type: custom:button-card
entity: null
size: 40px
show_state: true
styles:
  card:
    - background-color: rgba(190, 190, 190, 0.2)
    - height: 100px
  grid:
    - grid-template-areas: '"i" "n" "s"'
    - grid-template-columns: 1fr
    - grid-template-rows: 1fr min-content min-content
  img_cell:
    - align-self: center
    - text-align: center
  name:
    - justify-self: center
    - padding-left: 10px
    - font-weight: bold
    - font-size: 13px
    - text-transform: uppercase
  state:
    - justify-self: center
    - padding-left: 0px
    - font-weight: light
    - font-size: 12px
state:
  - value: 20
    operator: <=
    color: red

The following pic shows my custom:button-card named ‘Hauptgeräte’. It’s feeded by an automatic created dynamic group which is based on a template (see Developer Tools → Services → service: group.set).

A small badge displays the number of devices that are on. When I klick on the card, I can see all lights and media devices that are on.


i set a group over a automation with group.set.

but what i dont understand how to feed the custom:button-card with the dynamic group?

i have more than one device in the group, can you post your custom:button-card code that i see how to feed the card with the entitys in the group?

this is in my group

entity_id:
  - sensor.heizung_keller_battery_level
  - sensor.heizung_schlafzimmer_battery_level
order: 3
auto: true
friendly_name: Devices with Low Battery

with the auto entities its working see the picture,

popup

but there is the problem i only can choose a card with entities, but i want to use the

custom:button:card

you use the grid card as the card for the auto entities, and you provide the card configuration to options. I don’t have an example handy, sorry. There are other ways to do it as well.

something like

type: custom:auto-entities
card:
  type: grid
card_param: cards
filter:
  include:
    - group: group.devices_with_low_battery
      options:
        type: custom:button-card
        size: 40px
        show_state: true
        styles:
          card:
            - background-color: rgba(190, 190, 190, 0.2)
            - height: 100px
          grid:
            - grid-template-areas: '"i" "n" "s"'
            - grid-template-columns: 1fr
            - grid-template-rows: 1fr min-content min-content
          img_cell:
            - align-self: center
            - text-align: center
          name:
            - justify-self: center
            - padding-left: 10px
            - font-weight: bold
            - font-size: 13px
            - text-transform: uppercase
          state:
            - justify-self: center
            - padding-left: 0px
            - font-weight: light
            - font-size: 12px
        state:
          - value: 20
            operator: <=
            color: red
2 Likes

Nice, Petro!

pimp, let’s not misunderstand each other: I only use 1 card to display the group. But Petro’s code works very well also without auto-entities:


type: custom:button-card
entity: group.auto_gruppe_lampen_mediaplayer
triggers_update: all
name: angeschaltete Geräte
show_icon: false
custom_fields:
  batt:
    card:
      type: custom:button-card
      entity: |
        [[[ return entity.attributes.entity_id[0]; ]]]
      styles:
        card:
          - box-shadow: none
      size: 28px
styles:
  grid:
    - grid-template-areas: '"n" "batt"'

Just change the index number.

1 Like