Adding up two numbers

Friends,
I again need your help with a basic thing. I want to add up two values. I already spend 2 hours trying to find out myself but I could not figure it out.

I have:

var Number result  = (soll_gEG1 as DecimalType) + (offset_gEG1 as DecimalType)

I get the error:
2017-04-02 12:08:10.188 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule Get Temp: org.eclipse.smarthome.core.library.types.DecimalType

I already tried this, but also did not work:

var Number result  = (soll_gEG1.state as DecimalType) + (offset_gEG1.state as DecimalType)

The two value have been derive from a read:

var Number soll_gEG1 = transform("JSONPATH", "$.[0].raeume[0].solltemperatur", json) 
var Number offset_gEG1 = transform("JSONPATH", "$.[0].raeume[0].total_offset", json) 

Both values are not NULL. Can somebody please help me how to get it to work?

Thanks,
mroggi

If all variables are number types, there should be no need to cast, so:

var Number result  = soll_gEG1 + offset_gEG1

ought to be OK.

Thanks Oli for your swift reply.

I tried to add the two numbers. When I do this I get an error:

2017-04-02 21:12:56.201 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule Get Temp: An error occured during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.lib.NumberExtensions.operator_plus(java.lang.Number,java.lang.Number) on instance: null

I checked that both numbers are not null:

2017-04-02 21:12:56.190 [INFO ] [clipse.smarthome.model.script.Offset] - ************* soll_gEG1: 0.5
2017-04-02 21:12:56.196 [INFO ] [clipse.smarthome.model.script.Offset] - ************* offset_gEG1: 22.0

Here is the simple line of code:

soll_gEG1 = soll_gEG1.intValue + offset_gEG1 

Any idea what I am doing wrong?

Thanks a ton!

try:
soll_gEG1 = soll_gEG1 + offset_gEG1

Thanks Oli. Just tried but get the same error. Very strange, cannot be that hard to add two numbers.

Indeed. Uh…maybe because decimals?
try:
soll_gEG1 = soll_gEG1 as as DecimalType + offset_gEG1 as DecimalType
but picking at straws really…

Oli,
again, thanks for your support. I spent another 6 hours adding two numbers: crazy! Its still not working.

I can clearly see that the veriable is not NULL before doing a calculation. But in the calculation I get error messages stating that the variable is NULL. As soon as I try to cast it to x as DecimalType or x.doubleValue I get an error message.

Here is the line I get the value from:

var Number temp_gEG1 = transform("JSONPATH", "$.[0].raeume[0].temperatur", json)  

as soon as I write:

var Number temp_gEG1 = transform("JSONPATH", "$.[0].raeume[0].temperatur", json)  as DecimalType 
var Number temp_gEG1 = transform("JSONPATH", "$.[0].raeume[0].temperatur", json).doubleValue

I get error messages like

Error during the execution of rule Get Hum: An error occured during the script execution: The name ‘.doubleValue’ cannot be resolved to an item or type.
or
Error during the execution of rule Get Hum: org.eclipse.smarthome.core.library.types.DecimalType

Any ideas from the group how I can solve this? All I want to do is to add two values from my heating system.

Would be extremely grateful for help!!!

After another 2 hours I found the solution:

DecimalType.valueOf(soll_gEG1)

does the trick for me.

3 Likes