In openHAB 4, there’s also triggeringGroup
which is handy in this situation
rule "Sync switches"
when
Member of SwitchGroup changed
then
triggeringGroup.members
.filter[ item | item.state != newState ]
.forEach[ item | item.sendCommand(newState) ])
end
in JRuby, triggeringGroup is available as event.group
rule "Sync Switches" do
changed SwitchGroup.members
run do |event|
event.group.members.ensure.command(event.state)
end
end
Or in one line:
changed(SwitchGroup.members) { |event| event.group.members.ensure.command(event.state) }
It’s very useful when you have multiple groups, so you can use one rule
rule "Sync Switches" do
changed SwitchGroup1.members
changed HallwayGroup.members
changed LivingRoomGroup.members
run do |event|
event.group.members.ensure.command(event.state)
end
end
Or you can also write it as:
rule "Sync Switches" do
changed SwitchGroup1.members, HallwayGroup.members, LivingRoomGroup.members
run do |event|
event.group.members.ensure.command(event.state)
end
end