Where to find syntax for createTimer command

You need to declare the timer variable so that you can cancel the timer after execution.
It also allows you to reschedule it.

timer = createTimer(now.plusMinutes(1), [ |
    Front_Porch_Lights.sendCommand (OFF)
    Front_Porch_Alarm.sendCommand (OFF)
    timer = null
])

The example has not been updated…

Just to elaborate a bit on Vincent’s answer. Declaring the variable is optional. But there are lots of use cases where you will want to be able to interact with the Timer after it’s created:

  • cancel the Timer so it doesn’t execute the lambda
  • reschedule the Timer so it executed the lambda at a different time.

For example, let’s say you want to have a light turn off five minutes after the last detected motion from a motion sensor. You would set a Timer for five minutes and every time motion is detected reschedule the Timer to expire 5 minutes after that. You can’t do that if you didn’t save the reference to the Timer in a variable.

A method is a function. Java uses the term method and since the Rules DSL runs on Java I used that termonology. It is a bit of code you can call and pass zero or more arguments to it.

While studying DSL and other Rules related stuff I stumbled upon this discussion - where @rlkoshak goes out of his way to clarify things, as usual - and it makes me wonder if it is possible to actually pass variables to the lambda as stated in the Xtend description, like:

timer = createTimer(now.plusMinutes(5) [ some | logInfo("rules", "Timer activated with {}", some)

Don’t know, try it and let use know

You can but the lambda gets access to all of the variables and values that exist at the time it is created so why would you need to pass an argument to it? Any thing you could pass to it would already be available to it.

I’d agree - I wrote some Timer code many months ago and wanted to tweak it a bit so wanted some more detail on the methods available. I came to this thread hoping for some answers but don’t find anything that answered the question. After a lot of searching around, looking through code & other posts I found the following and although rather late for the original poster, I’ve added it to the thread for anyone else looking…

It looks like the Timer has come through from the Eclipse smarthome codebase.

The Timer type seems to be based on this
https://www.eclipse.org/smarthome/documentation/javadoc/org/eclipse/smarthome/model/script/actions/Timer.html

It’s created by a static method on the ScriptExecution class
https://www.eclipse.org/smarthome/documentation/javadoc/org/eclipse/smarthome/model/script/actions/ScriptExecution.html

… but someone who knows better please tell me if I’m wrong

Well, despite all the headaches it causes, there isn’t really much to it.

createTimer wants a future time to go off, and a block of code to run.

Once created, a timer has few methods; the self-explanatory .cancel and .reschedule (which wants another future time), and also the much more rarely used .isRunning (referring to code execution, not waiting) and .hasTerminated

What was it that you hoped to find?

Hoped for more methods that might help with what I wanted - but have confirmed they aren’t there so I’m looking at alternative ways of doing what I was after.

[Wanted to provide some visibility of time left on a timer - but looks like I’ll have to keep my own record of when the timer was started to provide the info]

Yep, a common frustration, hoping they make provision in OH3.
I did make a kludge for my purpose, there’ll be other ways too.

If you’re looking for samples of working timers and other patterns, I’ve been working an a product called S.A.R.A.H. that is, in essence, a companion product to OpenHAB that allows you to generate a complete working system with speech, mariadb, internet radio, weather/astronomical data, a fully functioning alarm system and tamper alarm system, etc., which will allow you to manage the entire system through a built-in web site. It has a lot of working examples with timers you can extend. I’m looking to launch it initally in June, so it should be ready in a couple of weeks. If nothing else it will give you a set of working config files that you can tailor as you see fit. You can look at the instructions here… https://rpi-ha.com. The site is not yet complete either, but should also be ready in a couple of weeks.

Cheers

For timers how important is the space between [ and | ?
I just checked mine and I didn’t have a space between them but they seem to be working OK

It used to be necessary, there was a bug.
They are no more needed but I keep them here for consistency.

1 Like

I have registered to the forum and openhab community in october 2019 and today I must say that every word and sentence you wrote is true. Nevertheless I’m very happy that OpenHAB exists after all.

My present smart home experience began somewhere in 2011 with VeraLite, continued with Fibaro HC2 untill about three years ago when the HC2 Z-Wave Controller died after the warrenty had ended and almost 500 EUR became useless because of an software update!

After a 1 year lasting learning curve today I am very happy to have all my Z-Wave Devices which had become useless are up and running with a Z-Wave USB Stick as Z-Wave Controller for which I had to pay 25 EUR. What a differnce, thanks to Open HAB!