Lambda used for Timer does not accecpt parameter

  • Platform information: RPI with Raspbian
    • Java Runtime Environment: Oracle 1.8.
    • openHAB version: 2.4


  • When creating a timer, I want to define the contents of the lambda separatedly (see rule code below)
    • Rules code
var fillTimerActive = false

val onFillTimer = [ 
  Boolean fillTimerActive |
        fillTimerActive = false
        logInfo("Filling is over", "onFillTimer invoked, fillTimerActive = {}", fillTimerActive)
        water_on_off.sendCommand("0")
        Fill_Switch.postUpdate(OFF)
]

rule "High level fill barrel for next irrigation"
...
            fillTimerActive = true // global var defined above
            var maxTimer = createTimer (now.plusSeconds(60), onFillTimer(fillTimerActive))
...

If I omit the parameter with which I want to pass the global variable fillTimerActive I get a Null Pointer exception when onFillTimer wants to access this variable.
Thus I added it the global variable as a parameter to the procedure onFillTimer,
But it tells me a syntax error in createTimer statement:

Invalid number of arguments. The field onFillTimer is not applicable for the arguments (boolean)

How can I implement the lambda for the timer w/o doing it inline?

As you already found out you cannot access (file-)global variables in lambdas, leaving you with either of 2 possibilities:
a) use a virtual item which is an accessible global … (well, sort of) variable
b pass the variable to the lambda like in this example. Note “3” is the number of arguments and the fourth is the return value you always have to have (but does not count as an argument)

val Functions$Function3 <RollershutterItem,ContactItem,NumberItem,Boolean> check_close_shutter = [ shutter, door, percentage |
var Boolean ret  = true
...
ret
]

Hello Markus,

thank you for your quick response.

I was also already thinking about option a) (Virtual Item), I’ll try that first.

Concerning b) I already tried this but as I already said I get a syntax error when trying to pass the parameter within createTimer(…).
So my question is:

What will be the exact syntax in the createTimer context?

The specification in the openhab doc just defines createTimer(<Instant>,<Procedure>).
I successfully defined another procedure outside w/o parameter, i.e. createTimer(now.plusMinutes(10),procName) and it works.
But how may I pass the parameter(s)?

I will be glad to get an advise .

Nevertheless, have a nice Sunday.

   t = createTimer(DateTime) [ |
     ...... code to execute ....
    ]

I suggest you read a couple of examples here on the forum.

Thanks again, Markus.
First: your proposal suing an Item as global worked.
Second: I’ll give your last proposal a try as well.

BR

Uwe Janssen