Group switch rule stuck in sendCommand loop

I’m trying to setup a group switch to turn on/off all my lights in one room using an item and corresponding rule.

For some reason once the switch is activated either off or on, the events.log shows that the lights get stuck in a loop continually sending the same on/off command.

Also is there some clear documentation of the available commands for openhab rules? I’ve been searching everywhere for explanations on to use gGroup.members.forEach(…) and what the ‘?’ does when used with gGroup.members.forEach does? I’ve seen some example code on this forum and on the github wiki with and without the ‘?’

home.items:
Group Living_Room “Living Room” (All)
Group Living_Room_Lights “Lights” (Living_Room)
Switch Living_Room_Lights_All “Living Room Lights” (Living_Room_Lights)

home.rules
rule "Living Room Group Light Switch"
when
Item Living_Room_Lights_All received update
then
if(Living_Room_Lights_All.state == ON)
Living_Room_Lights?.members.forEach(item | sendCommand(item, ON))
else
Living_Room_Lights?.members.forEach(item | sendCommand(item, OFF))
end

events.log:
2016-08-13 10:39:36 - Light_GF_Living_Room_Table received command ON
2016-08-13 10:39:36 - Light_GF_Living_Room_Floor received command ON
2016-08-13 10:39:36 - Living_Room_Lights_All received command ON
2016-08-13 10:39:36 - Light_GF_Living_Room_Table received command ON
2016-08-13 10:39:36 - Light_GF_Living_Room_Floor received command ON
2016-08-13 10:39:36 - Living_Room_Lights_All received command ON
2016-08-13 10:39:36 - Light_GF_Living_Room_Table received command ON
2016-08-13 10:39:36 - Light_GF_Living_Room_Floor received command ON
2016-08-13 10:39:36 - Light_GF_Living_Room_Table received command ON
2016-08-13 10:39:36 - Living_Room_Lights_All received command ON
2016-08-13 10:39:36 - Light_GF_Living_Room_Floor received command ON
2016-08-13 10:39:36 - Living_Room_Lights_All received command ON
2016-08-13 10:39:37 - Light_GF_Living_Room_Table received command ON
2016-08-13 10:39:37 - Light_GF_Living_Room_Floor received command ON
2016-08-13 10:39:37 - Light_GF_Living_Room_Table received command ON
2016-08-13 10:39:37 - Living_Room_Lights_All received command ON
2016-08-13 10:39:37 - Light_GF_Living_Room_Floor received command ON
2016-08-13 10:39:37 - Living_Room_Lights_All received command ON
2016-08-13 10:39:37 - Light_GF_Living_Room_Table received command ON
2016-08-13 10:39:37 - Light_GF_Living_Room_Floor received command ON
2016-08-13 10:39:37 - Light_GF_Living_Room_Table received command ON
2016-08-13 10:39:37 - Living_Room_Lights_All received command ON
2016-08-13 10:39:37 - Light_GF_Living_Room_Floor received command ON
2016-08-13 10:39:42 - Living_Room_Lights_All received command OFF
2016-08-13 10:39:53 - Light_GF_Living_Room_Floor received command OFF
2016-08-13 10:39:54 - Light_GF_Living_Room_Table received command OFF

This is the expected behavoir, because the rule triggers when receiving an update to the switch, which belongs to the group. Better don’t add the switch Living_Room_Lights_All to the group.
Second, change the rule trigger to

Item Living_Room_Lights_All changed

or at least to

Item Living_Room_Lights_All received command

That seems to have helped partially. Thank you Udo.

Is there a way to add a delay to each of the commands in the foreach loop?

Living_Room_Lights?.members.forEach(item | sendCommand(item, ON))

And can you confirm what the ‘?’ does in the forEach loop? Is there documentation of these commands in openHAB? I can’t seem to find anything.

Living_Room_Lights?.members.forEach(item | 
    sendCommand(item, ON)
    thread::Sleep(1000) //wait 1000 milli seconds until continuing
)

And yes, I can confirm, that <groupname>?.members.forEach() is the correct term.

rules are written in a DSL based on xtend.

Thank you again for your help Udo.

That sort of worked. However when I used you suggested code verbatim I kept getting an error with the thread::sleep command as follows:

2016-08-14 23:26:30.649 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Living Room Group Light Switch': The name 'Sleep(<XNumberLiteralImpl>)' cannot be resolved to an item or type.

Any ideas why this might have been happening?

For now I’ve fixed it with the following code block:

rule "Living Room Group Light Switch"
        when
                Item Living_Room_Lights_All changed
        then
                if(Living_Room_Lights_All.state == ON) {
                Living_Room_Lights?.members.forEach[item |
                        sendCommand(item, ON)
                        try { Thread::sleep(5000) } catch(InterruptedException e) { }
                ]
        } else {
                        Living_Room_Lights?.members.forEach[item |
                        sendCommand(item, OFF)
                                try { Thread::sleep(5000) } catch(InterruptedException e) { }
                        ]
        }
end

Sorry, misspelled Thread::sleep() :slight_smile: upper/lower case is awful…

Thanks again Udo.

So removing Thread::sleep(5000 from that try {} block should be fine?

that try {} would suppress a warning about unhandled exception, so try {} catch() is correct