Timers in rules

Working on rules in openhabian.
I based a rule on this:
Continuing the discussion from Light Timers:

It was a bad idea since the example is clearly bodgy, it doesn’t have the proper parenthetical pairs and is missing commas… :smirk:

The rule I made is thus:

rule "Driveway Motion"
when
	Item mqtt_test changed from "No Motion" to "Motion"
then
	zwave_device_16500637f6a_node12_switch_binary.sendCommand(ON)
	createTimer(now.plusSeconds(30), [ |
            zwave_device_16500637f6a_node12_switch_binary.sendCommand(OFF)
        ]
	)
end

It works as I expect…but:
Questions:

  • Is there documentation on createTimer? I’ve looked, obviously in the wrong places.
  • What does this syntax mean? [ | zwave_device_16500637f6a_node12_switch_binary.sendCommand(OFF) ]
    (I get the OFF command, but the “|” ? And why the “[…]” ? I’m assuming I am missing the syntax because I’ve not discovered the docs for the createTimer…)

Mac

There is a ton of info about timers and rules, Rich Koshak has many great post with examples.

The [ | is used to call for a lambda function.

Here’s a link to get you started. https://community.openhab.org/t/reusable-functions-a-simple-lambda-example-with-copious-notes/15888

This one trips people up all the time. Any “naked” call like createTimer it is an Action so the Actions page is where to look for it for the built in Actions and for separately installed Actions there will be a page under Add-ons.

And it looks like they finally got full text search working in the docs again so you can just search “createTimer” now in the search box at the top and find the link I provided above.

To elaborate on @H102’s response. One can create a “function” in the Rules DSL called a lambda. But a lambda is also an Object meaning you can pass it around, assign it to a variable, etc.

Many of the features of the Rules DSL uses lambdas including createTimer and many of the operations one can perform on a List.

To define a lambda, one uses [ <arguments> | <code> ] as the syntax. If there are no arguments you can skip <arguments> resulting in [ | <code> ]. Unfortunately there are situations where you can use parens instead of the square brackets but I recommend against using that syntax as it obscures what is going on,e.g. MyGroup.members.forEach( i | logInfo("test", "Item " + i.name + " is " + i.state) ).

So

	createTimer(now.plusSeconds(30), [ |
            zwave_device_16500637f6a_node12_switch_binary.sendCommand(OFF)
        ]
	)

means “Schedule the lambda passed to the createTimer function to execute 30 seconds from now.”

Sometimes you will see the lambda defined outside the call to createTimer a la:

	createTimer(now.plusSeconds(30)) [ |
            zwave_device_16500637f6a_node12_switch_binary.sendCommand(OFF)
        ]

This is also acceptable. The language lets you define the lambda that gets passed in a function call outside the function’s parens if the lambda is the last argument to the function. As with the parens, I recommend against using this syntax, at least when it comes to postings on the forum, because it obscures the fact that the lambda is being passed as an argument to createTimer.

1 Like