Calculate Energy in two tariff model

Hello to Community,

i am struggling for 3 weeks and can find anything useful for calculating price of power consumption with Sonoff POWR2.

Everything would be OK with this rule:

rule “Price Boiler”
when
Item Sonoff_POWR2_Total received update
then
Price_Total.
postUpdate (Sonoff_POWR2_Total.state as DecimalType * 0.37629)
end

but problem is that in my country we have two tariff for power. Between 07-21 is higher price and between 21-07 is lower with 0.376.

Can anybody help me with new rule to calculate for eq. in night with lowerprice,and if works in day hours calculate with higher price, store these values and then sum it.

Best Regards.

Try defining the time periods you need like below. Create an item for Price_Total_day and Price_Total_night.

rule “Price Boiler”
when
Item Sonoff_POWR2_Total received update
then
if(now.getHourOfDay > 7 || now.getHourOfDay < 21)
    Price_Total_day.postUpdate (Sonoff_POWR2_Total.state as DecimalType * 0.57629) //need to adjust value for whatever the day rate is
else
    Price_Total_night.postUpdate(Sonoff_POWR2_Total.state as DecimalType * 0.37629)

end

This is just an idea to help get you started not a complete solution. You’ll need a to create a Time cron rule, or something similar, for adding the totals. You may want to read up on persistence as you’ll need it to get a daily total.

Yes, I had same rule but this is crap. :slight_smile:
I need to store value of last state of Sonoff_POWR2_Total before state update and then calculate.

You will have to add Power consumption to two different items (dependent on time of day). If the item is persisted, you could calculate the consumption within the low/high period, but This is only an option for maybe consumption today

if time is after 7 a.m and before 9 p.m.:

(total at 7 a.m. minus total at start of day)*lowPrice +
(total now minus total at 7 a.m.)*highPrice

Please be aware that getHourOfDay is 0 to 23, so highPrice time would be

if( now.getHourOfDay > 6 && now.getHourOfDay < 21 )

It is to complicated for me…

Next question… Is it possible to separate Sonoff_POWR2_Total on two numbers:
Sonoff_POWR2_Total_LowTariff and
Sonoff_POWR2_Total_HighTariff?

I doubt that there is an option in Tasmota for doing that. As far as I understand, your Item Sonoff_POWR2_Total is an JSONPATH extraction of a tele topic? As this item is already a sum, how to do that job? Of course you can do it in openHAB for future data:

Number Sonoff_POWR2_Total { ... }
Number Sonoff_POWR2_Total_LowTariff
Number Sonoff_POWR2_Total_HighTariff

persist all three Items to rrd4j (everyMinute)

Rule:

rule "add high and low pow tariff"
when
    Time cron "0 * * * * ?" // every Minute
then
    val Number nOld = Sonoff_POWR2_Total.historicState(now.minusMinutes(1),"rrd4j").state as Number
    val Number nNow = Sonoff_POWR2_Total.state as Number
    val Number nConsumption = nNow - nOld
    if(now.getMinuteOfDay > 7 * 60 && now.getMinuteOfDay < 21 * 60 + 1)
        Sonoff_POWR2_Total_HighTariff.postUpdate(Sonoff_POWR2_Total_HighTariff.historicState(now.minusMinutes(1),"rrd4j").state as Number + nConsumption)
    else
        Sonoff_POWR2_Total_LowTariff.postUpdate(Sonoff_POWR2_Total_LowTariff.historicState(now.minusMinutes(1),"rrd4j").state as Number + nConsumption)
end

This will add the delta of Sonoff_POWR2_Total either to high or low tariff according to the time frame. Of course I don’t know how exact this calculation will be :slight_smile:

Hello,

i get this message:

Error during the execution of rule ‘add high and low pow tariff’: cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.persistence.HistoricItem.getState() on null

Try also this, but is the same:

rule “test”

when

Item Sonoff_POWR2_Switch changed to ON

then
time_play_started = now
logInfo(“test”," HistoricalState 1: " + Sonoff_POWR2_Total.historicState(now.minusDays(1),“rrd4j”).state)

end

My rrd4j.persist file

Strategies {
everyMinute : “0 * * * * ?”
everyHour : “0 0 * * * ?”
everyDay : “0 0 0 * * ?”
default = everyChange
}

Items {
* : strategy = everyChange
}

My rrd4j.cfg file:

MyLogger.def=COUNTER,90,0,U,60

MyLogger.archives=AVERAGE,.5,1,1440:MAX,.5,5,2016:MAX,.5,15,2668

MyLogger.items=Sonoff_POWR2_Total, Sonoff_POWR2_Total_LowTariff, Sonoff_POWR2_Total_HighTariff

As you are using rrd4j, please be aware that you have to use everyMinute as strategy.
Using * as items list will persist every Item, I don’t know how large the rrd file per item is (take a look at $OPENHAB_USERDATA/persistence/rrd4j/)

For a storage consuming persistence service it’s better to define only the items which you need to be persisted.


Can you please help me, i have problem with OH3 rule for splitting energy consumption data into Low and High Tariff.
Each time at the end of the month after resetting (postUpdate Items to 0) and when it’s 21:01 LowTariff is not 0+nConsuption difference. Instead LowTariff takes data same like HighTariff.

Persistance is rrd4j.persist each minute and items are all that are in the rule.