Calculation error with Numbers

I tried multiple ways to make my calculations to work but still fail. I hope someone can help.

Number GasVerbruikPerDag “Gasverbruik per dag [%.3f m3]” (Totalen)
Number GasVerbruikReference “Reference [%.3f m3]”

I show only one calculation below in 2 different ways but both fail

In my rule if have a cron trigger every night and the the following code is processed

GasVerbruikPerDag.postUpdate ((GasVerbruik.state as QuantityType).doubleValue - (GasVerbruikReference.state as QuantityType).doubleValue)
The error in the log file is as follows.

Could not cast 3060.516 to org.eclipse.smarthome.core.library.types.QuantityType; line 12, column 94, length 50

GasVerbruikPerDag.postUpdate ((GasVerbruik.getStateAs(QuantityType).doubleValue - GasVerbruikReference.getStateAs(QuantityType).doubleValue))

The error in the log file is as follows.
cannot invoke method public double org.eclipse.smarthome.core.library.types.QuantityType.doubleValue() on null

It’s just a Number, it is not a Quantity Type with units.

GasVerbruikPerDag.postUpdate ((GasVerbruik.state as Number) - (GasVerbruikReference.state as Number))

This willblow up if either Item has state NULL at the time, you might need to check for that before attempting the maths.

1 Like

Thanks for your feedback.
I found a right way to initialize the values in a posting from you. As a example I run this at startup
if ( ElectraVerbruikReference.state == NULL) { ElectraVerbruikReference.postUpdate(0) }

I was thinking the same and started with this but get a strange results.

The items i’m using in this example.
Number ElectraVerbruikReference “Reference [%.3f KWh]”
Number:Energy ElektraVerbruikDal “Elektriciteitsverbruik dal [%.3f KWh]” (Electra) {channel=“dsmr:electricity_v5_0:DSMRMeter:electricityV5:emeter_delivery_tariff1”}
Number:Energy ElektraVerbruikPiek “Elektriciteitsverbruik piek [%.3f KWh]” (Electra) {channel=“dsmr:electricity_v5_0:DSMRMeter:electricityV5:emeter_delivery_tariff2”}

The code
ElectraVerbruikReference.postUpdate (ElektraVerbruikDal.state as Number + ElektraVerbruikPiek.state as Number)
logInfo(“default.rules”, "Middernacht berekeningen 5: " + ElectraVerbruikReference.state + " " + ElektraVerbruikDal.state + " " + ElektraVerbruikPiek.state)

The logfile
Middernacht berekeningen 5: 24658290000.000 3787.608 kWh 3061.917 kWh

What do I wrong? The first value should be 6849.525

What makes you think that? postUpdate is asynchronous. The rule does not stop and wait for it take effect. The Item state in the next line will still be “before”. But you don’t need to examine the Item state because you already know what you just posted to it.

Not sure I understand your explanation. For the test I run this rule every 30 sec. So even if asynchronous it should log at least the value of the previous calculation or not?

If I change the calculation to
ElectraVerbruikReference.postUpdate (ElektraVerbruikDal.getStateAs(QuantityType).doubleValue + ElektraVerbruikPiek.getStateAs(QuantityType).doubleValue)
I get the correct value
Log result
Middernacht berekeningen 5: 6850.851000000001 3787.608 kWh 3063.243 kWh
But to make the other calculation to work I need to do it as below.
ElectraVerbruikPerDag.postUpdate (ElektraVerbruikDal.getStateAs(QuantityType).doubleValue + ElektraVerbruikPiek.getStateAs(QuantityType).doubleValue - ElectraVerbruikReference.state as Number)

But all are numbers. There must be a difference somehow between Numer and Number

Number ElectraVerbruikPerDag “Electra verbruik per dag [%.3f KWh]”
Number:Energy ElektraVerbruikDal “Elektriciteitsverbruik dal [%.3f KWh]” (Electra) {channel=“dsmr:electricity_v5_0:DSMRMeter:electricityV5:emeter_delivery_tariff1”}

but I don’t understand this.

I changed all calculations matching my remark above I believe it works. Will be sure tomorrow.
This means that every definition where there is something behind the Number like :Energy or :Gas
(Number:Energy or Number:Volume)
I need for the calculation to use .getStateAs(QuantityType).doubleValue and for the other Numbers .state as Number.
I hope someone can explain this. Thanks in advance

Yes, you should see in the log the results of your postUpdate (assuming it causes a change). We can’t see that and you haven’t said what you can see.

You’re essentially doing the same as here -

I think you’re getting in a mess here, with a mix on plain Number and Quantity Types, that you are trying to treat all alike and making assumptions about.

I don’t really understand why you don’t make your totalling Item a
Number:Energy type too. Then this should work -

logInfo("test", "Dal " + ElektraVerbruikDal.state.toString)
logInfo("test", "Piek " + ElektraVerbruikPiek.state.toString)
// this wil give a sum with units
var summing = ElektraVerbruikDal.state as QuantityType<Energy> + ElektraVerbruikPiek.state as QuantityType<Energy>
logInfo("test", "Sum " + summing.toString
// this will post to Item with units

A QuantityType has two parts, numeric & units
1 MWh + 1000 kWh = 2000000 Wh
The framework can do that calculation with QuantityTypes, but obviously it goes wrong if you extract just the numeric part and do it without units.

I tried to apply your suggestion but get a error
var summing = ElektraVerbruikDal.state as QuantityType + ElektraVerbruikPiek.state as QuantityType
The error is “Energy cannot be resolved to a type”

I hope I formulate my question correctly.
Where can I find how for instance a Number:Energy is designed.
How to access all the different items/types of this Number and what items it contains.

I think you must be on an older OH version? I think there may be imports needed.
You could try
var summing = ElektraVerbruikDal.state as QuantityType<Number> + ElektraVerbruikPiek.state as QuantityType<Number>

Note that content gets lost in posts here if you do not use code fences.