Where to find syntax for createTimer command

Tags: #<Tag:0x00007f387378be48>

I have a few IoT things working based on bits and pieces of rules that I found in various places. But I have no idea where to find a complete comprehensive Programming Reference Guide showing openHAB syntax. Furthermore I have no idea how many different script languages are used. I see all kinds of cryptic command examples, and don’t know whether the these commands are openHAB, Java, Extend, Json, Eclipse, JSR233, OSGi or whatever. All these anachronisms make my eyes glaze over. I had no problem learning Arduion IDE and the C+ Language because everything was in one concise Reference Guide. Similarly I ordered a Linux Pocket guide and found everything I needed to know about navigating the Rasberry PI Linux System. I became quite proficient in HTML and PHP by studying their reference manuals. But this OpenHAB stuff is scattered all over the place. I’m so frustrated. Is there a book I can purchase that has all all commands used in openHAB Rules? I do good with reference books.

However… My immediate need right now is the detailed syntax for createTimer. Is it Java, Eclipes, Json? Or is it a 3rd party Library call something like Cron time command

Here is the delay command I am having trouble with.

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

What are the funny vertical bars | in the syntax???

My rule turns light on with motion and after 5 minutes turns it off

Ok,

OpenhHAB uses XTend, based on Java.
The reference is here:

http://www.eclipse.org/xtend/documentation/101_gettingstarted.html

The way to create a timer is as follow:

In a rule file:

var Timer timer = null

rule "whatevet"
when
    Item myItem changed //Use whatever trigger
then
    (if timer === null) {
        timer = createTimer(now.plusMinutes(5), [ |
            //Do the timer stuff
            timer = null // cancel the timer
        ])
    }
end
1 Like

I think you may be missing a closing parenthesis, and could have a pipe too many. Shouldn’t the last few lines be like that?

...
            timer = null // cancel the timer
        ] )
    }
end

I’ve already been there and typed createTimer in the search field and got this:

Your search - site:eclipse.org/xtend/documentation createTimer - did not match any documents.

Yes sorry, Too early in the morning and only half way through my first coffee… Thanks
Post corrected

It won’t be there.
the createTimer is an openHAB action

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

You see… this is my problem. Stuff all over the place. No command index search. I don’t know which reference guide to use. Where Extend ends and where openHAB begins. Then I’m having to deal with MQTT which is not Extend or openHAB syntax and seems to be a language unto-its-own, but runs either like openHAB command or low-level broker pub/sub commands. Then there are things like Cron, which I figured out myself, but seems to be a library running under openHAB or Java??? So confusing.

1 Like

Now I’m really confused after studying the examples. Too many pipes?? Pipes are used in Linus command, not sure of use here? Furthermore I can’t see and difference in the corrected line other than an extra space

] )

vs

] )

Oops this editor stripped out the space.

Good night - late here in Texas

The space at the closing of the lambda doesn’t matter

And

MQTT is not a language, it’s a communication protocol:

The nice thing with openhab is that it is capable to combine or interact with a multitude of technologies, but this brings the effort to dig into each and understand them.

Maybe this helps you with the timer.

Thanks to all of you for the examples and suggestions, but I think everyone is missing the point. All I want to do is be able to help myself. So far I have been unable to do things myself. For every line of text in my Item and Rule files I have had to rely on the Tasmota or openHAb community to supply the command syntax text verbatim. Complicating matters is not knowing the terminology. This inhibits me from asking questions properly. Josas has it right, with his comment "multiple technologies require effort to dig into each and understand them; unfortunately I can’t learn everything at once.

Words used to describe the text that I must type into Item and Rule files all have have names such as Command, openHAB action, MQTT protocol, expressions, statements, scripts, strings, etc etc. They are really the language (for lack of better word) used by openHAB. I often call this the Command Syntax, where things must be entered with the proper format, spacing, commas, periods, case sensitivity, (and those funny | symbols), and then the descriptive item itself, such as “createTimer” which is the subject of my rant.

I have spent a frustrating amount of time digging through Extend Expressions only to be told that createTimer is a openHAB “action”. Now there is another confusing word, “action”, when it is a “command” in my mind, or possibly a subrouting call (or library). How would I know that? There is no master index of commands, only wiki text with examples presented in mesh manner rather than linear learning progression. Whenever I try to look things up myself I find myself in the wrong place. And…Yes I know that MQTT is a protocol, but it has a command language syntax (so to speak) when entering it into a Item file. Whenever I go to the community, I get complicated examples. I can only assume there is no detailed description of the createTimer command with complete syntax other than by examining examples. Once again… Is there a place that describes the createTimer command syntax in detail?

For what it’s worth… More discussion on openHAB: I got sucked into openHAB for the beauty of it’s initial simplicity, the logical things like… Things, Items, and great library of bindings, only to find out that one must become an expert and dig into the bowels of openHAB to do anything useful. It would be great service to others to explain on the home page that openHAB is not for the casual user and that even a technical person must be willing to spend an enormous amount of time to learn.

After 6 months into openHAB I gave up and switched to nodeRED only to find it had it’s complexity under the “shiny hood” just like openHAB. So… Now I’m back to openHAB, somewhat reluctantly. I suppose the fault I see in openHAB is in the documentation, a mesh wiki learning process where nothing is presented linearly. I thought the initial parts about logical items such as Things, Items, Bindings, and Rules was excellent, but after that, things break apart in all directions.

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 |.