How to start rule programming in OH3?

Tags: #<Tag:0x00007fc8fbb7a7c0> #<Tag:0x00007fc8fbb7a680> #<Tag:0x00007fc8fbb7a568>

Dear all,

I just started my new OH3 setup today after two years of OH2. I somehow made it through the old rules system, mainly by studying rules of others.

Now OH3 leaves me completely helpless, feeling like an idiot when it comes to Python rules. I simply wanted to program a switch that is set to ON after 3sec. Clearly, this requires a little script, and I want to trigger it within one of the new graphical rules.

I fail even at the very beginning.

I copied some code to the script within the graphical rule editor that should at least give an info somewhere in the logs and - I hope - in the event monitor.

from org.joda.time import DateTime
from core.rules import rule
from core.triggers import when
from core.actions import ScriptExecution

@rule("my First Rule", description="A first rule", tags=["Tag1", "Tag2"])

def my_rule_function(event):
    my_rule_function.log.info("Hello World!")

But when I press the “Play” button, simply nothing happens. How is debugging intended to happen in OH3? And could someone propose a piece of code to start from for a bloody beginner?

And are the helper libraries (https://openhab-scripters.github.io/openhab-helper-libraries) already included in OH3 basic installation?

Or would someone give me a hint to do the delayed switching in Blockly? I could not find a timer there.

Sorry for giving dumb questions, but OH3 seems to have turned into an expert tool when it comes to rules. I know the @rlkoshak does an excellent job in preparing documentation, but a tutorial to a very first script would help me a lot!

Thanks a lot for helping :slight_smile:

1 Like

Your old rules should still work from their *.rules files, with the exception of the features listed in the release notes (scroll to Rules).

You don’t have to use Python - the old rules syntax is also available, as is JavaScript (ECMA) and Groovy.

Does this help:

I know. But I never became an expert for the old rules engine, so I makes sense for me to jump on the Python train, I think.

1 Like

I am also on the Python train in openHAB2, but the helper libraries for Jython haven’t made it across to OH3 yet, so if you want something working today I would suggest old syntax (DSL) or Javascript.

I should add that - whatever crap I produce in the graphical Python editor - I never receive any error. The status is “idle”, then “running”, then “idle”.

A few things here.

  1. Joda DateTime is no longer there in OH 3.
  2. Did you install the Python add-on?
  3. Your rule has no triggers. Under what events do you want it to run?
  4. When you create a Rule through the UI, all the rule stuff is already handled. You don’t need to define the rule nor the triggers. So all you need is the logInfo. That example you found, (and all of the Python examples you will find for now) are for rules saved to conf/automation/jsr223/python/personal. Not UI rules.

I’ve not done it in Python yet but your Script Action in the UI created rule would basically just be something like:

from org.openhab.core.model.script.actions import Log
Log.logInfo("Hello Test", "Hello World")

That @rule decorator does some magic behind the scenes to give you access to the logger. Since the decorator isn’t used in the UI you have to get the logger yourself. The above gets the openHAB logger actions.

But notice, no imports from core which won’t be available until the Helper Libraries are ported to OH 3. No import of Joda since Joda has been replaced with the Java Time classes. You don’t even need to define the function.

  1. Pretty much all of the examples of Python you will find use the Helper Libraries. The Helper Libraries are not yet ported to OH 3.

Neither Python nor the Helper Libraries come with OH 3 by default. You have to install them as add-ons. The Helper Libraries are not yet available in OH 3.

If you are OK with JavaScript, here is a post that links to everything I know that is written that would be of help. New openHAB3 rules Tutorial - #6 by rlkoshak

Looking in openhab.log?

@rlkoshak Thanks a lot!

Yes, I installed the Python add-on. And indeed, I am doing this in the UI, so this is why I did not add a condition.

But let’s switch to a JavaScript implementation of my simple timer.

My problem is that most examples you gave seem to refer to writing classes or functions, except for the logging example in UI which indeed was the most useful regarding the overall approach. I simply want to write some simple lines to create a timer, compare or log some variables, or send a command.

What is the set of functions I can use already, and what is their syntax in JavaScript? Is there a reference?

And how would example implementations look like in UI? E.g., do I have to do all these imports in a UI script?

I made a patch for the python helper libraries to work on OH3. note the helper libraries are mainly for file based rules, not gui based.

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);
1 Like

it seems none of my old rules are working. Do I need to convert them?

For example:

rule “Velbus button to Hue Bureau lamp”

when

Channel “velbus:vmbelo:f0b42c6c5d:27:input#CH3” triggered PRESSED

then

if (BureauSonosLightSwitch.state == ON)

{ BureauSonosLightSwitch.sendCommand(OFF)

}

else

{ BureauSonosLightSwitch.sendCommand(ON)

}

end

If you want to easily write rules in python (3) you should check out HABApp.
It’s a python rule engine which focuses on simplicity and provides type hints and auto complete if you use an IDE. Together with PyCharm as an IDE it’s an awesome and fun way to create automation rules.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.