Count items in filter

I use this statement in a rule:

JalousienOG?.members.filter( x | x.state != 100 && x.name != "Jalousie_OG_Buero_Loggia").forEach[ item | item.sendCommand(100) ]
Now I want to count, how many items were really changed.
But I don’t get it to insert a counter in the code.

Any hints for me?

Currently i see two methods, but i am not sure if they are working

1.) Implement a counter

var Integer i = 0 JalousienOG?.members.filter( x | x.state != 100 && x.name != "Jalousie_OG_Buero_Loggia").forEach[ item | item.sendCommand(100) i = i+1 ]

at the end you should habe in i the numer of items

2.) use the size option

var Integer i = 0 i =JalousienOG?.members.filter( x | x.state != 100 && x.name != "Jalousie_OG_Buero_Loggia").size ]

the same as before i should now held the number of items

Thomas

1 Like

I may be wrong, but I think, vars which are defined outside the lambda aren’t allowed. But of course you could use an item to count up (ore use the latter term).

@Udo_Hartmann
Uhh, I think you are right. Then the second option should work hopefully

Thomas

Thank you! This works great.
For example:

rule "H_Beschattung_tmp" when Time cron "0 * * ? * *" then var o = JalousienOG?.members.filter( x | x.state != 80 && x.name != "Jalousie_OG_Buero_Loggia") var i = o.size LogInfo.postUpdate(i.toString + " Objekte.") end