[SOLVED] Problem with testing state of item with number type

I have this item:

Number   LightSensorCurrentLevel "_Light Sensor Current Level [%d Lm]" (GDevices,GRoom5) {mios="unit:house,device:137/service/LightSensor1/CurrentLevel"}

Which correctly reports the current light level using this sitemap:

Text item=LightSensorCurrentLevel label="Outside light level"

(it happens to be dark right now)

when I try to use this rule:

rule LightLevel
    Item LightSensorCurrentLevel changed
    LightLevelValue = LightSensorCurrentLevel.state
    logInfo ("Lighting rule", "New light level: " + LightLevelValue)
    if (LightLevelValue < 90){
        logInfo ("Lighting rule", "Low light on")
    if (LightLevelValue > 92) {
        logInfo ("Lighting rule", "High light off")

results in the following error:

2018-12-19 23:31:15.967 [INFO ] [smarthome.model.script.Lighting rule] - New light level: 1

2018-12-19 23:31:15.971 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'LightLevel': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_lessThan(int,int) on instance: null

Clearly the value of the item state is not NULL so what am I doing wrong?

I think you have to define a variable with ‘var’ or ‘val’ in front of it, to work properly

val LightLevelValue = LightSensorCurrentLevel.state
1 Like

Thank you. This now works. School-boy error!