Is it possible to return from a function only after timer is ended?

I want to implement reusable timers within a function. The function should return after timer is ended not before. Any idea?

my code with as lot of debug, sorry:

var HashMap<String, Timer> RandomTimers = newHashMap()

val Functions$Function2<GenericItem, HashMap<String, Timer>, String> randomTimerLambdaTest= [ s, RandomTimers  |
    logInfo("TIMERCHECK", "lambda before timer: " +

    RandomTimers.put(, createTimer(now.plusSeconds(120))
        logInfo("TIMERCHECK", "lambda TimerEnded: " +
        RandomTimers.put(, null)

    logInfo("TIMERCHECK", "lambda after timer:" +
    s.state.toString + " logged"

rule "Time random swRandomTimer02 start"
        Item swRandomTimer02 changed to ON
        var String result=randomTimerLambdaTest.apply(swRandomTimer02, RandomTimers)
        logInfo("TIMERCHECK", "randomTimerLambdaResult: " + result)

and the log:

2018-02-05 16:34:32.032 [INFO ] [se.smarthome.model.script.TIMERCHECK] - lambda before timer: swRandomTimer02
2018-02-05 16:34:32.046 [INFO ] [se.smarthome.model.script.TIMERCHECK] - lambda after timer:swRandomTimer02
2018-02-05 16:34:32.046 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimerLambdaResult: ON return form lambda
2018-02-05 16:36:32.047 [INFO ] [se.smarthome.model.script.TIMERCHECK] - lambda TimerEnded: swRandomTimer02

I can see, that the function returns right after creating timer. Is there a way to return only if timer is ended?

The answer is yes, Thread::sleep.

But you really don’t want to do this. Any sleep longer than half a second is going to increase the likelihood that you will run into problems of your Rules locking up or becoming laggy.

You should create a Timer and put the code that is supposed to run later inside the body of the Timer and not block the execution of your Rule.