I have a group called Lights i use in a rule. How do i find out witch item in that group that activates the rule and the new state of that item.
rule "Alle lys send push"
when
Item Lights received update
then
executeCommandLine("D:\\openhab2\\playsoundwait3.exe D:\\openhab2\\conf\\sounds\\warn.wav")
executeCommandLine("D:\\openhab2\\pushnote.exe \"itemname\" \"newstate\" \"kodi\"")
end
val light = Lights.members.sortBy[lastUpdate].last
OH 2 documentation is being worked and we would welcome the help. And for most things like this the OH 1 Wiki is a good source for documentation as well.
rule "Alle lys send push"
when
Item Lights received update
then
val light = Lights.members.sortBy[lastUpdate].last
logInfo("Test", light)
end
2016-11-10 22:14:03.170 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Alle lys send push': An error occured during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null
2016-11-10 22:14:03.914 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Alle lys send push': An error occured during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null
rule "Alle lys send push"
when
Item Lights received update
then
Lights.members.forEach[ currItem |
val itemName = currItem.name
if ((Lights.get(itemName) == null))
{
logInfo("hashInit", "item: " + itemName + ", value: true" )
Lights.put(itemName, true)
}
]
end
2016-11-10 22:22:09.446 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Alle lys send push': The name '<XFeatureCallImplCustom>.get(<XFeatureCallImplCustom>)' cannot be resolved to an item or type.
2016-11-10 22:22:10.334 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Alle lys send push': The name '<XFeatureCallImplCustom>.get(<XFeatureCallImplCustom>)' cannot be resolved to an item or type.
You must configure persistence on all the members of Lights and all the members of Lights must have been persisted at least once for this to work. And since this won’t work with a periodic update you must persist on everyUpdate or everyChange. That means after you configured persistence you must exercise each of the lights so they all have a persisted value.
The two errors are not the same. Lets deal with the second one first.
A Group is not the same thing as a HashMap. You can’t get and put on a Group and you cannot trigger a Rule on a HashMap.
And you cannot have a variable with the same name as a Group or an Item.
So what is Lights?
What are you trying to do in this second rule? It makes no sense as written.
I’m not sure what Item’s toString returns. Add some logging to see what is happening.
logInfo("Test", "Lights has " + Lights.members.size + " members")
logInfo("Test", "Lights has " + Lights.members.filter[l|l.lastUpdate != null].size + " members with a lastUpdate")
val GenericItem light = Lights.members.sortBy[lastUpdate].last
logInfo("Test", "The most recently updated Light is " + light.name)
When you run the above the number of members should equal the number with a last update.