Temperature conversion problem

Im trying to convert temperature readings from a 433MHz-device. I have a rule by AndrewZ as a model:

val Functions$Function1<Number, BigDecimal> convertTemp = [ tempData |
        if (tempData < 32768) { tempData/10 }
        else { (32768 - tempData)/10 }
]
rule "Temperaturkonvertering 1"
when
        Item utetemp1_raw changed
then
        var String tempHex = utetemp1_raw.state.toString
        var int tempDec = Integer::parseInt(tempHex, 16)
        utetemp1.postUpdate(convertTemp.apply(tempDec))
end

Unfortunately it doesn’t work and I get the following error:

An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.postUpdate(org.eclipse.smarthome.core.items.Item,org.eclipse.smarthome.core.types.State) on instance: null

Help appreciated!

Are you certain utetemp1 exists and is exactly like written, case and everything?

The error implies that utetemp1 doesn’t exist, or the lambda is returning null.

Yes it does… and it is correctly spelled…

The problem seems to be the function … I moved this into the rule instead:

   if (tempData < 32768) { tempData/10 }
        else { (32768 - tempData)/10 }

And then it works without any problems…

Perhaps the result of the calculation isn’t a BigDecimal, even though it is supposed to be.

What if you force it, or use Number instead?

Force it

if(tempData < 32768) { tempData/10 as BigDecimal }
else { (32768 - tempData)/10 as BigDecimal }

Use Number

val Functions$Function1<Number, Number>