Dynamic entities in group or light group

I’ve spent days on this, searching both the web and this forum for answers. If anyone could point me in the right direction it would mean a lot to me!

I am trying to create “dynamic” groups, meaning that lights or other entities are added automatically to groups based on certain filters. I do this because adding new entities/devices manually every time I acquire a new smart device to my household seems inefficient (to put it mildly).

For this first example I am trying to create an “inside” light group, hence why I am filtering out the outside lights as you will see below. The code runs perfectly fine in the Developer Template tool which makes it even more strange. I am guessing it has something to do with what kind of values are accepted after entities. I’ve read the documentation but can’t see why this won’t work. Am I going about this the wrong way?

This is what I currently have in my lights configuration file, lights.yaml:

  - platform: group
    name: Home Lights
    entities: >
      {{ states.light | map(attribute='entity_id') | reject('search', '.*outside*.') | join(',') }}

I’ve also tried the following in my groups configuration file, groups.yaml, with the same error:

    name: Home Lights
    entities: >
      {{ states.light | map(attribute='entity_id') | reject('search', '.*outside*.') | join(',') }}

The error I get is

String does not match the pattern of "^(?!.+__)(?!_)[\da-z_]+(?<!_)\.(?!_)[\da-z_]+(?<!_)\s?(?:,\s?(?!.+__)(?!_)[\da-z_]+(?<!_)\.(?!_)[\da-z_]+(?<!_))*$".

Please don’t hesitate to ask me if further information is required!

You can’t template in groups. You have to write an automation that sets the groups entities when homeasssistant start is triggered.

1 Like

Basically, take that template and feed it to the group set service call and use homeasssistant start as your trigger. You don’t even need to create the group beforehand in yaml.

1 Like

Thank you so much for your quick help! Did not know this. I thought I had read most that is to read about templating but obviously I missed something. Oh well, thanks for pointing it out! :slightly_smiling_face:

Sounds like a great work-around, thank you! Does it have to trigger on home assistant starting up? It would be nice if it could trigger more often, perhaps on a set time interval? I will definitely look into this.

Thank you again for great help!

Perhaps there is a way of triggering the service every time a new device is added to the system? That would be really neat.

You can have it trigger based on whatever you want. You just need to add a second trigger

There is no “new entity” event, so you have to get inventive

That’s too bad! I am sure I will think of something. :slight_smile:

Great! Thank you again for all the great help! Hopefully this might help other beginners too.

This is a topic I’ve bookmarked to create an auto-group on start (or with time pattern as you mentioned):

1 Like

Great tip! Will definitely look into it, thank you!