[SOLVED] Cast error during iteration

I like to iterate through the members of a group
Here is my rulebut I got an cast error

rule "totalpower" when 
    Time cron "0/10 * * ? * * *"
then
    var Number sum = 0;
    for(NumberItem n : group_house_totalenergy.members as List<NumberItem>){
        sum = sum + (n.state as DecimalType)
    }
    logInfo("Sum is ", sum)
end
Error during the execution of rule 'totalpower': Could not cast [Z_way_number_WallPlug2_totalpower (Type=NumberItem, State=0.01, Label=Wallplug2, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug3_totalpower (Type=NumberItem, State=0.02, Label=Wallplug3, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug4_totalpower (Type=NumberItem, State=0.33, Label=Wallplug4, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug5_totalpower (Type=NumberItem, State=0, Label=Wallplug5, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug6_totalpower (Type=NumberItem, State=0.22, Label=Wallplug6, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug7_totalpower (Type=NumberItem, State=0, Label=Wallplug7, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug8_totalpower (Type=NumberItem, State=4.0100004, Label=Wallplug8, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug9_totalpower (Type=NumberItem, State=0, Label=Wallplug9, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug10_totalpower (Type=NumberItem, State=0.29, Label=Wallplug10, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug11_totalpower (Type=NumberItem, State=0, Label=Wallplug11, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug12_totalpower (Type=NumberItem, State=0, Label=Wallplug12, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug13_totalpower (Type=NumberItem, State=29.87, Label=Wallplug13, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug20_totalpower (Type=NumberItem, State=0.4, Label=Wallplug20, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug21_totalpower (Type=NumberItem, State=0.05, Label=Wallplug21, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug22_totalpower (Type=NumberItem, State=1.52, Label=Wallplug22, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug23_totalpower (Type=NumberItem, State=0.99, Label=Wallplug23, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug24_totalpower (Type=NumberItem, State=4.2600004, Label=Wallplug24, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug25_totalpower (Type=NumberItem, State=0.03, Label=Wallplug25, Category=null, Groups=[group_house_totalenergy]), Z_way_number_WallPlug26_totalpower (Type=NumberItem, State=1.08, Label=Wallplug26, Category=null, Groups=[group_house_totalenergy])] to void; line 47, column 24, length 51

what’s wrong??

Probably because either one or more of the members is not a NumberItem or one or more of the Item’s states are NULL or UNDEF which cannot be cast to DecimalType.

Why not use the stream functions? The work better in almost all cases. See Design Pattern: Working with Groups in Rules. In particular you want filter, map, and reduce.

val sum = group_house_totalenergy.members.filter[ n | n.state instanceof Number ].map[state as Number].reduce[ sum, n | sum = sum + n ]

Another approach would be to use the SUM-function on the group item, and get rid of the rule altogether:

Sorry ther is an error for your solution

Error during the execution of rule 'totalpower': Couldn't invoke 'assignValueTo' for feature param sum

@pacive No it is not possible because i want to also use later historic values out of the database

Try that, var instead of val

var sum = group_house_totalenergy.members.filter[ n | n.state instanceof Number ].map[state as Number].reduce[ sum, n | sum = sum + n ]

You can add the group item to persistence, and get historic values from there. How do you get historic values from your rule? From what I can see the only thing it does is write to the log file.

The val should be fine. I think the problem is I’m reusing sum in the reduce. @milo, try replacing the reduce part with

reduce[ s, n | s = s + n ]

Yes, I thought about that later, and then I forgot.
I changed val to var because that’s normally the cause of this error

I will try it later first I want to reduce my other errors in my rules…

Thanks for your hel very appreciated :slight_smile:

Took me some grinding teeth to get this going, should be:

reduce[ s, n | s + n ]

Thanks anyway, in the end, it solved my problem (finding out whether all color lights are off)

val totalBright = gColorSZ.members.map[(state as HSBType).getBrightness()].reduce[ sum, n | sum + n ]

Left out the filter, as all members are ColorItems

But are all Items guaranteed to never be NULL or UNDEF when the rule runs? That’s what the filter is for, not to deal with different types of Items.

I’m not sure why you had to remove the s =, I have that is several places and it works fine.

Mine said:

java.lang.IllegalArgumentException: Couldn't invoke 'assignValueTo' for feature param sum

Thanks for the info on the filter, in this case, the group actually always gets a command before I need to find out whether something turned off the lights. It’s a color animation, which needs to stop running when lights are turned off.