[SOLVED] Lambda rule to count open contact items fails in OH2

Hi community,

I ported my OH 1.8 configuration to OH 2 and most rules just worked the same way.
For a rule that counts the number of open windows, I can’t figure out how to set it up.
Contact items use the knx binding and are all members of group “gWindows”.

The rule is:

rule offeneFenster
when
	Item gWindows received update
then
	val StringBuilder wc = new StringBuilder()
	
	gWindows?.members.forEach[curWindow|
		if((curWindow.state as OpenClosedType).equals(OPEN)) {
			wc.append("1")
		}
	]
	
	val openWindows = wc.length()
	FensterOffen.postUpdate(openWindows)
end

I also tried the filter method instead of counting the stringbuilder, but the results were the same:

	val Number ow = gWindows.members.filter[w | w.state as OpenClosedType != CLOSED].size

To compare the values, I tried ==, !=, .equals, comparing the string after using w.state.toString() - nothing works.
I have a similar rule for lights which runs on Switch items and worked without any change in OH2.

Does anyone have an idea how I can get the rule to work?

Thanks
Daniel

You shouldn’t have to cast the state to OpenClosedType.

By “nothing works” do you mean you get the wrong answer or an error in the logs?

I would write it as follows:

val ow = gWindows.members.filter[w | w.state == OPEN].size

Okay, I could swear I have tried this option before. I didn’t get any errors but until now the result always was zero.

I tried your piece of code and now it works - as expected.

Thanks a lot!