[SOLVED] Comparison operator flags null that shouldn't be there

  • Platform information:
    • Hardware: CPUArchitecture/RAM/storage Raspberry Pi 3
    • OS: what OS is used and which version Rasbian Stretch - up-to-date
    • Java Runtime Environment: which java platform is used and what version Not sure how to determine
    • openHAB version: 2.4 (packaage install)
  • Issue of the topic: please be detailed explaining your issue

Comparison between a number var and a constant complains of null value. Trying to turn on light switch when sensor luminance falls below threshold, and turn it off when it rises above a higher threshold. Each luminance change event stores the current value and successively compares to the old one to make sure the value crosses the threshold. 999999 is used to flag the old value for the first event (I was using -1, but changed it in subsequent debugging). System startup rule should not be necessary but again created for debug purposes.

Conditional statement is flagged with a null value, but null is explicitly tested prior to this line so should never happen :slight_smile:

            if (ff_k_pendants_lastvalue != 999999) {

I probably should rename ff_k_pendants_lastvalue to luminance_previous for clarity.

This is very likely my misunderstanding of the scripting language or environment. I’m not sure how variable typing and scoping works, being more accustomed to C and Python.

  • Please post configurations (if applicable):
    • Items configuration related to the issue

Aeotec Smart Sensor 6 (although not implicated in this issue)

  • Sitemap configuration related to the issue
  • Rules code related to the issue
var ff_k_pendants_threshold_lo = 20
var ff_k_pendants_threshold_hi = 30
var ff_k_pendants_lastvalue = 999999
var luminance = 0

// Experiment - triggers twice when rule is reloaded, both on reload & refresh

rule "System restart"
when
        System started
then
        if (ff_k_pendants_lastvalue == null) {
                logWarn("DefaultRules", "Restart: last value was null")
        } else {
                logWarn("DefaultRules", "Restart: Last value was [{}]", ff_k_pendants_lastvalue)
        }
        ff_k_pendants_lastvalue = 999999
end

// When it gets dark in the family room, turn on the kitchen pendants

rule "FF_K_Pendants_On_When_Dark"
when
        Item Sensor_FF_FR_Luminance changed
then
        if (ff_k_pendants_lastvalue == null) {
                ff_k_pendants_lastvalue = 999999
        }
        if (Sensor_FF_FR_Luminance.state != NULL && Sensor_FF_FR_Luminance.state != null) {
                luminance = Sensor_FF_FR_Luminance.state as Number
                logWarn("DefaultRules", "FR Luminance now [{}], was [{}]", luminance, ff_k_pendants_lastvalue)
                if (ff_k_pendants_lastvalue != 999999) {
                        if (ff_k_pendants_threshold_lo <= ff_k_pendants_lastvalue) {
                                if (luminance < ff_k_pendants_threshold_lo) {
                                        sendCommand(FF_K_Pendants, ON)
                                }
                        }
                        if (ff_k_pendants_lastvalue <= ff_k_pendants_threshold_hi) {
                                if (ff_k_pendants_threshold_hi < luminance) {
                                        sendCommand(FF_K_Pendants, OFF)
                                }
                        }
                }
                ff_k_pendants_lastvalue = luminance
        } else {
                ff_k_pendants_lastvalue = 999999
        }
end
  • Services configuration related to the issue
  • If logs where generated please post these here using code fences:
2019-03-17 11:44:37.040 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'default.rules'
2019-03-17 11:44:53.233 [WARN ] [.smarthome.model.script.DefaultRules] - Restart: Last value was [999999]
2019-03-17 11:44:53.451 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'default.rules'
2019-03-17 11:45:05.116 [WARN ] [.smarthome.model.script.DefaultRules] - Restart: Last value was [999999]
2019-03-17 11:47:07.095 [WARN ] [.smarthome.model.script.DefaultRules] - FR Luminance now [252], was [999999]
2019-03-17 11:53:23.552 [WARN ] [.smarthome.model.script.DefaultRules] - FR Luminance now [254], was [252]
2019-03-17 11:53:23.561 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'FF_K_Pendants_On_When_Dark': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_notEquals(int,int) on instance: null
2019-03-17 12:03:23.735 [WARN ] [.smarthome.model.script.DefaultRules] - FR Luminance now [253], was [252]
2019-03-17 12:03:23.747 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'FF_K_Pendants_On_When_Dark': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_notEquals(int,int) on instance: null

Have you considered that might be the source of the error? The ‘proper’ way to test would be !== null. But it’s a bit pointless anyway, you’ve already checked if state is NULL and that would blow up if the Item were null.

Thanks for the reply. Yes I did consider that, but from the log output it seems to get through that line OK and falls into the only other != in the code which is the one I called out.

Based on what you mentioned it seems I should check for !== null first, then != NULL.

Chris

Right enough.
I think I’d try defining all your global var as Number types.

That nailed it. It all works perfectly now. Thanks! (Onward to the next thing)