How to start rule programming in OH3?

As documented in [wiki] Getting Started with OH3: rewriting the tutorial - 8. Rules create a new Rule through the UI along with the rule triggers and conditions.

Then create a Script Action and choose the language. The rule already exists, no @rule decorator needed. The triggers already exists, no @when decorators. You just write the code that runs when the rule triggers as demonstrated in [wiki] Getting Started with OH3: rewriting the tutorial - 9. Make everything work together: rules (advanced).

OH 3 Examples: Writing and using JavaScript Libraries in MainUI created Rules has a section called “A Few Rule Examples” which also show examples of Rules. The stuff under script: >- is the code entered into the Script Action.

Also at OH 3 Examples: Writing and using JavaScript Libraries in MainUI created Rules, and at Experimental Next-Gen Rules Engine Documentation 5 of : Actions which is third page in the series I pointed to for Experimental Next-Gen Rules Engine Documentation 3 of : Your First Rule has examples of creating a Timer.

Experimental Next-Gen Rules Engine Documentation 3 of : Your First Rule is also a good place to look for answers to a lot of your other questions as well.

In general, if it has anything related to defining the Rule, you don’t use it. If you have to import it in text based rules, you have to import it in a Script Action.

So a Script Action that sets a timer would be something like:

var callMe = function(){
    // stuff to do when the timer goes off
}

this.ScriptExecution.createTimerWithArgument(ZonedDateTime.now().plusMinutes(1), callMe);
2 Likes