How to Setup Jython

Here are my imports from one of my switch rules:

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

import core.items
reload(core.items)
import core.rules
reload(core.rules)
import core.triggers
reload(core.triggers)

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

I’m not experiencing any issues calling methods on events.

Hi Scott,

events problem was for modules not scripts (rules).

the import required is

from core.jsr223.scope import events

OH updates seem to be removing the JAVAOPTs configuration for loading jython - is there a way to prevent this?

Never happened for me with the EXTRA_JAVA_OPTS in start.sh. Are your in /etc/openhab?

PS- I haven’t forgotten about your PR… holidays and birthdays are done, and will look through it this weekend.

That would seem to be an issue with the Windows installer (IIRC, your platform is a Windows box, right?) I’m running openHABian on an RPi 3B and the apt upgrade command is careful about not replacing my configs without giving me a chance to choose how to handle changes to configuration files. Bottom line, /etc/default/openhab2 (file containing startup options on openHABian) has been untouched since I edited it when I added Jython support to my OH2 system. I’m currently running a recent 2.5.0 SNAPSHOT and have updated a couple times since 2.4.0 STABLE was released.

I have a OpenHABian install on a PI3B.

I have just been editing the openhab2 config file -> /etc/default/openhab2

I have not edited that start.sh file.

I had the same issue when updating from a milestone to 2.4 GA

Weird, I have what would seem to be the same setup as yours, but I haven’t had any problems with an upgrade wiping out my configs. Most recently an upgrade did propose to replace /var/lib/openhab2/etc/org.ops4j.pax.logging.cfg with an updated version, but one of the options provided was to launch a console so I could handle it myself. I then opened both the existing and updated versions of org.ops4j.pax.logging.cfg and reconciled the updates myself.

What is your update and upgrade process? I do the following:

apt update 
apt list --upgradable
apt upgrade

[Update with a notion that just came to mind:
I do not run a GUI on my RPi, I use an SSH session exclusively, i.e., command line.]

Was cleaning up some tabs in my browser, and had one still open to your question, that it looks like I’d forgotten to answer! There wouldn’t be a way to do load this without an import. Later this year, I’ll work on adding most of the Jython helper libraries into a scripting API, so they can be used with any JSR223 language. Something like this will be added into the default scope for scripts.

In the meantime, you may want to use this for logging, which uses a class from the Rules DSL…

from org.eclipse.smarthome.model.script.actions.LogAction import *
logInfo("Rules", "Test logInfo")
logDebug("Rules", "Test logDebug")

I’ve been using this:

It’s almost “automatic” :slight_smile: Just have to remember to use

def execute(event, log):

and not

def execute(event):

Hmmm… you must not be aware of the log attribute (execute.log) that is added to the function when decorated with core.rules.rule. When decorating a class, it will also add self.log to the class instance.

As previously discussed in the forum somewhere, you really should be using unique names for your action functions! In addition to what was discussed, there is possibly a change coming to allow the use of Jython 2.7.1, which will cause some issues with your setup. Specifically, the function log attribute will not work.

@rule("Test duplicate name 1")
@when("Item Virtual_Switch_1 received update")
def testDuplicateFunctionName(event):
    testDuplicateFunctionName.log.info("Function 1")

@rule("Test duplicate name 2")
@when("Item Virtual_Switch_1 received update")
def testDuplicateFunctionName(event):
    testDuplicateFunctionName.log.info("Function 2")

These rules will produce this…

2019-01-28 03:46:06.170 [INFO ] [jsr223.jython.Test duplicate name 2] - Function 2
2019-01-28 03:46:06.198 [INFO ] [jsr223.jython.Test duplicate name 2] - Function 1

No I’m not aware of this. Haven’t updated my jython install in a while.

I remember that discussion well and havn’t used duplicate names since. In that PR they are named execute and execute2 so I think they are not duplicate?

We really need to get versioning, releases, and release notes setup… there has been some cool stuff added!

The execute name looked so generic, I thought you were using it for everything. Sorry… I made a bad assumption. :slightly_smiling_face:

This would be great! At some point last december I decided not to update until the developement cooled down a bit. So all breaking changes would be covered with one update. Think I already sould have done this but other things kept me busy. But with versioning and changelog it would’ve gone different.

No harm done :+1:

1 Like

I found
https://openhab-scripters.github.io/openhab-helper-libraries/Getting%20Started/VS%20Code.html
I managed to setup jython this way. An automatic install would be very much appreciated.
I’m not a linux guy, so, I had to search linux commands… But in the end, I succeeded.

This is on it’s way and can be tested here…

excellent!

I am trying to setup Jython on Openhabian 1.5 (Openhab 2.5) following the instructions here:

I have downloaded this the Jython JAR here, which should be the correct one(?):
https://search.maven.org/remotecontent?filepath=org/python/jython-standalone/2.7.0/jython-standalone-2.7.0.jar

I have setup the folders and paths according as described. However, I do not get any output in openhab.log, when creating the file test.py in automation/jsr223 as described in configuration-testing.

I have also tried enabling debug-output in the Karaf console log:set DEBUG org.openhab.core.automation. But that still does not give any output.

I also tried moving test.py to automation/lib/python/ and automation/jython/, which also appear in the EXTRA_JAVA_OPTS, but that does not change anything.

I restarted Openhab in between by doing sudo systemctl restart openhab2.service.

I have no idea what to try next, so any support would very appreciated. Thanks in advance!

The easiest way to setup Jython is to use this bundle…

It also includes the core and community helper libraries, which you can read about here…

https://openhab-scripters.github.io/openhab-helper-libraries/index.html

In those docs, you can also find detailed installation instructions, if you’d like to manually install everything. The official documentation needs to be updated, but it’s hard to get started with that until some of the details for OH 3.0 become finalized. Shout if you need any help!

I tried to install jython on my dev machine (RPi4 running openHABian) but failed.
Purged OH, installed 2.5.9 and followed the install instructions first.
But I kept getting this error
2020-10-09 11:48:41.152 [INFO ] [me.core.service.AbstractWatchService] - ScriptEngine for py not available

EXTRA_JAVA_OPTS is properly set as you can see below.
I can’t find out what’s wrong. All settings (seem to) match that of my (working) production box.
Any ideas ?

[12:28:48] root@devpi:/etc/openhab2/automation/jython# ps -ef|grep java|grep ython
openhab  10579     1 23 12:29 ?        00:01:51 /usr/bin/java -Dopenhab.home=/usr/share/openhab2 -Dopenhab.conf=/etc/openhab2 -Dopenhab.runtime=/usr/share/openhab2/runtime -Dopenhab.userdata=/var/lib/openhab2 -Dopenhab.logdir=/var/log/openhab2 -Dfelix.cm.dir=/var/lib/openhab2/config -Djava.library.path=/var/lib/openhab2/tmp/lib -Djetty.host=0.0.0.0 -Djetty.http.compliance=RFC2616 -Dorg.ops4j.pax.web.listening.addresses=0.0.0.0 -Dorg.osgi.service.http.port=8080 -Dorg.osgi.service.http.port.secure=8443 -Djava.awt.headless=true -Xms192m -Xmx320m -Xbootclasspath/a:/etc/openhab2/automation/jython/jython-standalone-2.7.2.jar -Dpython.home=/etc/openhab2/automation/jython -Dpython.path=/etc/openhab2/automation/lib/python --add-reads=java.xml=java.logging --add-exports=java.base/org.apache.karaf.specs.locator=java.xml,ALL-UNNAMED --patch-module java.base=/usr/share/openhab2/runtime/lib/endorsed/org.apache.karaf.specs.locator-4.2.7.jar --patch-module java.xml=/usr/share/openhab2/runtime/lib/endorsed/org.apache.karaf.specs.java.xml-4.2.7.jar --add-opens java.base/java.security=ALL-UNNAMED --add-opens java.base/java.net=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.naming/javax.naming.spi=ALL-UNNAMED --add-opens java.rmi/sun.rmi.transport.tcp=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED --add-opens java.base/java.text=ALL-UNNAMED --add-opens java.desktop/java.awt.font=ALL-UNNAMED --add-exports=java.base/sun.net.www.protocol.http=ALL-UNNAMED --add-exports=java.base/sun.net.www.protocol.https=ALL-UNNAMED --add-exports=java.base/sun.net.www.protocol.jar=ALL-UNNAMED --add-exports=jdk.xml.dom/org.w3c.dom.html=ALL-UNNAMED --add-exports=jdk.naming.rmi/com.sun.jndi.url.rmi=ALL-UNNAMED -Dkaraf.instances=/var/lib/openhab2/tmp/instances -Dkaraf.home=/usr/share/openhab2/runtime -Dkaraf.base=/var/lib/openhab2 -Dkaraf.data=/var/lib/openhab2 -Dkaraf.etc=/var/lib/openhab2/etc -Dkaraf.log=/var/log/openhab2 -Dkaraf.restart.jvm.supported=true -Djava.io.tmpdir=/var/lib/openhab2/tmp -Djava.util.logging.config.file=/var/lib/openhab2/etc/java.util.logging.properties -Dkaraf.startLocalConsole=false -Dkaraf.startRemoteShell=true -classpath /usr/share/openhab2/runtime/lib/boot/org.apache.karaf.diagnostic.boot-4.2.7.jar:/usr/share/openhab2/runtime/lib/boot/org.apache.karaf.jaas.boot-4.2.7.jar:/usr/share/openhab2/runtime/lib/boot/org.apache.karaf.main-4.2.7.jar:/usr/share/openhab2/runtime/lib/boot/org.apache.karaf.specs.activator-4.2.7.jar:/usr/share/openhab2/runtime/lib/boot/osgi.core-6.0.0.jar:/usr/share/openhab2/runtime/lib/jdk9plus/istack-commons-runtime-3.0.8.jar:/usr/share/openhab2/runtime/lib/jdk9plus/jakarta.xml.bind-api-2.3.2.jar:/usr/share/openhab2/runtime/lib/jdk9plus/javax.activation-1.2.0.jar:/usr/share/openhab2/runtime/lib/jdk9plus/javax.annotation-api-1.3.1.jar:/usr/share/openhab2/runtime/lib/jdk9plus/jaxb-runtime-2.3.2.jar:/usr/share/openhab2/runtime/lib/jdk9plus/txw2-2.3.2.jar org.apache.karaf.main.Main

So I then installed the addon (still the download link from post #1?) but got

2020-10-09 11:40:30.926 [ERROR] [le.script.scriptenginefactory.jython] - bundle org.openhab.core.automation.module.script.scriptenginefactory.jython:2.5.0.202001221001 (320)[org.openhab.core.automation.module.script.scriptenginefactory.jython.JythonScriptEngineFactory(537)] :  Error during instantiation of the implementation object
java.lang.NoClassDefFoundError: javax/script/ScriptEngineFactory