What's the deal with Python rules in OpenHab 3?

Firstup a primer. Look for the Just tell me already!! title down below if you already know all this.

Openhab has included a python rule engine into its UI in OH3. This means you can write and interact with your own python scripts inside the web interface rules section. When you’re building a rule in the UI, one of the options for Then will be Python scripting.

At this point the Python rule engine uses Python version 2.7, so once setup, you’re basically just exploiting your Python 2.7 skillz. How cool :slight_smile:

To get setup, Openhab 3 Python in the UI is very nearly ready to go, but there’s a few parts missing.

To be fair, the Python engine is there in its entirety, so you can write Python code, but there are very few available libraries available to you.

You can absolutely reinvent the wheel yourself and get things happening, but the more common practice is to include standard Helper Libraries that do a lot of the work for you. Certainly whenever you ask someone “How do I…?” they will reference libraries and the functions therein rather than breaking down the code in those libraries.

But fear not, while Openhab 3 doesn’t yet have a bundled set of libraries to work with, some of our diligent community members have picked up the slack.

Just tell me already!!

@5iver has upgraded the old OH2.5 Jython libraries and @JimT has made them available as a Pull Request.

You can find the package here: https://openhab-scripters.github.io/openhab-helper-libraries

And the instructions for installing are here: https://openhab-scripters.github.io/openhab-helper-libraries/Getting Started/Installation.html (The link to the latest package version for download is in Step: 7)

(Actually, I haven’t followed the full list. I found out about all this in non sequential increments so I was desperately just trying to patch. See below for a breakdown of what I did and didn’t do)

Once you’ve got them in, you’ll need to know what the libraries contain and how to use their functions, you can read into that here: https://docs.python.org/2.7/

You’ll also need to know how to access OpenHab components from within your code. Item states for example. You’ll find a nice primer on this here: https://openhab-scripters.github.io/openhab-helper-libraries/Guides/But How Do I.html#get-the-state-of-an-item

I can’t guarantee this is all perfect, but as a noob, I discovered what I detailed above and managed to get a Python scripts working from the UI.

So this exact process worked for me?

I actually didn’t follow the full list from the Jython Libraries Instalation Instructions. I found out about all this in non sequential increments so my process was actually:

  • Install Python Add-on in: UI -> Settings -> Add-ons -> Automation + Jython Scripting

  • Download and extract … you know what, I listed all this in another reply, go look at that if you must know. Its the long one with heaps of code references.

  • Work through the install guide, skipping the bits I didn’t think I had to do

    1. skip
    2. I did this but didn’t have to. It did answer a question in my mind about logging though.
    3. See 2
    4. Skip, come back to it if I need to
    5. Didn’t think I’d need as I already have a Python interface in the UI
    6. Skip. Can do a restart later
    7. Did this. It is a newer version the the other one I downloaded.
    8. Did this, only did /automation/lib/python/, couldn’t locate another configuration.py.example to rename
    9. Did this
    10. Skipped. Thought I might have to come.back to it, but have been assured it is already taken care of in OH3 by the add-on.
    11. See 10
    12. instead of doing this, recreated the same conditions in UI. Basically, you just want to write to the log file every 10 seconds
    13. did a service openhab restart because of not having shut it down in step 6
    14. Not a real step. I think it was included to stop there being just 13 steps. That would be unlucky and invite problems :wink:
    15. see 14

Any Questions?

1 Like