Cannot refer to the non-final variable timerCloset inside a lambda expression

  • Platform information: Ubuntu 20.04.01
  • openHAB version: 2.5.11-1

code server debugger is flagging some timers; can someone tell me why?
error: Cannot refer to the non-final variable timerCloset inside a lambda expression(org.eclipse.xtext.xbase.validation.IssueCodes.invalid_mutable_variable_access)

It’s highlighting the lines:
timerCloset.cancel
timerCloset = null

I’m led to believe that error is telling me not to put the cancel or null inside the timer? If so why would you put it outside the timer. I want to cancel it when the timer is done(after it runs).

Not an engineer; dumb it down please :slight_smile:

In the code below:

if (EmergencyRuleShutoff.state == OFF){  

var Timer timerCloset = null
timerCloset = createTimer(now.plusSeconds(120))[|

//do stuff here; where i have all my things to do

timerCloset.cancel
timerCloset = null  

            ]
    }

Timer var must be set outside the rule.

Furthermore, you won’t need nor is there any reason to cancel the timer within the timer itself, as the code is already executed.
You can’t cancel the execution after the code block is already started, so <timervar>.cancel is only to prevent the execution of the Lambda at all.

1 Like

Thanks so much! Appreciate it!