Subtraction of two values gives always 0

Hey there,

i have the following script:

var Number actual = (Stromzahler_Zahlerstand.state as DecimalType)
var Number prev = (Stromzahler_Zahlerstand.previousState.state as DecimalType)   
var Number energyDiff = (actual - prev) * 1000
logInfo("default.rules", "Previous-State: " + Stromzahler_Zahlerstand.previousState(true).state.toString)  
logInfo("default.rules", "Actual-State: " + Stromzahler_Zahlerstand.state.toString)  
logInfo("default.rules", "Diff: " + energyDiff.toString)  
logInfo("rule name", "temp=" + energyDiff) 

Tips produces the following log-output:

2022-11-09 20:39:07.267 [INFO ] [org.openhab.core.model.script.test  ] - elapsed=69265
2022-11-09 20:39:07.280 [INFO ] [org.openhab.core.model.script.test  ] - lastchange=2022-11-09T20:37:58+01:00
2022-11-09 20:39:07.284 [INFO ] [g.openhab.core.model.script.kWhPower] - duration in ms: 69265
2022-11-09 20:39:07.357 [INFO ] [nhab.core.model.script.default.rules] - Previous-State: 30145.0
2022-11-09 20:39:07.361 [INFO ] [nhab.core.model.script.default.rules] - Actual-State: 30145.1
2022-11-09 20:39:07.364 [INFO ] [nhab.core.model.script.default.rules] - Diff: 0.0
2022-11-09 20:39:07.368 [INFO ] [.openhab.core.model.script.rule name] - temp=0.0
2022-11-09 20:39:07.372 [INFO ] [g.openhab.core.model.script.kWhPower] - energyDiff in Wh: 0
2022-11-09 20:39:07.380 [INFO ] [nhab.core.model.script.default.rules] - PowerCalc: 0

energyDiff should be (30145,1 - 30145,0)*1000 = 100
But in the log the diff is 0…

Do you see what is going wrong here?

But those are not the values you are calculating with; Item states may change during execution of a rule, and the persistence function is not the same.

logInfo("default.rules", "Previous-State: " + prev.toString)  
logInfo("default.rules", "Actual-State: " + actual.toString)