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?