Where to find syntax for createTimer command

No, it has a binding syntax described in detail in the binding documentation.

Yes: https://www.openhab.org/docs/configuration/actions.html#timers

The website has been updated recently and a number of people are working on the docs.
There is enough in the existing documentation to build a home automation system already.
If you feel there is something missing please highlight it.

Thanks

The following is not working properly:

  1. rule “Front_Porch_Motion”
  2. when Item Front_Porch_Motion received update ON
  3. then logInfo(“RULES”, “Sound Alarm and light for 1 Minute”)
  4. Front_Porch_Alarm.sendCommand (ON)
  5. Front_Porch_Lights.sendCommand (ON)
  6. createTimer(now.plusMinutes(1)) [| Front_Porch_Lights.sendCommand (OFF) ]
  7. Front_Porch_Alarm.sendCommand (OFF)
  8. end

The alarm beeps momentarily and drops out, whereas the light properly stays on the intended one minute. I tried to put the alarm off command behind the light off command in line 6 like this:

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

but when I added it with a comma separator I get an log error

‘,’ expecting ‘]’

How do I add muntiple commands inside the timer braces [ | …]? I still don’t know what the | symbol does.

Another question; lines 2, 3. and 4 are of the style

do this
do this
do this

Could they be comma separated on one line like"

do this, do this, do this

Like this:

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

The space after the [ and before the | is important!

No

Thanks vzorglub,
While you were typing I figured out this work around:

rule “Front_Porch_Motion”
when Item Front_Porch_Motion received update ON
then logInfo(“RULES”, “Sound Alarm and light for 1 Minute”)
Front_Porch_Alarm.sendCommand (ON)
Front_Porch_Lights.sendCommand (ON)
createTimer(now.plusMinutes(1)) [| Front_Porch_Lights.sendCommand (OFF)]
createTimer(now.plusMinutes(1)) [| Front_Porch_Alarm.sendCommand (OFF) ]
end

Will try your sugestion tomorrow, thanks again

@highvoltage
I am always happy when people find their own solutions!! Well done

Please use the code fences in the future when posting code

There is a fundamental misunderstanding about how OH works.

There is the Rules DSL. Vincent posted a link to a reference for that. Though the Rules page on the docs is probably the more appropriate first stop because you are not having issues with just the Rules, but fundamental understanding of OH.

There are extensions to the base language that add commands to the language. These extensions are called Actions. createTimer is one example of just such an extension. You won’t find it in a reference to the language because it isn’t part of the language.

OH integrates and allows users to work with 330+ technologies and APIs. Each and every one of these technologies and APIs work differently in how one configures and uses them in OH. It’s the fragmented reality that we live in in this problem space. If you are expecting to be able to use something that can bridge between 330+ APIs you have to understand at least a little bit about the API you are working with or you will be disappointed.

MQTT isn’t a language. It’s one of those 330 APIs that OH integrates with. The publish Action you found is an extension to the Rules DSL and is one of the ways you can integrate with MQTT. You have to install it and the documentation for this extension is under Add-ons Actions MQTT.

If you want to use a library to talk to a DHT22 sensor from Adafruit, you don’t expect to find the docs for that library in Straustrup’s C++ Reference? There same applies here. Actions and Bindings are like that library.

OH is a while ecosystem, it’s not just a programming language. If you try to approach it like it is just a language you will be disappointed.

Have you spent any time looking at the openHAB docs themselves? Because really you are not looking for a language reference, you are looking for an openHAB reference and openHAB is so much more than just it’s Rules DSL.

At this stage you should be looking at the Beginner’s Tutorial (incomplete but better than nothing), the concepts section of the docs, and the Rules section of the docs. From these you should understand what a Thing, Channel, Item, Binding, Sitemap, Persistence, and Action are. You should also understand how to structure your rules file syntactically for the most part. The Xtend docs are linked to from the Rules page and a few users find use in it, but the vast majority never look at it nor need to.

You should also be looking at the Demo config and playing around with that too start with.

If after going through all that if you still don’t know where to look then issues need to be filled.

In that frustrating amount of time in the Xtend docs have you read the lambda page yet? The [ | ] are used to define a lambda, a method that is encapsulated in an object you can pass to another method, like createTimer.

Part of the problem is no one has stepped up to write this sort of tutorial. The Beginner’s Tutorial is intended to be this but it’s incomplete.

Go down to the Timers section.

≥ createTimer(AbstractInstant instant, Procedure procedure): schedules a block of code to execute at a future time

instant is usually a DateTime calculated using the built in variable now.
procedure is the block of code and is defined using lambda notation (i.e. square brackets)

Ok, it says it right there, the square brackets define a lambda.

Lambdas are a core party of the underlying language. You don’t expect a library to document how to write an if statement so we go to the base reference and find

http://www.eclipse.org/xtend/documentation/203_xtend_expressions.html#lambdas

As you might have guessed, a lambda expression is surrounded by square brackets (inspired from Smalltalk). Similarly to a method, a lambda …

This is the nature of ALL home automation systems. There are thousands of APIs out there. They are incompatible. They reach have their own quirks and requirements. They reach work in their own unique way. States like OH and NodeRed can only do so much to hide that complexity. If you want simple you need to stick to a commercial system that supports at most a handful of technologies.

Ok, the brackets define a lambda. A lambda is a method, i.e. a function, i.e. a block of code you can pass to another function. In this case you are defining a method to be called at a later time.

Since it’s a function you just list all the “commands” you want to execute like any other block of code. We usually treat the square brackets the same as curly brackets and indent the code to show context.

So your Timer would be:

createTimer(now.plusMinutes(1)) [ | 
    Front_Porch_Lights.sendCommand (OFF)
    Front_Porch_Alarm.sendCommand (OFF) 
]

Since a lambda is a method, it can have arguments. The | separates the arguments from the code to be executed. Most of the time we do not use arguments with Timers because, as the reference says:

A lambda expression also captures the current scope. Any final local variables and all parameters that are visible at construction time can be referred to from within the lambda body.

This means in the way we use lambdas in createTimer we usually don’t need to pass anything to it because it already had copies of the local context. There is a way to create a timer with an argument but I’ve never used it and don’t recommend doing that so have forgotten how. There are examples on the forum though.

1 Like

Thanks rlkoshak for your generous time to explain things. I comprehend 99% of it, except for when I take a close look at Timer Action. I can make the basic timer work for me like:

 createTimer(now.plusMinues(5))

and I understand the lambda thing now ,
but the purpose of declaring the variable (which *I’m not using, seem optional) does not gel.
Also the new term “methods” has me confused (…supports the following methods")

In the wiki example there seems to be a ) missng… I think there should be two of them after the 5, like 5))

Also the wiki example seems to have the space missing before the |.

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!