[SOLVED] Group Members count if ON

  • Platform information:
    • Hardware: _ Raspberry Pi 3 Model B Rev 1.2_
  • OS: Raspbian GNU/Linux 8 (jessie)
    • Java Runtime Environment: (Zulu Embedded (build 1.8.0_152-b76)
    • openHAB version: openHAB 2.4.0 Release Build
    • Bindings:Astro, AVM, EXEC, MQTT, Network, NTP, Samsung, Sonos, Weather (binding-weather1), OpenWeatherMap, MapDB-Persitence, http
    • Transformations: exec, Javascript, JSONPATH, Map, RegEx, Scale

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

// 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)

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

any hints for me ?

Skip all of that.

put them all in the same group “gNet”
then item definition for “gNet”

Group:Number:SUM			gNet

It works for numbers and, as I understand it, will work for switches. I have not tested it but I see others report it works.

1 Like

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

1 Like

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.

Cheers, Peter

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.


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 :+1:. But as learning exercise … :wink:
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

The best way is to use VSCode. Then when you type

val sr1 = gNet.members.filter[ i | i.state == ON ].

it will pop up a list of all the correct ways to complete that statement (i.e. show you all the data and methods you can call).

1 Like

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.

Wish you a merry x-mas, and a happy new year.

Cheers Peter