I have a Number item,Nibe_44300_HeatMtrHeatCprAdd, which gets updated regularly. It’s a floating point number.
All I’m trying to do in a rule is to calculate the difference between the current value and the previous value, and store that in a different Number item (Nibe_44300_HeatMtrHeatCprAdd_Counter. Here is what I have been trying:
rule "Nibe_44300_HeatMtrHeatCprAdd updated, calculate difference"
when
Item Nibe_44300_HeatMtrHeatCprAdd received update
then
var double currentStateValue = (Nibe_44300_HeatMtrHeatCprAdd.state as DecimalType).doubleValue
var double previousStateValue = (Nibe_44300_HeatMtrHeatCprAdd.previousState.state as DecimalType).doubleValue
var double diff = currentStateValue - previousStateValue
logInfo("test", "test: " + diff + " (" + Nibe_44300_HeatMtrHeatCprAdd.state +" : " + Nibe_44300_HeatMtrHeatCprAdd.previousState.state +")")
postUpdate(Nibe_44300_HeatMtrHeatCprAdd_Counter, diff)
end
Somehow that’s not totally working. Somehow the calculations seem to be somewhat inaccurate, as I am getting the following, when in fact only the first decimal place should change, if at all:
2017-11-24 20:09:06.166 [INFO ] [g.eclipse.smarthome.model.script.test] - test: 0.09999999999990905 (4091.9 : 4091.8)
2017-11-24 20:10:06.465 [INFO ] [g.eclipse.smarthome.model.script.test] - test: 0.0 (4091.9 : 4091.9)
2017-11-24 20:11:06.285 [INFO ] [g.eclipse.smarthome.model.script.test] - test: 0.09999999999990905 (4092.0 : 4092.0)
2017-11-24 20:18:06.621 [INFO ] [g.eclipse.smarthome.model.script.test] - test: 0.1000000000003638 (4092.3 : 4092.3)
I tried using var Number instead of var double, but I’m not really sure what I’m doing. Using Numbers, I got the following, which is also totally weird (the jumps from one change to the next doesn’t get “recognized”, and then wheren there is no change, I am getting a difference value?!):
2017-11-24 19:49:05.384 [INFO ] [g.eclipse.smarthome.model.script.test] - test: 0.0 (4091.1 : 4091.1)
2017-11-24 19:50:05.510 [INFO ] [g.eclipse.smarthome.model.script.test] - test: 0.0 (4091.2 : 4091.2)
2017-11-24 19:51:05.490 [INFO ] [g.eclipse.smarthome.model.script.test] - test: 0.0 (4091.2 : 4091.2)
2017-11-24 19:52:05.610 [INFO ] [g.eclipse.smarthome.model.script.test] - test: 0.0 (4091.2 : 4091.2)
2017-11-24 19:53:05.626 [INFO ] [g.eclipse.smarthome.model.script.test] - test: 0.0 (4091.3 : 4091.3)
2017-11-24 19:54:05.512 [INFO ] [g.eclipse.smarthome.model.script.test] - test: 0.0 (4091.3 : 4091.3)
2017-11-24 19:55:05.640 [INFO ] [g.eclipse.smarthome.model.script.test] - test: 0.1 (4091.4 : 4091.4)
2017-11-24 19:56:05.651 [INFO ] [g.eclipse.smarthome.model.script.test] - test: 0.0 (4091.4 : 4091.4)
2017-11-24 19:57:05.676 [INFO ] [g.eclipse.smarthome.model.script.test] - test: 0.0 (4091.4 : 4091.4)
2017-11-24 19:58:05.850 [INFO ] [g.eclipse.smarthome.model.script.test] - test: 0.1 (4091.5 : 4091.5)
What am I doing wrong? Should I be using doubles at all? What else? floats? I am confused.