Variable suddenly null?!

I am struggeling with a strange problem.

I’m trying to fade up a dimmer slowly. Therefore I have the following rule:

var DIMMER_VALUE = 15
var firstSwitchOfDay = true
var firstSwitchOfDayRunning = false

rule "Wohnzimmer tradfri-2 toggle"
when
	Item REMOTE_TRADFRI_2 received update "toggle"
then
    if (firstSwitchOfDayRunning) {
        logInfo(LOGGER, "SoftSwitch running, abort!")
        firstSwitchOfDayRunning = false;
    }    
    if (firstSwitchOfDay) { 
        // At first switch of day, fade from 10 to 70%
        firstSwitchOfDay = false        
        DIMMER_VALUE = 10
        logInfo(LOGGER, "First Switch of Day - slowly starting up")
        firstSwitchOfDayRunning = true
        while (firstSwitchOfDayRunning) {
            logInfo(LOGGER, "Current DIMMER_VALUE: " + DIMMER_VALUE)
            DIMMER_VALUE = DIMMER_VALUE + 1
            logInfo(LOGGER, "Dim: " + DIMMER_VALUE)
            DIMMER_DUMMY_WOHNZIMMER.sendCommand(DIMMER_VALUE)
            Thread::sleep(5000)
        }
    }else if (SWITCH_DUMMY_WOHNZIMMER.state == ON) {
		logInfo(LOGGER, "Tradfri toggle -> OFF")
		SWITCH_DUMMY_WOHNZIMMER.sendCommand(OFF)
	}else{
		logInfo(LOGGER, "Tradfri toggle -> ON")
		SWITCH_DUMMY_WOHNZIMMER.sendCommand(ON)
	}
end

I have a strange behavour in that “firstSwitchOfDay” block. In the first round of the loop everything works fine, DIMMER_VALUE is set to 11.
However during the second round, that DIMMER_VALUE = DIMMER_VALUE + 1 fails. The log states:
2020-04-04 13:54:08.273 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Wohnzimmer tradfri-2 toggle': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_plus(int,int) on instance: null

Any idea why this value is suddenly null? As you see I am logging out the value just one line before that increment, ant this value is output correctly:

020-04-04 13:54:08.265 [INFO ] [rthome.model.script.wohnzimmer.rules] - Current DIMMER_VALUE: 11
2020-04-04 13:54:08.273 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Wohnzimmer tradfri-2 toggle': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.IntegerExtensions.operator_plus(int,int) on instance: null

So it looks like the variable becomes null from one line to the other. Any ideas why this might happen? I have no other rules working in parallel that might set the value to null in the background.

P.S. I know I wouldn’t block a thread when using a timer. I had a timer before but with the same problem on that variable. This loop was just to try if maybe I have a problem with variable visibility inside the timer execution.

Try using a different trigger, something other than received update.

Also here is a tutorial I found that may help with var’s in rules for fading a light.

I’d be a bit suspicious about multiple “copies” of the very long running rule operating in parallel too (on the same global variable).

1 Like