Script execution worked well in the past since the upgrade to OH 3.4.4 error is logged

  • Platform information:runtimeInfo:
    version: 3.4.4
    buildString: Release Build
    locale: de-DE
    systemInfo:
    configFolder: /openhab/conf
    userdataFolder: /openhab/userdata
    logFolder: /openhab/userdata/logs
    javaVersion: 11.0.19
    javaVendor: Eclipse Adoptium
    javaVendorVersion: Temurin-11.0.19+7
    osName: Linux
    osVersion: 5.13.x
    osArchitecture: amd64
    availableProcessors: 4
    freeMemory: 1172249456
    totalMemory: 1529872384
    startLevel: 100
  • Issue of the topic: comparison of two variables not working

I have a rule to control my house ventilation based on the CO2 values from my Netatmo sensors. The rule worked fine until I upgraded from OH 3.0.2 to OH 3.4.4.

Below the rule:

var t1_co2 = new DecimalType(400) 
var t0_co2 = new DecimalType(400)
var qt1_t0 = new DecimalType(1)
var cwl_output = new DecimalType(1)
val co2_min = new DecimalType(500)
val fan_max = new DecimalType(45)
val co2_range = new DecimalType(22) //Wert 1500 ppm an dem die L├╝ftung auf 45 Prozent l├Ąuft
 
rule "Hausl├╝ftung anpassen"
when
    Item gNetatmoCO2 changed
then
    logInfo("L├╝ftung","Hausl├╝ftung anpassen")
    if (Autolueftung_S.state == ON) {
        //Historie aktualisieren
        t1_co2 = t0_co2

        //Aktualisiere t0
        t0_co2 = (gNetatmoCO2.state as DecimalType)
            
        //Quotient berechnen
        qt1_t0 = t0_co2 / t1_co2
        logInfo("L├╝ftung","Hausl├╝ftung anpassen 2")
        //Berechne neuen Outputwert, wenn CO2 ├╝ber CO2 min liegt
        if (t0_co2 > co2_min) {
            cwl_output = (t0_co2 - co2_min) / co2_range * qt1_t0
            logInfo("L├╝ftung","Hausl├╝ftung anpassen 2.1 " + cwl_output)
        }
        else {
            cwl_output = new DecimalType(0)
            logInfo("L├╝ftung","Hausl├╝ftung anpassen 2.2")
        }
        logInfo("L├╝ftung","Hausl├╝ftung anpassen 3 " + fan_max)
        // Pr├╝fe ob berechneter Wert unter max fan liegt, sonst L├╝ftung auf fan_max setzen
        if (cwl_output < fan_max) {
            UG_CWL_Hauslueftung_D.sendCommand(cwl_output as Number)
        }
        else {
            UG_CWL_Hauslueftung_D.sendCommand(fan_max as Number)
        }
    }
end

When the rule is executed I get the following error (see below). I included additional logs to ensure none of the values is null.

2023-07-08 14:06:15.095 [INFO ] [rg.openhab.core.model.script.Lüftung] - Hauslüftung anpassen
2023-07-08 14:06:15.097 [INFO ] [rg.openhab.core.model.script.Lüftung] - Hauslüftung anpassen 2
2023-07-08 14:06:15.098 [INFO ] [rg.openhab.core.model.script.Lüftung] - Hauslüftung anpassen 2.1 3.3270181350000000
2023-07-08 14:06:15.099 [INFO ] [rg.openhab.core.model.script.Lüftung] - Hauslüftung anpassen 3 45
2023-07-08 14:06:15.100 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Air_control-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.lib.NumberExtensions.operator_lessThan(org.openhab.core.types.Type,java.lang.Number) on instance: null in Air_control
2023-07-08 14:06:15.171 [INFO ] [rg.openhab.core.model.script.Lüftung] - Hauslüftung anpassen
2023-07-08 14:06:15.172 [INFO ] [rg.openhab.core.model.script.Lüftung] - Hauslüftung anpassen 2
2023-07-08 14:06:15.173 [INFO ] [rg.openhab.core.model.script.Lüftung] - Hauslüftung anpassen 2.1 3.9700645972869072
2023-07-08 14:06:15.174 [INFO ] [rg.openhab.core.model.script.Lüftung] - Hauslüftung anpassen 3 45
2023-07-08 14:06:15.174 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Air_control-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.lib.NumberExtensions.operator_lessThan(org.openhab.core.types.Type,java.lang.Number) on instance: null in Air_control

Thanks for your help.

There is only one less than comparison in your rule, here:

if (cwl_output < fan_max)

so either cwl_output is null or fan_max is null (or at least openHAB treats it as null).

First option is, not to set all types as DecimalType but to let openHAB set the vars itself.

As there is one Item involvedÔÇŽ what exact Item type is gNetatmoCO2 of? If itÔÇÖs of Type Number, use it as Number, if itÔÇÖs of Type Number:Dimensionless, youÔÇÖll have to ensure that itÔÇÖs used as a Value without any unit.
Maybe consider to do a more failsafe code:

var     t1_co2 = 400.0   // openHAB sucht selbst den passenden Datentyp
var     t0_co2 = 400.0
var     qt1_t0 = 1.0
var cwl_output = 1.0
val    co2_min = 500.0
val    fan_max = 45.0
val  co2_range = 22.0    // Wert 1500 ppm an dem die L├╝ftung auf 45 Prozent l├Ąuft
 
rule "Hausl├╝ftung anpassen"
when
    Item gNetatmoCO2 changed
then
    if(Autolueftung_S.state != ON)                                                  // Automatik aktiv?
        return;

    if(previousState instanceof Number)
        t1_co2 = (previousState as Number).floatValue                               // Letzten Wert holen

    if(newState instanceof Number)
        t0_co2 = (newState as Number).floatValue                                    // aktuellen Wert holen
    else {
        logWarn("lueftung","gNetatmoCO2 liefert ung├╝ltigen Status! ({})",newState)
        return;
    }

    if (t0_co2 > co2_min && t1_co2 != 0 && co2_range != 0)                          // Berechne neuen Outputwert, falls n├Âtig und m├Âglich
        cwl_output = (t0_co2 - co2_min) * t0_co2 / t1_co2 / co2_range 
    else
        cwl_output = 0

    logInfo("lueftung","clw_output {} fan_max {}", cwl_output, fan_max)
    if (cwl_output > fan_max)                                                       // Wert ├╝ber fan_max?
        cwl_output = fan_max                                                        // dann auf fan_max setzen!

    UG_CWL_Hauslueftung_D.sendCommand(cwl_output.intValue)
end
2 Likes

Thank you for your help, the new code is working well!

item gNetatmoCO2 is defined as Group:Number:MAX

I thought already that one of the two values might be the problem, therefore I included them in the logfile. The only difference was that cwl_output was recorded as a decimal type, but fan_max just as a numberÔÇŽ