Hi, want to count how much Items items have the state ON in my Group “gNet” and made this rule:
rule "gNetChangeTest"
when Member of gNet changed or
Item Dummy4 changed to ON
then
// Map/Reduce
val str = gNet.members.filter[ i | i.state == ON ].map[ name ].reduce[ s, name | s + ", " + name ]
logInfo("gNetTest", "Ausgabe: " + str)
//val str1 = gNet.members.filter[ i | i.state == ON ].map[ state as Number ].reduce[ sum, v | sum + v ]
//logInfo("gNetTest", "Ausgabe: " + str1)
//val str2 = gNet.members.reduce[ sum, v | sum + v ]
//logInfo("gNetTest", "Ausgabe: " + str2)
end
The first command works and shows me the names of the items in the logger. the other two shows only error in the logger, where i expected a number.
2018-12-20 12:47:52.686 [INFO ] [ipse.smarthome.model.script.gNetTest] - Ausgabe: Printer_WF2760, iPhone_Peter, smartphone_Claudia, fritz_w7390, fritz_r310
2018-12-20 12:47:52.698 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'gNetChangeTest': Could not invoke method: org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_plus(java.lang.Object,java.lang.String) on instance: null
This will work. I’ve tested it. I use this one to monitor the sum and the overall status
Group:Number:SUM gnuDoorSensor_Glasbruch_Status "Glasbruchmelder Status sum [(%d)]"
Group:Switch:OR(ON,OFF) gswDoorSensor_Glasbruch_Status_All "Glasbruchmelder Status alle [(%d)]"
gnuDoorSensor_Glasbruch_Status gives me the number of triggered items
gswDoorSensor_Glasbruch_Status_All gives me the global status ON or OFF
thx, Guys. @waspie and @HomeAutomation. Works as you described. It’s not quite the solution, i want to to have, as i want to fiddle about DP of @rlkoshak with GroupMembers.
As Scott and HomeAutomation, indicate, the appropriate solution is to use the aggregation function.
But as a learning exercise…
Failing using the aggregation functions, the next best solution would be:
val str1 = gNet.members.filter[ i | i.state == ON ].size
To use a map/reduce approach the problem is that ON isn’t a Number so when you try to cast it to a Number you get null and you can’t do math with null.
Try:
val str1 = gNet.members.filter[ i | i.state == ON ].map[ 1 ].reduce[ sum, v | sum + v ]
Obviously this is not a good solution as there are better ways to get a count of the number of devices that are ON, but it shows how map/reduce works I guess.
The third line fails for sort of the same reasons. When you don’t use the map, v ends up being of type SwitchItem and you can’t do math with a SwitchItem either.
Thx, that’s it . But as learning exercise …
I just want to learn more about GroupMembers a.s.o. But i have no idea where to find such parameters/methods like .size or your second hint. I will try it. The first one work like charm.
Cheers Peter
thx again. will test it with VS code.
I tested the other way you showed me above. Works fine. I’m a Dummy, sorry. Now i understand what meant with state as Number. 1=ON/0=OFF.
I will try to use VS more in the future. At the moment i use the normal “notepad” mostly.