Cast error during iteration


(M) #1

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??


(Rich Koshak) #2

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 ]

(Anders Alfredsson) #3

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


(M) #4

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


(Vincent Regaud) #5

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 ]

(Anders Alfredsson) #6

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.


(Rich Koshak) #7

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 ]

(Vincent Regaud) #8

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


(M) #9

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

Thanks for your hel very appreciated :slight_smile: