This is a bit odd. It was working earlier today, but now I’m seeing errors:
Script execution of rule with UID 'solar-5' failed: The name 'energystatus' cannot be resolved to an item or type; line 62, column 36, length 12 in solar
It doesn’t like this rule. There are no global variables set for this.
rule "Convert Inverter Power to the Grid to Positive"
when
Item Solar_HouseGrid_Power changed or
Item Solar_ACPower changed
then
// If its a negative value, we are feeding to the grid so update the item and change negative to positive for display purposes
if(Solar_HouseGrid_Power.state < 0){
var Number energystatus = (Solar_HouseGrid_Power.state as Number).floatValue * -1
Energy_Status.postUpdate(energystatus/1000)
} else {
if(Solar_HouseGrid_Power.state > 0)
Energy_Status.postUpdate(energystatus/1000)
}
end
Maybe move this:
var Number energystatus = (Solar_HouseGrid_Power.state as Number).floatValue * -1
To outside of the if loop because if the Solar_House_Power state is greater than zero then the var is not defined because it is only defined if the state is less than zero?
rule "Convert Inverter Power to the Grid to Positive"
when
Item Solar_HouseGrid_Power changed or
Item Solar_ACPower changed
then
val Number energy = if(Solar_HouseGrid_Power.state instanceof Number) Math::abs((Solar_HouseGrid_Power.state as Number).floatValue / 1000) else 0
Energy_Status.postUpdate(energy)
end
All in all it’s a one-liner.
First step is, to ensure that energy will be a number, no matter if Solar_HouseGrid_Power.state is a number or not (this part is missing in your rule). Easiest way is to use a ternary operator.
Second step is to calculate the positive value, that is done via Math::abs()
As 0 is always 0, there is no need to do further calculation if Solar_HouseGrid_Power.state is not of type Number.
Unfortunately what has happened now, is its losing its value of being positive or negative, as I need that value to determine if its going to or from the grid.
So what’s the goal of the rule if not getting rid of the sign?
If it’s only to multiply with -1, the rule would be even less complex:
rule "Convert Inverter Power to the Grid to Positive"
when
Item Solar_HouseGrid_Power changed or
Item Solar_ACPower changed
then
val Number energy = if(Solar_HouseGrid_Power.state instanceof Number) ((Solar_HouseGrid_Power.state as Number).floatValue / -1000) else 0
Energy_Status.postUpdate(energy)
end
The goal of the rule is to report a value to Energy Status without the UoM, convert from watts to kilowatts, negative if we are taking energy from the grid and positive if we are sending energy to the grid.
You cannot post kilowatts to your target Number:Energy type Item. What is it you want to represent here? Watts are a unit of instantaneous power. Energy would be power integrated over time, typically Watt-hours.
Thanks rossko, but the final statement, where it says 0, I don’t want that. I want to keep the negative number because it’s the value being positive or negative that determines if we feed to the grid, or draw from it.
Have a think about what the rule does, have a think about what you want, then edit it to suit.
The “0 kW” gets posted when the input is non-numeric, not a number, neither positive nor negative. Like NULL or UNDEF because data has not come yet or an error has occurred somewhere else.
If that’s never going to happen, don’t bother checking.
If it might, then decide if you want your output to be zero or not to do any output at all in the circumstances - then the target Item will remain stuck at its last value instead.
its doing what I want more or less, when its feeding in its showing negative (in an ideal world, it would be nice to have no negative value) but its more important that the MainUI cell be the right color with the right text (feed in/from grid) - that bit is working
Allows the wife to determine if we can wash, dishwash etc when theres excess power
That’s a UI widget thing then, not a rules issue. You want the pos/neg value to arrive at your widget, so that you can use it to determine colour, but then take the abs value in the widget for display.