Are there any special trigger for groups?
When yes where would this be documented or implemented so we may look it up?
When not, why are there no special triggers?
Trigger i know off:
when
// will trigger when group switch is pressed by user or
// rule .sendCommand()
Item <item> received command
// will trigger when a member is updated,
// pressed by user or
// changed from system as group switch was pressed or
// last/first group member changed state or
// group switch is pressed by user or
// rule .sendCommand()
Item <item> received update
// will trigger when group switch is pressed or
// last group member changed state
// rule .sendCommand()
Item <item> changed
Use cases:
- Trigger rule when member received a command
Example: To find out which member off a group was triggered it needs persestence and then the last changed member can be found.
But if the member was changed by pressing the group switch this will execute the rule for each member but each time the lastUpdated member will be the same.
To overcome this one additional rule which triggers only if the group switch was pressed can be added. But then the Single member rules will fire additionally.
The solution would be a trigger which only fires when a member received a command.
.items
Group:Switch:OR(OFF, ON) group
Switch member_1 (group)
Switch member_2 (group)
Switch member_3 (group)
Switch member_4 (group)
.sitemap
sitemap GroupRulesTest label="Test Group Rules"
{
Frame label="Member"
{
Switch item=member_1 label="Member 1"
Switch item=member_2 label="Member 2"
Switch item=member_3 label="Member 3"
Switch item=member_4 label="Member 4"
Switch item=group label="Group"
}
}
.rules
rule "Member pressed"
when
Item group received update
then
// give persistence time to catch up
Thread::sleep(500)
val member = group.members.filter[s|s.lastUpdate("mapdb") !==null].sortBy[lastUpdate("mapdb")].last as SwitchItem
val name = member.name.toString
logInfo("GroupTest", "Member "+ name +" to " + member.state )
end
rule "Group pressed"
when
// using "received command" to avoid trigger
// when gPlugs is Updated when the last Switch is pressed
// only trigger when switch is actually changed by user
Item group received command
then
Thread::sleep(200)
logInfo("GroupTest", "Group changed to " + group.state )
end
log when group is pressed:
[INFO ] [smarthome.event.ItemCommandEvent ] - Item 'group' received command ON
[INFO ] [smarthome.event.ItemCommandEvent ] - Item 'member_1' received command ON
[INFO ] [smarthome.event.ItemCommandEvent ] - Item 'member_2' received command ON
[INFO ] [smarthome.event.ItemCommandEvent ] - Item 'member_3' received command ON
[INFO ] [ipse.smarthome.model.script.GroupTest] - Group changed to ON
[INFO ] [smarthome.event.ItemCommandEvent ] - Item 'member_4' received command ON
[INFO ] [smarthome.event.ItemStateChangedEvent] - member_1 changed from OFF to ON
[INFO ] [smarthome.event.ItemStateChangedEvent] - member_2 changed from OFF to ON
[INFO ] [smarthome.event.ItemStateChangedEvent] - member_3 changed from OFF to ON
[INFO ] [smarthome.event.ItemStateChangedEvent] - member_4 changed from OFF to ON
[INFO ] [home.event.GroupItemStateChangedEvent] - group changed from OFF to ON through member_4
[INFO ] [ipse.smarthome.model.script.GroupTest] - Member member_4 to ON
[INFO ] [ipse.smarthome.model.script.GroupTest] - Member member_4 to ON
[INFO ] [ipse.smarthome.model.script.GroupTest] - Member member_4 to ON
[INFO ] [ipse.smarthome.model.script.GroupTest] - Member member_4 to ON
[INFO ] [ipse.smarthome.model.script.GroupTest] - Member member_4 to ON
I thought maybe removing the “Thread:sleep” could be used to ensure that the single rule gets fires for each member thus reducing the rules to one. But unfortunatly this will not produce riliable behavior. Sometimes persistence is too fast so only one member gets posted or some other mixed stated of things.
[INFO ] [ipse.smarthome.model.script.GroupTest] - Member member_1 to OFF
[INFO ] [ipse.smarthome.model.script.GroupTest] - Member member_1 to OFF
[INFO ] [ipse.smarthome.model.script.GroupTest] - Member member_4 to OFF
[INFO ] [ipse.smarthome.model.script.GroupTest] - Member member_4 to OFF
What would be the best way to gets this working properly?
Best case would be one rule, and a way to get the member which triggered this rule. But unfortunatly there is nothing like this.
Second choice two rules, avoid single rule to trigger when group was pressed.
Any suggestions are welcome?