I'm getting ... null where there is no null ;)

can please somebody help me understand java math logic? :slight_smile:

    var hpamax = 1050   // local max
    var hpamin = 950    // local min
    var hparng = hpamax - hpamin
    var Number hpcont = Math.round((hparng / 22),3)
    var Number hpa = p0

    var opt = Math.floor((hpa - hpamin) / hpcont)

getting this

2021-11-28 13:24:17.072 [INFO ] [org.openhab.core.model.script.x     ] - 997.8
2021-11-28 13:24:17.073 [INFO ] [org.openhab.core.model.script.x     ] - 950
2021-11-28 13:24:17.074 [INFO ] [org.openhab.core.model.script.x     ] - 4
2021-11-28 13:24:17.075 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'weather-10' failed: An error occurred during the script execution: Could not invoke method: java.lang.Math.floor(double) on instance: null in weather

can somebody kick me, because I have no idea why math.floor is seeing null somewhere??
because i’m expecting
math.floor((997.8 - 950) / 4)

What’s the definition of p0?

it’s number

val p0   = (House_Pressure_Sealevel.state as Number).floatValue

not really sure why, but this made it work

    var opt = Math.floor((hpa.floatValue - hpamin) / hpcont)

It’s quite confusing … :wink:

Could not invoke method: java.lang.Math.floor(double) on instance: null in weather

This sort of error is a little bit misleading in a way. The problem isn’t that the calculation is null or any of the three variables involved in the calculation is null. The problem is that Rules DSL couldn’t figure out a way to coerce the type of the result of the calculation to the type required by Math.floor.

As shown in the error, Math.floor requires a double. But calculation in Rules DSL result in a BigDecimal, not a primitive double or int. So this is one of the cases where you need to convert and specify the type explicitly.

Your “fix” worked because you coerced one of the number to be a primitive float which caused Rules DSL to keep the result of the calculation as a float and there is in fact a Math.floor(float).

I would recommend doing the following instead.

    var opt = Math.floor(((hpa - hpamin)/hpcont).doubleValue())

Let Rules DSL keep everything as a BigDecimal until the vary last moment where we convert the result of the calculation to a double.

Thank you for your explanation! as always detailed and on point.
Changed code as you are suggesting.