Get group members not in other group

I’m trying to build a rule for presets in my living room. I have a Switch with 3 options: all off, all on, or mood lighting.
I want the mood preset to turn off all lights in the living room, except the ones that are mood lights, and turn those on if they are off

I defined my items and groups like this (pseudo):

Group li
Group li_mood (li)

Switch li_light1 (li)
Switch li_light2 (li_mood)
Switch li_light3 (li_mood)

Is it possible in the rules to do something like this? It doesn’t seem to work for me:

rule "Living room preset - Mood"
when
    Item li_preset changed to 2
then
    li.allMembers.filter(i | !li_mood.contains(i)).send(OFF)

    li_mood.send(ON)
end

I’d prefer doing the presets this way, because it doesn’t require me to change the rules every time I should add or remove items. I could do it by switching off all the lights first and then turning on the mood lights, but that means I would be in the dark for a little while when going from “on” to “mood”.

switch all Group members on:

li_mood.members.forEach(moodlight|moodlight.sendCommand(ON))

I don’t know if members.forEach would resolve the child groups, but if, I doubt there is an easy way to know, if the item belongs to a child group. If the items are named strictly in a scheme like li_light_n and li_mood_light_n you could do it like this:

val myLights = li.members.filter[lights|!lights.name.contains("mood")]
if (!MyLights.empty)
    MyLights.members.forEach(light|light.sendCommand(OFF))

Code not tested…

That would require a strict naming scheme indeed.
I ended up doing it like this:

val itemsToTurnOff = new GroupItem("temp_itemsToTurnOff")

li.allMembers.forEach(i | itemsToTurnOff.addMember(i))
li_mood.allMembers.forEach(i | itemsToTurnOff.removeMember(i))

itemsToTurnOff.members.forEach(i | i.sendCommand(OFF))
li_mood.send(ON)
1 Like