Creating Solar rules for energy consumption/production

Hi All,

Ive created some rules which so far give me the production (from solar) , consumption (from grid) and therefore I can work out the overall consumption. This works fine however, the inverter channels display positive or negative values for when its feeding in to the grid, or drawing down.

Is there a way I can just reference the actual number, without the - or + ?

Or a way to populate an item based off those states, ie if value is -, then TO the grid (feeding in) so populate Item:Number Feed_In

and if the value is 0 or more, populate Item:Number Grid_In

My rules so far really just changing things from watts to kilowatts and dont remove the negative value, despite saying so


rule "Convert Inverter Power to the Grid to Positive"
when
        Item Solar_HouseGrid_Power changed
then
        val Number fromgrid = (Solar_HouseGrid_Power.state as Number).floatValue
        Energy_FromGrid.postUpdate(fromgrid/1000)
end

rule "Real Time Overall Consumption - Solar & Grid"
when
        Item Solar_HouseGrid_Power changed
then
        var Number gridconsumption = (Solar_HouseGrid_Power.state as Number).floatValue / 1000
          logInfo("energy", "Grid Consumption is " + gridconsumption)
        var Number solarproduction = (Solar_ACPower.state as Number).floatValue
          logInfo("energy", "Solar production is " + solarproduction)
        var Number totalconsumption = gridconsumption + solarproduction
           Energy_Used.postUpdate(totalconsumption)
end

Thanks!

If you can test if greater than zero then why don’t you use if else?
For example:

then
//    if(Sun_Elevation.state >  0){
//        if(NightState.state != OFF) postUpdate(NightState, OFF)
//    } else {
//        if(NightState.state != ON) postUpdate(NightState, ON)
//    }

Right, so something like this?

When its a negative value, I want to convert it to positive and update the item (Its feeding in). When its a positive value, dont convert it but update the item (its drawing from the grid)

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){
        val Number fromgrid = (Solar_HouseGrid_Power.state as Number).floatValue * -1
          Energy_FromGrid.postUpdate(fromgrid/1000)
        } else {
        if(Solar_HouseGrid_Power.state > 0)
          Energy_FromGrid.postUpdate(fromgrid/1000)
}
end

Multiply the negative value by -1
That makes it positive.

Yep, that’s in my rule. I think it’s working now!

I didn’t see the -1 in your rule as I didn’t scroll across that far.
Glad it’s working.

Just need to now some how display these states as colors in MainUI so I can color the tiles correctly.

All that is here:

Yep just working through it :slight_smile: having issues with negative values , ill sort it out!

Try multiplying those by -1 as well.

1 Like