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?
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!
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”.
Your rule has no triggers. Under what events do you want it to run?
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.
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.
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?
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);
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.