[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.
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.
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/
0 directories, 1 file
[15:55:25] openhabian@openhab:/etc/openhab2$ ll addons/
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
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 * * * * ?")
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?
After copying it there, I now actually get output related to Jython in the logs. An error :
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 
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]
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
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.
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
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.
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?