Create a group with either dynamic or with static members

Create Group Blueprint

This blueprint creates a group with either static or dynamic members.

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.


Screen Shot 2021-11-29 at 1.01.52 PM

It has two main functions to add members

  • Dynamically include multiple members based on a common string in their object ID’s. (Dynamic Include List)
  • Or statically add individual members. (Static Entities To Add)

Then the group can be refined by

  • Dynamically excluding multiple members based on a common string in their object ID. (Dynamic Exclude List)
  • Or statically removing individual members. (Static Entities To Remove)

Group Types

Simple (Static) Group

  • If you would like to create a group and manage its members manually, you only need to use the Static Entities To Add field to select entities.
    • If you add or remove devices from home assistant, the group will need to be updated manually.

Dynamic Group

  • If you would like members to be automatically added or removed from the group, use the Dynamic Domain List, Dynamic Include List, and Dynamic Exclude List fields and enter identifying strings.
    • You can still add or remove members statically by using the Static Entities To Add or Static Entities To Remove fields.

Recommended Steps for Creating a Dynamic Group

First familarize yourself with domain and object_id state objects.

Start with a broad brush.

  • Add a domain in Dynamic Domain List. Example: - light
  • Add a string in Dynamic Include List. Example: - bedroom
  • Save the automation and run it.
  • View the group created, (Configuration → Entities → in the search bar enter ‘group’, click the group) and its members.
  • Make note of the members that need to be removed and strings they have in common.
  • If necessary, edit the object IDs of the members and add identifiers to make them easier to exclude.

Then narrow the scope.

  • Go back to the automation and in Dynamic Exclude List add a list of common strings to exclude.
  • Save and run the automation then go back to the group to view updated list members.

Rince and repeat.

  • Continue this process until only the members you want are in the group.

Order of Operation

This blueprint follows a specific order of operation when adding members to a group.

  1. If a domain is entered in Dynamic Domain, all entities in those domain are added to an internal list. Otherwise all entities are added to the list if Dynamic Include List or Dynamic Exclude List are used.
  2. If an include string is entered in Dynamic Include List, only entities with object ID’s containing that string remain in the list.
  3. If an exclude string is entered in Dynamic Exclude List, all entities with that string are removed from the list.
  4. Then static members are added or removed from the list
    a. If specific entities are selected in Static Entities To Add, those entities will be added to the list.
    b. If specific entities are selected in Static Entities To Remove, those entities will be removed from the list.

Field Types

Dynamic Domain List, Dynamic Include List, and Dynamic Exclude List
Strings entered in these fields need to be entered as a yaml list. For example:

- light

or for multiple strings

- light
- lock

Static Entities To Add, and Static Entities To Remove
Multiple entities can be added to these fields. If devices are added, expand the device and remove unnecessary entities.

Notes

  • The only way to see what entities are added to the group is to actually view the group members.
  • Recommend opening two home assistant windows, one with the automation (Configuration → Automations) and another with the entities list (Configuration → Entities) filtered for ‘group’.
  • Dynamic Domain List, Dynamic Include List, and Dynamic Exclude List fields are lists and need to be entered with a hyphen, then a space, then the string. Example: - light
  • Automations created with this blueprint are ran at home assistant start and when groups are reloaded (Configuration → Server Controls → Click Groups, Group Entities… ). Groups will be recreated at this time also.
    • If you would like to update groups while home assistant is running, all you have to do is run the automation.
  • To delete a group, delete the automation that created it and restart home assistant or reload the groups.

Credits

Portions of this blueprint are from the following repositories or posts

4 Likes

I like the idea here, but your code is poorly optimized. It would be a resource hog. There’s loops and loops and loops. With jinja, you want to take advantage of the filters because they use generators to handle the heavy load. Filters like select, reject, selectattr, rejectattr, and unique.

This replicates your code but it executes extremely fast, even with 1000s of entities and it achieves the same result.

{%- set entities = states | selectattr('domain','in', domains) | list if domains else states | list %}
{%- set ns = namespace(include=[], exclude=[]) %}
{%- for item in includes %}
  {%- set ns.include = ns.include + entities | selectattr('object_id', 'search', item) | map(attribute='entity_id') | list %}
{%- endfor %}
{%- for item in excludes %}
  {%- set ns.exclude = ns.exclude + entities | selectattr('object_id', 'search', item) | map(attribute='entity_id') | list %}
{%- endfor %}
{%- set entities = entities | map(attribute='entity_id') | select('in', ns.include) | reject('in', ns.exclude) | list %}
{%- set entities = (entities + add) | reject('in', rem) %}
{{ entities | unique | sort }}
1 Like

Thank you! That is much more efficient! Still learning filters and their order of operation. Updated the GIST with your code and a few changes.

Just verify it works for all your use cases, I tried it a bunch and it appeared to do what yours did. Either way, its much faster and it should be the same.

EDIT: speaking of which, found some errors. This should fix em.

        {%- set entities = states | selectattr('domain','in', domains) | list if domains else states | list %}      
        {%- set ns = namespace(include=[], exclude=[]) %}
        {%- for item in includes %}
          {%- set ns.include = ns.include + entities | selectattr('object_id', 'search', item) | map(attribute='entity_id') | list %}
        {%- endfor %}
        {%- for item in excludes %}
          {%- set ns.exclude = ns.exclude + entities | selectattr('object_id', 'search', item) | map(attribute='entity_id') | list %}
        {%- endfor %}
        {%- set entities = entities | map(attribute='entity_id') %}
        {%- set entities = entities | select('in', ns.include) if ns.include else entities %}
        {%- set entities = entities | reject('in', ns.exclude) if ns.exclude else entities %}
        {%- set entities = (entities | list + add) | reject('in', rem) %}
        {{ entities | unique | sort }}

Here’s what I came up with earlier. Only minor changes from what you posted before. Tested it pretty thoroughly and it appeared to work. Did I miss something?

{%- set entities = states | selectattr('domain','in', domains) | list if domains else states | list %}
{%- set ns = namespace(include=[], exclude=[]) %}
{%- for item in includes %}
  {%- set ns.include = ns.include + entities | selectattr('object_id', 'search', item) | list %}
{%- endfor %}
{%- for item in excludes %}
  {%- set ns.exclude = ns.exclude + entities | selectattr('object_id', 'search', item) | list %}
{%- endfor %}
{%- set entities = entities | select('in', ns.include if includes else entities ) | reject('in', ns.exclude) | map(attribute="entity_id") | list %}
{%- set entities = (entities + add | list ) | reject('in', rem) %}
{{ entities | unique | sort }}

If domain is listed with nothing else it fails, that’s at least what I fixed in the one I just posted

I just tested the blueprint with only two static entities and it added absolutely all of the entities in home assistant to the group. Something’s not working.

Thanks for letting me know. I’ll have it fixed shortly.

1 Like

Need to wrap if statement around includes and excludes sets, I’ll post tomorrow morning if ya don’t post anything

1 Like

Made changes and thoroughly tested. I believe it’s fixed but please let me know if you find anything.

1 Like

It works great now! thank you so much for taking the time to fix it.

@mwolter Do you think this would also enable me to create a group from en existing sensor that contains multiple sensors called out as entities.

This is the entity (example)

And if I use (for instance) this template below I get the list of entities.

It would be great if your blue print would allow me to create a real ‘group’ from this and keep it in ‘sync’ with the fake group sensor powercalc generates.

This only works with old school groups as the new school groups do not have the ability to adjust their members.

Short answer: Not possible.

Is it possible to also work with friendly names instead of object IDs?

I have tons of Zigbee devices and I normally do not care their object ID, instead I simply rename them to what they should be used for instead (as any normal user would also do). But instead I use a specific naming concept in my friendly names that would be very beneficial to have such dynamic groupings.

Example:
‘Bathroom Window1 on_off’ = binary_sensor.lumi_lumi_sensor_magnet_aq2_80e6df01_on_off
‘Bathroom Window2 on_off’ = binary_sensor.lumi_lumi_sensor_magnet_aq2_80e9df04_on_off
‘Livingroom Window1 on_off’ = binary_sensor.lumi_lumi_sensor_magnet_aq2_81e9da01_on_off
‘Livingroom Window2 on_off’ = binary_sensor.lumi_lumi_sensor_magnet_aq2_86e96f01_on_off

… which are not unique in general.

No objection in this but still, as in my setup they are not, i´d love this option. So would it be possible?

Just rename your entity_ids

OT then here, but:

Let´s leave it like this

It takes less than 5 seconds to do a find in files/replace with a text editor. Notepad++, VSCode, or the vscode addon.

I would like to set the ‘all_entities’ option. Is there any way to do this?