Variable in rule and if else statement

  • Platform information:
    • openHAB version: 3.2.0.M2

Hello, please explain me the use of variables in rule. has anything changed due to an update?

In the past the rule work fine, but not anymore.

var Boden = (HmIPWKlima04_ActualTemperature.state as QuantityType<Number>).doubleValue
var Decke = (HmIPHeater09_ActualTemperature.state as QuantityType<Number>).doubleValue

if ((Boden != NULL) && (Decke != NULL)) {
  VI_Heating_Buero_IST.postUpdate(((Boden) + (Decke)) / 2)
} else {
  VI_Heating_Buero_IST.postUpdate(0) 

Without the if else statement it work.

var Boden = (HmIPWKlima04_ActualTemperature.state as QuantityType<Number>).doubleValue
var Decke = (HmIPHeater09_ActualTemperature.state as QuantityType<Number>).doubleValue

VI_Heating_Buero_IST.postUpdate(((Boden) + (Decke)) / 2)

WHY?

These will never be state NULL because you’ve already got the .doubleValue. If they were NULL, the rule would fail at that point first.
You probably want to check before getting the state.

I just want to make sure that nothing is calculated if one of the two values is NULL.

I wonder why it worked for months and now it doesn’t work anymore. It’s like he forgot the variables in the IF.

So you’ll be wanting to check for NULL states before trying to turn them into numbers.

Well, without you telling us what “doesn’t work” means, it is difficult to say. Nothing has changed about openHAB in this area. Maybe your variables haven’t been NULL for months.

The old rule would not have posted any updates if either Item were NULL state, because it would fail. Maybe you didn’t look for errors before.

I thing the NULL is not the problem.

Both VAR are filled with values

[INFO ] [e.model.script.IST Temperatur - Büro] - Temp: 21.6 | 22.0

the raw value is “21.6 °C” and with (HmIPWKlima04_ActualTemperature.state as QuantityType).doubleValue it reduce it to 21.6 as Number.

this work, why not with the IF

var Boden = (HmIPWKlima04_ActualTemperature.state as QuantityType<Number>).doubleValue
var Decke = (HmIPHeater09_ActualTemperature.state as QuantityType<Number>).doubleValue
logInfo("IST Temperatur - Büro", "Temp: " + Decke + " | " + Boden)
VI_Heating_Buero_IST.postUpdate(((Boden) + (Decke)) / 2)
[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Klima_Calc_Buero_Temp' failed: var Boden = (HmIPWKlima04_ActualTemperature.state as QuantityType<Number>).doubleValue

var Decke = (HmIPHeater09_ActualTemperature.state as QuantityType<Number>).doubleValue

logInfo("IST Temp - Büro", ""Boden + Decke)

VI_Heating_Buero_IST.postUpdate(((Boden) + (Decke)) / 2)

There is no if() in the code that you are showng us now.

this code work

var Boden = (HmIPWKlima04_ActualTemperature.state as QuantityType<Number>).doubleValue
var Decke = (HmIPHeater09_ActualTemperature.state as QuantityType<Number>).doubleValue
logInfo("IST Temperatur - Büro", "Temp: " + Decke + " | " + Boden)
VI_Heating_Buero_IST.postUpdate(((Boden) + (Decke)) / 2)

and this code dont work

var Boden = (HmIPWKlima04_ActualTemperature.state as QuantityType<Number>).doubleValue
var Decke = (HmIPHeater09_ActualTemperature.state as QuantityType<Number>).doubleValue
logInfo("IST Temperatur - Büro", "Temp: " + Decke + " | " + Boden)
if ((Boden != NULL) && (Decke != NULL)) {
  VI_Heating_Buero_IST.postUpdate(((Boden) + (Decke)) / 2)
} else {
  VI_Heating_Buero_IST.postUpdate(0) 

Okay, what does it do instead?
The if() is completely pointless and will never execute the else section, but that shouldn’t stop it working.

The snippet shown needs a closing curly brace } but I expect that just got lost in the copy-paste