Type java error

I have 2 items. One is a temperature sensor with type Number and the other is a setpoint with type Number. When I try to subtract them in code i get a java error. I have tried every which way to get round this. I am sure this is a common problem but I cannot debug this. Here is the rule:

var SetPoint = 1.00
var CurrentTemp = 1.00
var Error = 1.00
var Timer LoopTimer = null // global variable usually
var kp = 1.10
var ki = 0.03
var IntegralError = 0.01
var ProportionalError = 1.00

rule "TPIRule"
when
    Item TPI_test_setpoint changed
then
    if (TPI_test_switch.state == ON)
    {
        CurrentTemp = GenericMQTTThing_Back_room_temperature.state as Number
        logInfo ("TPItest  ", "Current temp = " + CurrentTemp)
        LoopTimer = createTimer(now, [ |
            if(TPI_test_switch.state == OFF) LoopTimer = null
            else 
            {
                CurrentTemp = GenericMQTTThing_Back_room_temperature.state as Number
                SetPoint = TPI_test_setpoint.state as Number
                ProportionalError = ((SetPoint - CurrentTemp) * kp)
                // IntegralError = IntegralError + ((SetPoint - CurrentTemp) * ki)
                // Error = ProportionalError + IntegralError
                logInfo ("TPItest  ", "Current temp = " + CurrentTemp + " Setpoint = " + SetPoint)
                LoopTimer.reschedule(now.plusMillis(1000))
            }
        ])
    }
end

The error is thrown on the first subtraction:

Caused by: java.lang.IllegalStateException: Could not invoke method: org.eclipse.xtext.xbase.lib.DoubleExtensions.operator_minus(double,double) on instance: null

Yet if I comment out the line:

// ProportionalError = ((SetPoint - CurrentTemp) * kp)

the loop works correctly:

2020-02-25 23:20:47.384 [INFO ] [pse.smarthome.model.script.TPItest  ] - Current temp = 21.22 Setpoint = 4

2020-02-25 23:20:48.404 [INFO ] [pse.smarthome.model.script.TPItest  ] - Current temp = 21.22 Setpoint = 4

2020-02-25 23:20:49.427 [INFO ] [pse.smarthome.model.script.TPItest  ] - Current temp = 21.22 Setpoint = 4

2020-02-25 23:20:50.441 [INFO ] [pse.smarthome.model.script.TPItest  ] - Current temp = 21.22 Setpoint = 4

so neither value appears to be NULL.

Any help please?

As written it should work. Maybe we can give the Rules Ending a little bit of a hint. Define the variables as Numbers.

val Number SetPoint = 1.00
val Number CurrentTemp = 1.00

If that doesn’t work, even though I advocate against it all over the place, coerce the Number to a primitive to do the math.

    ProportionalError = ((SetPoint.floatValue - CurrentTemp.floatValue) * kp)

Thanks. Will give it a try later. I did wonder if the issue is I declared the global variable outside the lambda having read today somewhere that it isn’t possible. But not at home to confirm.

That’s only a problem for global lambdas. The lambdas you create inside a Rule inherit the full context of the Rule so all the global variables will be available. Global lambdas don’t have a context when they are created so you have to pass them as argumnets.

Thanks. Your first suggestion has fixed it. i.e. var Number SetPoint = 1.0 etc.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.