Strange results when calculating in rule

Hello again, and thanks for helping in advance!

Today, I was trying to calculate my daily pv income within a rule but sadly I figured out that this is not easy.
I created two Number:Energy items to see the total amount of energy produced and the last day income.

My rule should be triggered at midnight what is not the problem but as my energy monitor only shows a total consumption value I need some calculation first. The variable energy_today show the value 80708400, what looks like a casting issue but I do not know how to make it right.

I hope someone can help to fix my rule, thanks.


Number:Energy total_energy_income  "PV Ertrag Gesamt [%.2f kWh]"    <energy>
Number:Energy yesterday_energy_income  "PV Ertrag Gestern [%.2f kWh]"    <energy>
var Number energy_monitor_yesterday = 0|kWh

rule "Record_Energy_Income"
when
    Time is midnight
then
    var Number energy_today = 0|kWh
    var energy_monitor_today = pv_system_energy.state as QuantityType<Number>
    logInfo("debug","monitor {}", energy_monitor_today)

    energy_today = energy_monitor_today - energy_monitor_yesterday 
    logInfo("debug","Today:{} = {} - {}", energy_today, energy_monitor_today, energy_monitor_yesterday)

    energy_monitor_yesterday = energy_monitor_today
    logInfo("debug","monitor {}", energy_monitor_yesterday)

    //var yesteray = yesterday_energy_income.state
    //var today_energy = ( energy_monitor - yesteray)
    //logInfo("photovoltaik","PV Ertrag war heute {}", today_energy)

    //yesterday_energy_income.postUpdate(yesterday_energy_reading)
    //total_energy_income.postUpdate(total_energy_income.state as QuantityType<Number> + today_energy)
    
end

Outcome


2022-03-09 23:32:40.482 [INFO ] [org.openhab.core.model.script.debug ] - monitor 22.419 kWh
2022-03-09 23:32:40.483 [INFO ] [org.openhab.core.model.script.debug ] - Today:80708400 = 22.419 kWh - 0 kWh
2022-03-09 23:32:40.484 [INFO ] [org.openhab.core.model.script.debug ] - monitor 22.419 kWh

Instead of doing your own calculation I would use persistence and the deltaSince function, what would reduce the rule to one simple line of code

You should save energy_monitor_yesterday into something more permanent, like persistence database, or metadata. Otherwise it will be gone if you ever re-saved / reloaded your script or restarted openhab.

You could just save the pv_system_energy into persistence then do the calculation using deltaSince(now.minusHours(24))

I would follow the advice to do this with persistence.

but to offer some clues on the problem

The right hand side is a Quantity, a value with units, that’s good.
But then you cast it as Number. Numbers types are not Quantity types. Strangely, they can still carry units BUT if you do maths with that it will not work properly.

In DSL rules in general, avoid casting stuff unless you have to. Let the rules interpreter sort it out.
var energy_monitor_yesterday = 0|kWh

Hello rossko57,
Thanks for your explanation, unfortunately it did not change the behaviour showing the wrong value.
Br