[SOLVED] Determine which Item of a Group is switched ON

That’s what the filter does, gives you a list of members with state ON. Be aware it may be empty.

I know that, thats why I posted it. But that doesnt really help because the filter is being used with a variable. Im trying to use it for an if statement.

You would use a filter to select only members of the group with state ON, and then send each of those an off command, If you must include the keyword IF, you could always test their state again to be really really sure that the filter coulddo it on it’s own.

Thinks; is it the i’s and j’s here causing trouble?

myGroup.members.filter[i | i.state==ON]

Okay, here we filter any members of the group. We refer to each member as “i”, it’s arbitrary could be “banana”, then test the state of i to see if it is ON. The results go into a list (which might be empty).
We don’t do anything with that list yet.

myGroup.members.filter[i | i.state==ON].forEach[ j |
   j.sendCommand(OFF)
]

So we make the same list, then for-each through the list referring to each list entry as “j”

@binderth adds a nice refinement with a ? , which just suppresses any silliness if the group has no members etc.

myGroup?.members.filter[i | i.state==ON].forEach[ j |
   j.sendCommand(OFF)
]
1 Like

Thanks rossko57, a very informative post. I assume I can then do:

gAllLights?.members.filter[Lights | Lights.state==ON].forEach[ LightsON |
if(LightsON.state==ON){
LightsON.sendCommand(OFF)
]
}

You could, but there’s no need since LightsOn.state == ON for each LightsOn… that’s what the filter did for you.

2 Likes

Thanks Scott, that works well. Much cleaner

1 Like