This little tutorial might be for you if you want to get started doing some Jython scripting using lucid. Even if you are happy with Xtend, just give it a try, it won’t hurt to widen the horizon sometimes.
- openHAB version 2.3 or later
- The Experimental Rule Engine add-on must be installed in openHAB
- JSR223 Jython Scripting
- lucid, an openHAB 2.x jsr223 Jython helper library
It’s assumed that you already have some very basic python knowledge (or at least that you are very willing to learn), you have set up an editor (VSC is recommended but vi if you insist, gedit or any other editor will probably also do) for editing your scripts and you know where to put them. You should also know how to watch the debug output.
Before we start, define 2 openHAB switch items and name them “My_TestSwitch_1” and “My_TestSwitch_2”
Now, we start with a very basic script, full of comments and we add functionality to it step by step. Copy this script and save it by the name stepByStep.py in your jsr223 script folder (E.g. something similar to /etc/openhab2/automation/jsr223 )
Your script will look like this:
# The lucid step_by_step script will take you through some very basic steps using lucid and Jython # This is the starting script and we will slowly add some complicity. from lucid.rules import rule, addRule # Needed for defining and adding a rule from lucid.triggers import ItemStateChangeTrigger # Import the different type of triggers that you will be using from logging import DEBUG, INFO, WARNING, ERROR # Importing a few constants needed for setting the debug level # First we add a rule that triggers upon the change of any of 2 switches. # To run this, you need to define 2 openHAB switch items and name them "My_TestSwitch_1" and "My_TestSwitch_2" # Add them to a site map so that you can operate them and watch the debug output. # You will find that the script won't trigger when My_TestSwitch_2 when changes from ON to OFF @rule class StepByStep(object): # Giving the class a unique name def getEventTriggers(self): return [ ItemStateChangeTrigger('My_TestSwitch_1'), # Triggering when the switch changes its state. ItemStateChangeTrigger('My_TestSwitch_2', 'ON'), # Only trigger when switch turns on ] def execute(self, modules, inputs): self.log.info('One of the test switches has changed its state') # That's all we do addRule(StepByStep()) # Needed to add the rule, use the same name as defined for the class
Now, place your newly created switches on a sitemap and try toggling them and watch the debug output.
If You have any comments or questions please feel free to post them here in this thread. I also appreciate if you’d just like to leave a comment that you have joined. Then I will know that this isn’t yet another one of my many monologues (A message should never have more than 3 smilies)
Next step will be in my next post. We will add some functionality to the script so that it becomes aware about which one of the switches that triggered the script.
It will probably take a day or two. (My next post)
You might want to watch the thread but others will probably reply to it too.