Yet another Timer question

So I have been reading a lot on Timers and I have tried every example and I still can’t seem to enter the timer.

I get to the " logInfo(“In Init------>…” but never the logInfo("In Init1------>…"
I’m running OpenHab 2.0.1

var Timer timerActive = null
rule "Init"
System started

// let openHAB settle and give other rules the chance to fire
logInfo(“In Init------>timerActive”,“Timer=”+timerActive)

    timerActive = Timer.createTimer(now.plusSeconds(1),[|
                    loginfo("In Init1------> ","In Timer")
                    if (garage_sw.state == "NULL"){
                            logInfo("In Init2------> ","setting deffaul values")



Execution of startup rule ‘Init’ has been postponed as items are still missing: An error occurred during the script execution: The name ‘.createTimer(,)’ cannot be resolved to an item or type.

IF I compare this to my timers, this part:

should probably read:

createTimer(now.plusSeconds(1)) [|

Note the extra parenthesis and the missing comma

Do you use Designer? It will easily help with such syntax issues.

Oh and I think you need a declaration in the same file but outside your rule block that reads

var Timer timerActive

Just saw you had that…code fences would make your rules easier to read

I tried both syntax as I saw implementations using both. Still no Joy. I know “now” is valid… not sure what is causing this to not recognize .createTimer

Sorry, this was not resolved. New to the forum and was trying to “like” a comment. :frowning: ugg

Not sure whether you saw, but I can call my timers

timerActive = createTimer(now.plusSeconds(1)) [|

For clarity: Timer. createtimer: do not use Timer here
When I tried your syntax, Designer (see previous link) gives me already on the screen the error that .createTimer cannot be resolved for Timer; would strongly recommend using it

Thanks!!! That was it.

Actually, OP’s syntax for that part (i.e. , [|) is valid and actually my preferred way to create timers. It’s long and technical that I’ve written it up before and won’t go into here. The tl;Dr is the stuff in [| ] defines a lambda. Lambdas are a way to pass a function around to methods like any other object or primitive (a lambda is in fact an object). The syntax shortcut that lets you write creatTimer(sometime) [| code] hides that fact. IMHO it is a bit of syntactical sugar that saves a few keystrokes but obscures what is really going on so I don’t like it.

And now to bring up a little background as to why Timer.createTimer didn’t work. In another weird language decision (note that both of these decisions were made by Xtext, not the OH developers) one must use :: to access static methods and data members of classes. So the following probably would have worked:

Timer::createTimer( ...

The Rules DSL does some magic behind the scenes so that Timer, all the core classes, Joda DateTime, and Actions are imported by default so you don’t have to put the class name in front. This is why we can use createTimer without the Timer in front.

But for other Java classes that won’t be the case. Common examples include:

  • Thread::sleep
  • Math::round
  • Math:rand
1 Like

My suggestion: timers are fiddly, prone to bugs (unless you are very careful), and therefore a bit rubbish. Using the expire binding instead is much more elegant and efficient: see here

1 Like