Dynamic Groups

Has anyone got an elegant solution for dynamically creating groups? It has been raised as a gap in WTH but I’m keen to get something going now.

I imagine each group would appear similar to how a list of input_boolean entities appear. Toggle what you want in the groups and off you go. Not entirely sure how to work out which entities appear for selection, but I’d likely use a manually defined groups.yaml entry for that.

I can probably do it myself via group.set but I’d love to hear if others have solved it before I have a crack.

May I ask you what you want to do with dynamic groups? Can you tell me some use cases?

Lights based off motion are an easy example. When there’s motion during the day, I want all my lights in each room to turn on but at night I only want one light per room to turn on. I currently achieve this by creating a night group (per room) and a day group. I use a variable to define which group should be on and off and update the variable based criteria. Perfect; on movement turn on the group stored in my variable.

That’s lovely for most cases. Given it’s Christmas, I want the Christmas lights (via smart plug) to turn on based on/off the same motion logic but not on the same time. At dusk turn on the lights but only turn on the Christmas lights 30mins after dusk. Do I define another group? Yeah, I do now but I think I’d rather be able to dynamically update the group which is turned on. In this case, 30mins after dusk add the Christmas switch to the group & at midnight remove them.

There’s other examples like notify on movement on selected sensors. I want to dynamically select these in the gui based on rules I can’t predict (which kid is home, which part of the house is the dog in). I do this by defining a collection of booleans and toggling then interrogating.

There’s heaps of ways to solution it, but I think dynamic groups are a good base layer.

I didn’t fiddle with this jet, but the services group.add and group.remove sound Like what you are looking for.

So what happened here - what solution did you use?

I defined more groups, and an input select to toggle between them.

lounge_day_lights: group with all lights (6)
lounge_night_lights: group with only one light.
lounge_lights: input select to toggle between the two groups above.

At 9pm an automation triggers to update the input select to the night group. At sunrise it switches to the day group. A bit clunky but it’s good enough for my use case.

For the motion sensors, I’ve moved it to auto-entities.