Where to find syntax for createTimer command

rules
Tags: #<Tag:0x00007fe054e0c7e8>

(Vincent Regaud) #6

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

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


(Bill Richter) #7

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.


(Bill Richter) #8

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


(Vincent Regaud) #9

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


(Vincent Regaud) #10

And


(Vincent Regaud) #11

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


(Josar) #12

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.


(Bill Richter) #13

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.


(Vincent Regaud) #14

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


(Bill Richter) #15

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


(Vincent Regaud) #16

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


(Bill Richter) #17

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


(Vincent Regaud) #18

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

Please use the code fences in the future when posting code


(Rich Koshak) #19

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.


(Bill Richter) #20

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


(Vincent Regaud) #21

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…


(Rich Koshak) #22

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.


(Ron) #23

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)

(Vincent Regaud) #24

Don’t know, try it and let use know


(Rich Koshak) #25

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.