Filtering switches out of group members in a rule

Hello all,

I have a group

group_house_doorwindow

in this group there are switch and contacts items grouped

Switch  Z_way_switch_DoorWindowSensor14                 "Balkontür [MAP(windowdoorcontact_switch.map):%s]"                              
Contact Z_way_contact_DoorWindowSensor14                "Balkontür [%s]"                                  

in a rule I am filtering according to the state “Off” or “On” this is fine and I get all switches whichare off or on. When I want to count the sum of all switches in this group. How can I filter correctly?

I tried

var Integer local_integer_countsum = group_house_doorwindow.members.filter[Switch m|true].size

But it was not correct.

Which rules language? This looks like Rules DSL. I’m pretty sure this approach won’t work for Rules DSL. You’ll need to test for instanceof instead of relying on the type not matching the filter argument. Rules DSL has a poor attempt at a type system and I’m pretty sure that it simply ignores the type in this case. It’s probably the primary reason I recommend against any new development of rules in Rules DSL.

Anyway

...filter[m | m instanceof Switch]...

DSL rule

but this is not working

var Integer local_integer_countsum = group_house_doorwindow.members.filter[Generic m | m instanceof Switch].size

Don’t give m a type. You really should almost never force the type of any variable in Rules DSL. It causes way more problems than it solves. In this case, trying to force the type to GenericItem (see below) can work against you becuase Rules DSL doesn’t usually look down the type hierarchy, only up. So it would never see that m is a SwitchItem.

I do see a problem with my version though. All the classes for Items end in “Item” so it should be

...filter[m | m instanceof SwitchItem]...