[beta testers wanted!] Jython addon w/ helper libraries (requires OH 2.5.x)

Of course! :smile:

[INFO ] [.openhab.automation.jython.Test Rule] - 1. Test switch is NULL
[INFO ] [.openhab.automation.jython.Test Rule] - 2. Test switch is NULL
[INFO ] [.openhab.automation.jython.Test Rule] - 3. Test switch is NULL

That does make a difference… but not as I expected.

UPDATE: I was not very clear. So to clarify, I’ve set the Item to ON in Karaf console and restarted OH. I forgot that this item is not persisted, so it went back to NULL, but nevertheless it is now accessible using items.testitem.
And after ‘flipping the switch’, it obviously shows its state as ON.

1 Like

Thank you for the testing, Ron! I’ve setup a test environment that can reproduce this, though I think you may have some added complexity due to using Docker. I’ve added a commit that will allow the HLs to be used even if no Items have been defined.

1 Like

I am failing setting up Jython with the instructions in the original post.

I am running OpenHABian 1.5 (OpenHAB 2.5) on a Raspberry Pi 4.

I think I have setup everything correctly, but I do not get output from the hello_world.py rule (see below). I have made sure the file ownership is openhab for all files.
This is the file structure I currently have and the content of hello_world.py:

[15:54:46] openhabian@openhab:/etc/openhab2$ tree addons/
└── org.openhab.core.automation.module.script.scriptenginefactory.jython-2.5.0-SNAPSHOT.jar

0 directories, 1 file
[15:55:25] openhabian@openhab:/etc/openhab2$ ll addons/
total 36M
drwxrwxr-x+  2 openhab openhab 4.0K Mar 14 14:50 ./
drwxrwxr-x+ 16 openhab openhab 4.0K Mar 14 15:23 ../
-rw-r--r--   1 openhab openhab  36M Mar 14 14:51 org.openhab.core.automation.module.script.scriptenginefactory.jython-2.5.0-SNAPSHOT.jar
[15:53:08] openhabian@openhab:/etc/openhab2$ tree automation/                                                                                                                                                     
└── jsr223
    └── python
	├── core
	│   ├── 000_startup_delay.py
	│   └── components
	│       ├── 100_DirectoryTrigger.py
	│       ├── 100_OsgiEventTrigger.py
	│       ├── 100_StartupTrigger.py
	│       ├── 200_JythonBindingInfoProvider.py
	│       ├── 200_JythonExtensionProvider.py
	│       ├── 200_JythonItemChannelLinkProvider.py
	│       ├── 200_JythonItemProvider.py
	│       ├── 200_JythonThingProvider.py
	│       ├── 200_JythonThingTypeProvider.py
	│       └── 200_JythonTransform.py
	└── personal
	    └── hello_world.py

5 directories, 12 files
[15:53:14] openhabian@openhab:/etc/openhab2$ ll automation/jsr223/python/core/
total 16K
drwxr-xr-x+ 3 openhab openhab 4.0K Mar 14 15:00 ./
drwxrwxr-x+ 4 openhab openhab 4.0K Mar 14 15:35 ../
-rw-r--r--  1 openhab openhab  648 Mar 14 15:00 000_startup_delay.py
drwxr-xr-x+ 2 openhab openhab 4.0K Mar 14 15:00 components/
[15:53:25] openhabian@openhab:/etc/openhab2$ ll automation/jsr223/python/core/components/
total 52K
drwxr-xr-x+ 2 openhab openhab 4.0K Mar 14 15:00 ./
drwxr-xr-x+ 3 openhab openhab 4.0K Mar 14 15:00 ../
-rw-r--r--  1 openhab openhab 3.6K Mar 14 15:00 100_DirectoryTrigger.py
-rw-r--r--  1 openhab openhab 3.4K Mar 14 15:00 100_OsgiEventTrigger.py
-rw-r--r--  1 openhab openhab 1.9K Mar 14 15:00 100_StartupTrigger.py
-rw-r--r--  1 openhab openhab 1.9K Mar 14 15:00 200_JythonBindingInfoProvider.py
-rw-r--r--  1 openhab openhab 4.2K Mar 14 15:00 200_JythonExtensionProvider.py
-rw-r--r--  1 openhab openhab 2.4K Mar 14 15:00 200_JythonItemChannelLinkProvider.py
-rw-r--r--  1 openhab openhab 2.4K Mar 14 15:00 200_JythonItemProvider.py
-rw-r--r--  1 openhab openhab 2.2K Mar 14 15:00 200_JythonThingProvider.py
-rw-r--r--  1 openhab openhab 1.9K Mar 14 15:00 200_JythonThingTypeProvider.py
-rw-r--r--  1 openhab openhab 1.8K Mar 14 15:00 200_JythonTransform.py
[15:53:29] openhabian@openhab:/etc/openhab2$ ll automation/jsr223/python/personal/
total 12K
drwxrwxr-x+ 2 openhab openhab 4.0K Mar 14 15:48 ./
drwxrwxr-x+ 4 openhab openhab 4.0K Mar 14 15:35 ../
-rwxrwxr-x  1 openhab openhab  383 Mar 14 15:43 hello_world.py*

I created the hello_world.py myself, as I was unable to find the file itself. But I found the content in the documentation. I ran chmod +x hello_world.py:

[15:53:40] openhabian@openhab:/etc/openhab2$ cat automation/jsr223/python/personal/hello_world.py 
from core.rules import rule
from core.triggers import when

@rule("Jython Hello World (cron decorators)", description="This is an example cron triggered rule using decorators", tags=["Test tag", "Hello World"])# [description and tags are optional]
@when("Time cron 0/10 * * * * ?")
def hello_world_cron_decorators(event):
	hello_world_cron_decorators.log.info("Hello World!")

There is a link to the repository (top right) on all of the documentation pages. From there, the hello_world.py example is in Script Examples. But if you did step 7, you should have also copied it to where it needs to go.

What do you see in the logs? If you have not set the logging level (step 3), then you will not see anything. What do you get when you execute this in your console?

log:get|grep jsr223
1 Like

The addons directory should be under /usr/share/openhab2/
It looks like you’ve created one under /etc/openhab2


@DavidR,@5iver: Thank you for your quick replies.

You are right. I actually had it in /etc/openhab2. The instruction in the original post are incorrect then(?!), because it says copy it to:


So it should actually be $OPENHAB_HOME/addons/, because:

[21:34:44] openhabian@openhab:/etc/openhab2/automation/jsr223/python/personal$ echo $OPENHAB_HOME

After copying it there, I now actually get output related to Jython in the logs. An error :sweat_smile::

2020-03-14 19:50:31.849 [WARN ] [org.apache.felix.fileinstall        ] - Error while starting bundle: file:/usr/share/openhab2/addons/org.openhab.core.automation.module.script.scriptenginefactory.jython-2.5.0-SNAPSHOT.jar
org.osgi.framework.BundleException: Could not resolve module: org.openhab.core.automation.module.script.scriptenginefactory.jython [297]
  Unresolved requirement: Import-Package: org.openhab.core.automation.module.script; version="[2.5.0,3.0.0)"

        at org.eclipse.osgi.container.Module.start(Module.java:444) ~[org.eclipse.osgi-3.12.100.jar:?]
        at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:383) ~[org.eclipse.osgi-3.12.100.jar:?]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle(DirectoryWatcher.java:1260) [bundleFile:3.6.4]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles(DirectoryWatcher.java:1233) [bundleFile:3.6.4]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startAllBundles(DirectoryWatcher.java:1221) [bundleFile:3.6.4]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.doProcess(DirectoryWatcher.java:515) [bundleFile:3.6.4]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:365) [bundleFile:3.6.4]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:316) [bundleFile:3.6.4]

Did you add the NGRE in addons.cfg (or via Paper UI? In addons.cfg` it should look like this:


Also, I noticed that the core folder is at the wrong level:

core should be a folder inside python.
EDIT: Below that folder tree I now see the individual directories and there it looks alright. :sunglasses:

BTW, See here for the correct file locations. And post #112 does a decent job of defining the steps in more detail.

Thank you, I think this is fixed now. Try $OPENHAB_HOME/addons.

Thank you so much the reply!

I got it to work with you pointers regarding the ruleengine addon (which I had not installed :crazy_face:) and following the pointer to post #112.

I am still getting started, but at least now I have python scripting running. Yeay! :grinning:

I did and I got it to work with that now after also following the pointers from @noppes123 . I had messed up the path in my post and written $OPENHAB_USERDATA, but as you said it should be $OPENHAB_HOME, so I corrected my post you referenced here.

One more question, which is a bit off topic of setting this up: I have written a python script for controlling by beamer screen (move it up/down). Where would I put this script (which is not a rule), so that I can call it from a rule? A link to the relevant info for getting started would be much appreciated!

I suggest making this into a module and putting it into $OPENHAB_CONF/automation/lib/python/personal/. Or maybe just add it to your script as a function that is called from the rule. If you have more questions about using Jython, please start a new topic (tag it with jython) and I’ll be glad to help. There is some info here.

I’m reporting on a fresh first time install on openhabian/RPi4. hello_world.py runs and the only error in the log was mentioned in January as a non critical error of the OsgiEventTrigger:

2020-04-30 21:38:00.994 [ERROR] [ion.module.script.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/python/core/components/100_OsgiEventTrigger.py': ImportError: cannot import name osgi_triggers in <script> at line number 17
1 Like

Hello, coming from [JSR223] [JYTHON] Jython with requests successful? my question is how to add support for the requests module to this beta jython installation?

I have tried

pip install --target=/etc/openhab2/automation/lib/jython requests

and it installs successfully. However, it seems to make no difference to the jython environment coming with the beta? Once I add import requests I get an error message in the logs saying requests is not available.

Thanks for a pointer.

See this thread for more info on requests. After going to a bit of trouble installing it, I found that are some incompatibilities that made it not useful for what I wanted - authorization. Fortunately @JimT came up with a way to get extra headers into the sendHttpGetRequest. So, the question is what do you want to do with the requests library?

Yes, I had referenced that thread and worked through it :frowning:

requests is what I believe is the most general library for doing HTTP request in python? I have seen the tweaks to sendHttpGetRequest() discussed, but I actually would prefer to not use a proprietary extension for a pretty general topic if not necessary.

I have working python code using requests I’d like to integrate into my openHAB installation. It utilizes POST and GET requests with data and headers. I have tried urllib2 and got it working for python. However, it bombs when using the same script in the jython environment (urllib2 is available in the jython instance coming with the beta). I have tried sendHttpGetRequest() too, but it didn’t work for me either. I’m pretty sure it is about how data and headers are passed into the requests, but I have tried what I could think of…

So that’s the reason I asked for instructions how to install requests to the beta jython.

@HarryS1, I’d be happy to help with this, but please open a new topic for it. When installing requests, use this which will reinstall any dependencies into the location specified…

sudo -u openhab pip2 install --ignore-installed --install-option="--prefix=/etc/openhab2/automation/lib/python" requests

/etc/openhab2/automation/lib/jython means nothing to the beta Jython addon, since it uses a path inside the addon for python.home.

So is the command listed supposed to make requests available in the beta environment or not? I ran it and restarted openHAB in addition to be sure. But it is still not available.

I have one stupid question just to have a shortcut for a solution. When I restart my openhab it loads all DSL rules but does not load my new Python rules from C:\openhab2\conf\automation\jsr223\python\personal When I open and save my rules, they are reloded and works, but I need to do it after all startup. What should I check to make it loaded automated?

This is a bug that was fixed, but will not be included in a release until OH 3.0. However, I posted a jar with the fix for OH 2.5…

1 Like