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

Hi Kris.

Are you still using the rule above?
Im using openhab 3.3.0, and I simply cant get it to work… Nothing seems to happen.

This is my rules:

rule "Convert Inverter negative Power to Positive"
when
        Item Grid_Power 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(Grid_Power.state < 0){
        val Number fromgrid = (Grid_Power.state as Number).floatValue * -1
          Energy_FromGrid.postUpdate(fromgrid/1000)

        } else {

        if(Grid_Power.state > 0)
          Energy_FromGrid.postUpdate(fromgrid/1000)
}
end

My original item is: Grid_Power
I Made an proxy item to work with which is: Energy_FromGrid

Grid_Power is udating just fine every 5 sec. But Energy_FromGrid never gets updated…
Any idea of what am I missing?

What is the value of fromgrid in the second case? Seems not to be defined as it is only set in the first if clause.

Ahh, seems like you have a point… Its just a Val item, which needs to be defined…

Nope, cant get it to work. I think there are issue in the original rules, and I´m havnt got the knowlegde to correct it. Spend a few hours trying now getting nothing but either obvious errors or no result :frowning:

Then add logging to see if the rule is started and if you get into the if clauses at all.

I get no logging inside the if at all.
First loginfo returns fine. Second loginfo (within the if) do not show in the log at all :face_with_raised_eyebrow:

rule "Convert Inverter Power to the Grid to Positive"
when
        Item Grid_Power changed
then
        logInfo("Grid Power","Grid power is {} ", Grid_Power.state)

        // If its a negative value, we are feeding to the grid so update the item and change negative to positive for display purposes

        if(Grid_Power.state < 0){
        val Number fromgrid = (Grid_Power.state as Number).floatValue * -1

        logInfo("From Grid","Grid power is {} and fromgrid is {}  ", Grid_Power.state,fromgrid.state)

          Energy_FromGrid.postUpdate(fromgrid/1000)
        } else {
        if(Grid_Power.state > 0)
          Energy_FromGrid.postUpdate(fromgrid/1000)
}
end
2023-05-07 18:00:04.745 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Grid_Power' changed from -1993.9 W to -1993.3 W
==> /var/log/openhab/openhab.log <==
2023-05-07 18:00:04.746 [INFO ] [openhab.core.model.script.Grid Power] - Grid power is -1993.3 W 
==> /var/log/openhab/events.log <==

According to this log, your item has a quantity. That means you have to take the unit into account when doing the numerical comparison.

if (Grid_Power.state < 0|W) {

For a more in-depth explanation see here (even though you’re using power and not temp):

1 Like

Ahhh… damit, the only thing I didnt try…
It´s working just fine now… Thank you very much, Justin!

Shorten your code to a 1-liner:

val Number fromgrid = (Grid_Power.state as Number).floatValue * (if (Grid_Power.state < 0|W) -1 else 1)
1 Like

Alot easier. Thanks alot Jorg!