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

This will be considerably quicker to read than it was for me to decipher and perform, so please enjoy…

SSH into openhabian machine as openhabian

Make a directory to work in.

openhabian@openhab:~$ mkdir -p tmp/py

then: (Download the OH3 version of the openhab helper libraries for OH3, extract them, isolate the ones noted as required on this page and save the whole automation directory and all its children to the conf share directory.

cd tmp/py
wget https://github.com/jimtng/openhab-helper-libraries/archive/oh3-patch.zip
unzip oh3-patch.zip 
mkdir -p exp/automation/lib/python
mkdir -p exp/automation/jsr223/python
cp -vr openhab-helper-libraries-oh3-patch/Core/automation/lib/python/core exp/automation/lib/python/
cp -vr openhab-helper-libraries-oh3-patch/Core/automation/jsr223/python/core exp/automation/jsr223/python/
cp -vr exp/automation /etc/openhab/

Then back in the OH3 UI, create one of the example scripts that came with the download

  • Go* to Rules
  • Create a new rule (+)
  • Fill in the following fields:
    • Name: Jython Hello World (cron decorators)
    • Description: This is an example cron triggered rule using decorators
    • Tags: Test tag & Hello World
    • When: (Add Trigger)
      • Time Event
      • on a schedule (cron)
      • Cron Expression: 0/10 * * * * ?
    • (and that reports back: Every 10 seconds )
  • Done
  • Save

Then in the Then section:

  • Add Action

  • Run Script: Python (2.7)

  • And on the Edit Script screen

    from core.rules import rule
    from core.triggers import when

    def hello_world_cron_decorators(event):
    hello_world_cron_decorators.log.info(“Hello World!”)

I then click Save, then Run Now and get this in the logs

2021-01-23 23:15:00.395 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule ‘a39174d329' with status 'RUNNING'
2021-01-23 23:15:10.400 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule ‘a39174d329' with status 'RUNNING'
2021-01-23 23:15:12.342 [WARN ] [jython.core.log                     ] - The 'configuration.py' file is missing from teh python.path!

I try:

import java.time.ZonedDateTime
var now = ZonedDateTime.now()
if now.isAfter(items["MyDateTimeItem"].getZonedDateTime()):

…as per your

The answer to that question is going to be the same (or very nearly the same) in any of the rules languages.

and get:

[internal.handler.ScriptActionHandler] - Script execution of rule with UID 'd7add55e50' failed: <eval>:1:0 Expected an operand but found import
import java.time.ZonedDateTime
^ in <eval> at line number 1 at column number 0

—edit
I later came back and realised I should have put in a real item. So I amended the script to:

from core.log import logging, LOG_PREFIX
import java.time.ZonedDateTime
log = logging.getLogger("org.openhab.core.model.script.MyLog")
\# Note: removing "var " from the front of this next line stops it from failing
now = ZonedDateTime.now()
if now.isAfter(items["LocalSunrise_StartTime"].getZonedDateTime()):
    log.info("after sunrise")
else:
    log.info("before sunrise")

…and got back

2021-01-24 02:23:27.487 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'tryJython' failed: SyntaxError: no viable alternative at input 'now' in <script> at line number 4 at column number 4

when I had that var in there, and then without it:

Script execution of rule with UID 'tryJython' failed: NameError: name 'ZonedDateTime' is not defined in <script> at line number 4

As you can see, I’m not shy. I’m not trying to get someone else to do all the work for me. I’m working with the information I have at hand and am absolutely stuck.

Your best source for “how do I do X in Python” will be looking at the Helper Library docs and the Helper Library code (if you don’t want to use the Helper Libraries themselves).

My best way of doing this is finding someone who can commit to an official source and I can go from there.

Weeks I have been pouring through discussions and I am still no closer to being able to run the most elementary scripts. I was doing fine with OH2.5 but when I upgraded because persitance just absolutely refused to work, and because I was assured it was officially upgraded, I’ve hit nothing but roadblocks in trying to write automation.

I’ll write up documentation myself if no one else has time, but seriously, I can’t find anything solid to teach.

It’s far easier to answer specific questions than to just dump the [javadocs](http://www.openhab.org/javadoc/latest/) on you.

Not at all. Have you heard the old “teach a man to fish” theory?

I’ve considered javascript, but havent even worked out how that would work yet. I might be stuck in the “but Javascript is client side” paradigm.

Except for the ItemRegistry (ir, Item states are available through the items dict), just about anything from openHAB you’ll need to use will need to be imported. That’s the sort of thing the Helper Library takes care of for you.

How about a short page with a crash course on library importing for Openhab 3? I can’t be the only one finding it difficult to just get things going.

When I try to apply the logic that its all Python and Java anyway so it shouldn’t be a problem that I’m in a particular environment, I find there’s enough issues coming up that Google has never ever heard of that its clearly not a straightforward implementation.

I come from a Javascript, CSS, PHP, SQL, Basic, VBasic, etc world where life s good because everything is documented. If I want to know how to do something, I describe it in my head and then find the way to describe it in my chosen language. References are plentiful and very verbose. Forums are full of people who will give you absolute answers to your questions, and who were baptised by fire in the coding forums where useless: why do you want to do that when… questions are flamed mercilessly.

I wish I had time to go down every dead end I find. I’ve been doing it a lot and I’m learning a lot about what doesn’t work, and often why, but I’ll be 60 in 11 years and I don’t have all the time in the world left to me.

I posted this topic last night, my time. I woke up today to your response and wanted to take my time going through everything before I got back to you. That then stretched out to working out what a PR is, wondering why you’re calling it a PR when I don’t think it actually is, wondering why it took until today before I found out that PRs existed, trying the suggested “PR”, searching to make sure the one I found that is actually called something entirely different is in fact what you’re suggesting (I still can’t be sure), documenting everything I did, addressing some of the off topic points that came up, spell checking, formatting, posting… and still I am no closer :frowning:

OpenHAB could be massive. I chose it because it showed the best potential for the future. But if we can’t make people understand how it works, it will only ever be a weird program that annoys hobbyists.