Val Number - cant be resolved to an item or type

Hi All,

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


Am I doing something wrong?!

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?

1 Like

That looks to have done the trick Greg! :slight_smile: Thank you

I recommend another rule:

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.

1 Like

Looks cleaner! Thanks Udo, Ill give this a try

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.

What are the two Item types here? As you know, this is important info when dealing with units.

These are the items:

Group:Number:SUM gSolar_ACPower              "Overall Solar AC Generated [%.2f W]"
Group:Number:SUM gSolar_EnergyDay            "Overall Energy Generated for the Day kWh [%.2f kWh]"
Number:Energy Solar_ACPower                  "Overall AC Power kWh [%.2f kWh]"
Number:Energy Solar_FeedIn                   "Overall AC Feedin kWh"
Number:Energy Energy_Used                    "Real Time Energy Consumption [%.2f kW]"
Number:Energy Energy_Status                  "Real Time Grid Status [%.2f kW]"


// Garage Inverter//
Number:Energy Solar_GarageAC_Power     "AC Power" (gSolar_ACPower)                     { channel="fronius:powerinverter:809abad4df:d5e0145e76:inverterdatachannelpac" }
Number:Power Solar_GarageDay_Energy    "Energy Generated - Day" (gSolar_EnergyDay)     { channel="fronius:powerinverter:809abad4df:d5e0145e76:inverterdatachanneldayenergy" }
Number:Power Solar_GarageTotal_Energy  "Energy Generated - Total"                      { channel="fronius:powerinverter:809abad4df:d5e0145e76:inverterdatachanneltotal" }
Number:Power Solar_GarageYear_Energy   "Energy Generated - Year"                       { channel="fronius:powerinverter:809abad4df:d5e0145e76:inverterdatachannelyear" }
Number Solar_GarageErrorCode           "Inverter Error Code"                           { channel="fronius:powerinverter:809abad4df:d5e0145e76:inverterdatadevicestatuserrorcode" }
Number Solar_GarageStatusCode          "Inverter Status Code [MAP(inverter.map):%s]"   { channel="fronius:powerinverter:809abad4df:d5e0145e76:inverterdatadevicestatusstatuscode" }
Number Solar_GarageGrid_Power          "Power from/to Grid"                            { channel="fronius:powerinverter:809abad4df:d5e0145e76:powerflowchannelpgrid" }
Number Solar_GarageLoad_Power          "Power from Inverter"                           { channel="fronius:powerinverter:809abad4df:d5e0145e76:powerflowchannelpload" }



// House Inverter //
Number:Energy Solar_HouseAC_Power      "AC Power" (gSolar_ACPower)                     { channel="fronius:powerinverter:172a3b9554:e4b3052ae0:inverterdatachannelpac" }
Number:Power Solar_HouseDay_Energy     "Energy Generated - Day" (gSolar_EnergyDay)     { channel="fronius:powerinverter:172a3b9554:e4b3052ae0:inverterdatachanneldayenergy" }
Number:Power Solar_HouseTotal_Energy   "Energy Generated - Total"                      { channel="fronius:powerinverter:172a3b9554:e4b3052ae0:inverterdatachanneltotal" }
Number Solar_HouseErrorCode            "Inverter Error Code"                           { channel="fronius:powerinverter:172a3b9554:e4b3052ae0:inverterdatadevicestatuserrorcode" }
Number Solar_HouseStatusCode           "Inverter Status Code [MAP(inverter.map):%s]"   { channel="fronius:powerinverter:172a3b9554:e4b3052ae0:inverterdatadevicestatusstatuscode" }
Number Solar_HouseGrid_Power           "Power from/to Grid"                            { channel="fronius:powerinverter:172a3b9554:e4b3052ae0:powerflowchannelpgrid" }
Number Solar_HouseLoad_Power           "Power from Inverter"                           { channel="fronius:powerinverter:172a3b9554:e4b3052ae0:powerflowchannelpload" }

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.

I want to represent in kilowatts, if we are either importing or exporting to the grid.

I’m not sure what you suggesting I do/change?

Right, instantaneous power. Use a Number:Power type.

when
    Item Some_Number_Item changed
then
    if (Some_Number_Item.state instanceof Number) {
        Some_Power_Item.postUpdate( ((Some_Number_Item.state as Number)  /  1000).toString + " kW")
    } else {
        Some_Power_Item.postUpdate( "0 kW")
    }

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.

You can see here I use this widget to do that

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.

Thanks rossko, Ok shall do.

It might be easier to populate two items but then displaying that in the widget isnt going to be easy I guess.

Does Solar_HouseGrid_Power go both positive and negative? Then so should your Number:Power Item.
Show us some real values that you don’t like.

Hi Rossko, it does

The values are fine :slight_smile:

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.

1 Like

Thats exactly right!