Skip item in group loop

Hey all,
I have a group containing all groundfloor rollershutters (gEG_Rollaeden) in the house. In case we’re NOT at home (alarm_system.state = ON) all rollershutters go down at sundown. This works flawlessly with some rule code.

In case we’re at home, 2 rollershutters should NOT go down, but the rest should. Maintaining 2 rollershutter groups (rolllershuttersAlarmON & rolllershuttersAlarmOFF) seems to be overhead, so does OH2 support something like “continue” in group loops?

gEG_Rollaeden?.members.forEach(rollo|
    if (rollo == eg_esszimmer_rolladen_garten && alarm_system.state == OFF){
        continue
    }
)

Or can I temporarily remove an item from a group? Whats the best practice for such use-case?
Thanks much for your ideas!

What about:

gEG_Rollaeden?.members.forEach[rollo|
    if (rollo != eg_esszimmer_rolladen_garten && alarm_system.state != OFF){
        ... do your stuff here ...
    }
]

Note the negation at the if-statement

PS: I also adjusted the brackets for the foreach statement

1 Like

Hey,
thanks for your suggestion. That might be an option. Only thing I’m thinking about is the if statement might end up with few more conditions like second rollershutter, and another alarm related item. Then I might end up with 5 - 6 conditions in that if-statement which makes it hard to read/understand/troubleshoot.

I will give it a try with your suggested solution. In case someone is aware of a more elegant way to implement this, please let me know.
Thanks much

IMHO I would solve this with subgroups if the items to filter grow, then you can just filter out the members of the specific subgroup. That would be the most elegant solution.

But if you really want to remove dynamically a member from a group, you can use:

gStateON.removeMember(item)

and

gStateON.addMember(item)

And this is why the Group approach actually ends up being less overhead in the long run.

Groups would be more elegant. You will have complexity somewhere. Having one extra Group ends up being the most elegant because your code only has a few extra characters:

gEG_Rollaeden.members.filter[rs | !rs.getGroupNames.contains("gAlarm")].forEach[ rs |
    // do stuff
]

Then oyu can add or remove shutters from the groups dynamically as Benjamin indicates or, at a minimum change which shutters do what by Group membership instead of needing to modify rules.

If you have a separate Group that only lists those shutters that should go down, and the only thing that you need to do is send the same command to all members of the Group then your rule simplifies even further to:

gEG_Rollaeden.sendCommand(DOWN)

Doesn’t get more elegant than that. Two tow three Groups and one simple line of code.

Thanks for your detailed explanation. I’m aware of the
gEG_Rollaeden.sendCommand(DOWN)
feature but that one doesnt work for my Homematic devices as I have to delay each command execution for 400ms. Homematic cant cope with how fast OH executes the command when looping over a group.
And secondly, I have some window contact. In case they are open, I dont want the rollershutters to go down.
In fact I ended up setting a virtual switch triggering another rule, that does the logic.

For the problem here, I went for the second group. You’re right that this will pay off in the long run.

I have to admit that I did not get the point of your rule suggestion:

gEG_Rollaeden.members.filter[rs | !rs.getGroupNames.contains("gAlarm")].forEach[ rs |
    // do stuff
]

What does this do? It filters for items that are in 2 groups (gEG_Rollaeden & gAlarm)?
Thanks much.

You missed the “!”, so the filter is all Elements that are in gEG_Rollaeden but NOT in gAlarm!

Assuming all your roller shutters are a member of gEG_Rollaeden, it filters out those items that are also a member of gAlarm . You would put those items that should not go down into gAlarm.

Thanks much. Really appreciate your help on this topic.
The logic with gAlarm is really smart. I like it a lot and I’m about to implement it.

Are you aware of any limitation when adding groups or members to groups, that this needs some time or OH2 restart?
I added the gAlarm group and added the members via cfg file. Log sais, that item files have been refreshed. But when testing the new group via rest interface, there are no members in the group. Same when checking the item via rest. The newly added group is not listed in the groupNames array.
Only after stopping OH and starting again the group memberships are populated.

swa@nuc:~$ curl http://nuc:8080/rest/items/gAlarm
{"members":[],"link":"http://nuc:8080/rest/items/gAlarm","state":"NULL","type":"Group","name":"gAlarm","label":"Rollaeden","tags":[],"groupNames":["All"]}
swa@nuc:~$ curl http://nuc:8080/rest/items/eg_wz_rolladen
{"link":"http://nuc:8080/rest/items/eg_wz_rolladen","state":"0","stateDescription":{"pattern":"%d %%","readOnly":false,"options":[]},"type":"Rollershutter","name":"eg_wz_rolladen","label":"Rolladen Wohnz.","category":"rollershutter","tags":[],"groupNames":["gEG_Rollaeden"]}
swa@nuc:~$ oh2 stop
swa@nuc:~$ oh2 start
swa@nuc:/etc/openhab2/items$ curl http://nuc:8080/rest/items/gAlarm
{"members":[{"link":"http://nuc:8080/rest/items/eg_wz_rolladen","state":"0","stateDescription":{"pattern":"%d %%","readOnly":false,"options":[]},"type":"Rollershutter","name":"eg_wz_rolladen","label":"Rolladen Wohnz.","category":"rollershutter","tags":[],"groupNames":["gEG_Rollaeden","gAlarm"]},{"link":"http://nuc:8080/rest/items/eg_esszimmer_rolladen_garten","state":"0","stateDescription":{"pattern":"%d %%","readOnly":false,"options":[]},"type":"Rollershutter","name":"eg_esszimmer_rolladen_garten","label":"Rolladen Essz.","category":"rollershutter","tags":[],"groupNames":["gEG_Rollaeden","gAlarm"]}],"link":"http://nuc:8080/rest/items/gAlarm","state":"NULL","type":"Group","name":"gAlarm","label":"Rollaeden","tags":[],"groupNames":["All"]}
swa@nuc:/etc/openhab2/items$ curl http://nuc:8080/rest/items/eg_wz_rolladen
{"link":"http://nuc:8080/rest/items/eg_wz_rolladen","state":"0","stateDescription":{"pattern":"%d %%","readOnly":false,"options":[]},"type":"Rollershutter","name":"eg_wz_rolladen","label":"Rolladen Wohnz.","category":"rollershutter","tags":[],"groupNames":["gEG_Rollaeden","gAlarm"]}

This is a known bug. Sometimes changes in group memberships do not get picked up immediately and require an OH restart.