WTH why is there no Group option for devices with Batteries?

If this was available in the helpers I could remove my final group from the ‘groups.yaml’ file.

Don’t forget to vote for your own WTH.

By the way, what should be the state for a battery group (or any other sensor group) wne all the different entities in the group have distinct states?
How it is today with your existing group?

Hmm, good point. Currently I use the group to iterate through and let me know when any battery levels are below a threshold.

Maybe all I need is the ability to add/edit generic groups through the interface?

Are you doing that with a card in the user interface or in some automation (no UI)?

group.set service. The group doesn’t even need to exist in order for you to make it.

Currently I use it in a monthly email status report that iterates through the battery devices and reports back a list with each % battery remaining.

If they were to be implemented as a new group type I guess the overall state of the group could be defined as Minimum / Maximum / Total / Average ? That way a user could look at the Minimum of the group and if any battery devices are below that minimum, a trigger could be activated?

I’ll check this out. I haven’t played with that service before. I’ve only ever predefined them in my groups.yaml file. I guess I would need to set the group on startup somewhere.

You don’t need a group for that, or if you want you can easily create a dynamic group.

If what you want is a list of battery entities, try something like this:

{{ states.sensor 
| selectattr('attributes.device_class', 'defined')
| selectattr('attributes.device_class', 'eq', 'battery')
| map(attribute='entity_id')
| list
}}

This way is much easier to maintain when you add or remove a new device.

And you can modify to have only the entities with battery level bellow a given threshold (in this example, 30%):

{{ states.sensor 
| selectattr('attributes.device_class', 'defined')
| selectattr('attributes.device_class', 'eq', 'battery')
| selectattr('state', 'lt', '30')
| map(attribute='entity_id')
| list
}}
  • we probably have to test this a bit to make sure it is correct,

And if you still wanna a group, you can use this together the group.set as @petro suggested.

2 Likes

It can be set in an automation, which could be at startup, or once an hour, or maybe just before you need that group (once a month, right?).
But again, you probably don’t need a group… Try to share your automation here and I’m sure someone will be able to help.

These are perfect, thanks @EdwardTFN much appreciated.

Hi, i am very new to this but if i add something like this to my configuration.yaml

binary_sensor:
  - platform: group
    name: "DEntities with low battery"
    device_class: battery
    entities: >
      {{ states.sensor 
      | selectattr('attributes.device_class', 'defined')
      | selectattr('attributes.device_class', 'eq', 'battery')
      | selectattr('state', 'lt', '30')
      | map(attribute='entity_id')
      | list
      }}

I get a validation error Invalid config for [binary_sensor.group]: Entity ID {{ states.sensor | selectattr('attributes.device_class' is an invalid entity ID for dictionary value @ data['entities']. Got "{{ states.sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', 'battery') | selectattr('state', 'lt', '30') | map(attribute='entity_id') | list }}\n". (See ?, line ?).

How can you get such a dynamic group ?
thanks

Please create a new thread since this is not the correct topic for your question. Make sure to include what is it exactly you are trying to do… The configuration you have posted is a mixture of different types and platforms that does not make sense as it is.

Call the service group.set on an automation or a script. Something like this:

service: group.set
data:
  object_id: low_battery
  name: Entities with low battery
  entities: >-
    {{ states.sensor 
    | selectattr('attributes.device_class', 'defined') 
    | selectattr('attributes.device_class', 'eq', 'battery') 
    | selectattr('state', 'lt', '30') 
    | map(attribute='entity_id') 
    | list 
    }}
1 Like

I am trying this to send a lost of low battery levels, but I get the list of all battery devices.
Anything obvious I don’t get yet?

service: telegram_bot.send_message
data:
  message: >
    "{{ states.sensor 
    | selectattr('attributes.device_class', 'defined') 
    | selectattr('attributes.device_class', 'eq', 'battery') 
    | selectattr('state', 'lt', '25') 
    | map(attribute='name') 
    | list 
    }}"

I don’t see anything that justify you getting all the entities with batteries, but you don’t need the " when in a multiline statement.
I mean, this should work:

service: telegram_bot.send_message
data:
  message: >
    - {{ states.sensor 
    | selectattr('attributes.device_class', 'defined') 
    | selectattr('attributes.device_class', 'eq', 'battery') 
    | selectattr('state', 'lt', '25') 
    | map(attribute='name') 
    | list
    | join("
    - ")
    }}

States are strings, | selectattr('state', 'lt', '30') will compare the first 2 characters of your state to 30, if they are lower, it will pass. That means 100 and it’s first 2 characters 10 will show up as less than 30.

The only way to do these tests properly are to convert the state to a number and then do the comparison. The unfortunate part, is that you have to map the value, which means you’ll lose the entity_id association. Therefore you need to use namespace to tabulate the entities.

service: telegram_bot.send_message
data:
  message: >
    {% set ns = namespace(ret=[]) %}
    {% for e in states.sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', 'battery') %}
      {% if e.state | is_number and e.state | float < 25 %}
        {% set ns.ret = ns.ret + [ e.name ] %}
      {% endif %}
    {% endfor %}
    {{ ns.ret }}
2 Likes

thank you, @petro and @EdwardTFN :slight_smile:
I will play around with your suggestions!

I tried the following call service and it didn’t seem to update the empty group helper group.sensor_batteries I created. Thoughts?

service: group.set
data:
  object_id: sensor_batteries
  name: Sensor Batteries
  entities: >-
    {{ states.sensor 
    | selectattr('attributes.device_class', 'defined') 
    | selectattr('attributes.device_class', 'eq', 'battery') 
    | rejectattr('entity_id', 'in', integration_entities('mobile_app'))
    | map(attribute='entity_id') 
    | list 
    }}

Please go to Developer Tools > Templates and paste this:

Do you have an error or a list of entities?

I did exactly that before posting the question and it does in deed list the filtered entities. No error calling the service either which is why I’m not sure why it isn’t updating the group.

1 Like