How to Setup Jython

Tags: #<Tag:0x00007f617dbf3ed0> #<Tag:0x00007f617dbf3bd8> #<Tag:0x00007f617dbf3a98>

Hi Guys,

Im on the latest Milestone on a Openhabian power Pi 3.

I wanted to get into Jython as im currently learning Python anyway

I already found:

Lucid Script step by step

and:

Openhbab Doc

However I already struggle to get jython onto my pi. I tried ap-get however this fails. Do I just need to download the jar file? and if so into which folder?

Thanks guys.

Wahoo!

You can use the standalone jar (that’s what I use) or an install. They can be anywhere, but you need to configure the paths in EXTRA_JAVA_OPTS. I did this in my start.sh…

#!/bin/sh
echo
echo Launching the openHAB runtime...

DIRNAME=`dirname "$0"`

export EXTRA_JAVA_OPTS="-Xbootclasspath/a:${DIRNAME}/conf/automation/jython/jython-standalone-2.7.0.jar \
-Dpython.home=${DIRNAME}/conf/automation/jython \
-Dpython.path=${DIRNAME}/conf/automation/lib/python"

exec "${DIRNAME}/runtime/bin/karaf" "${@}"

I don’t use lucid, but it has a lot of nice things in it to simplify the transition from the Rules DSL. I am also currently finishing up an update that will really simplify rule definition. Should be ready by this weekend.

Please let me know what your challenges are and I’ll work on getting the documentation cleaned up to address those areas. I already have some updates in here.

Hi Scott,

Will this also work with OH 2.3? If so, can both Rules DSL and Jython be used? I would like to use Jython and slowly move from Rules DSL.

Thanks

I cut a branch to keep the older (pre 2.3 build 1319) code archived, and these changes could be made to work with it. But there have been so many changes/improvements to the Automation API, that anyone using JSR223 should really be using a milestone build, at a minimum. The Rules DSL and JSR223 can definitely both be used simultaneously, and multiple languages can be used at the same time too.

Continuing the discussion from How to Setup Jython:

Hello Tomibeck,
you are on the right track. In addition to the reply of @5iver, please also check the following wiki page. There you will also find generic examples for all three JSR223 implementations (JavaScript, Jython, Groovy).

EDIT: Alright, my browser cache was outdated… I still saw your initial question…

Thanks Scott, thinking I may setup my other pi with the latest build and see how everything works out.

@5iver

Okay I was able to install jython now trying to setup

As soona s i put the py files into the jsr223 fodler i get the following errors:

2018-10-05 19:03:05.078 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_StartupTrigger.py'

2018-10-05 19:03:05.082 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error while creating ScriptEngine

java.lang.NullPointerException: null

	at org.eclipse.smarthome.automation.module.script.internal.GenericScriptEngineFactory.scopeValues(GenericScriptEngineFactory.java:59) ~[?:?]

	at org.eclipse.smarthome.automation.module.script.internal.ScriptEngineManagerImpl.createScriptEngine(ScriptEngineManagerImpl.java:88) ~[?:?]

	at org.eclipse.smarthome.automation.module.script.rulesupport.internal.loader.ScriptFileWatcher.importFile(ScriptFileWatcher.java:164) ~[?:?]

	at org.eclipse.smarthome.automation.module.script.rulesupport.internal.loader.ScriptFileWatcher.processWatchEvent(ScriptFileWatcher.java:135) ~[?:?]

	at org.eclipse.smarthome.core.service.WatchQueueReader.lambda$3(WatchQueueReader.java:323) ~[?:?]

	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:?]

	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:?]

	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [?:?]

	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [?:?]

	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:?]

	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:?]

	at java.lang.Thread.run(Thread.java:748) [?:?]

2018-10-05 19:03:05.106 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - could not load script as no engine is created

First, did you install the Experimental Rule Engine in PaperUI> Addons> Misc.?

Second, have you restarted OH since adding the EXTRA_JAVA_OPTS?

yes to both

I’m not familiar with the error you are getting. Also, it looks like you are adding in one of the component scripts, and this particular one will not work yet. I’d leave them out until they are updated. Sorry… you are starting up just as everything is being shuffled. I’m almost getting to the documentation, which is already partially updated in the PR. I plan to push some more updates this weekend.

Are you using the lucid modules? These are definitely out of date, and I’m hoping @RRoe updates them after I get openhab2-Jython cleaned up.

Are you using the standalone 2.7.0 Jython?

Not a problem at all I can easily wait a week or two :smiley:

No lucid modules.

I used the installer jar 2.7 jython

Put in the modules but hold off on the components for now, and turn on debug logging for org.eclipse.smarthome.automation. Use the triggers.py from my PR. Then try a simple script to test it out. Start with the Raw ESH example, then grab the triggers.py, then extensions, then decorator. See if that all works for you. I haven’t tried with a full Jython install, but others have.

I’ve merged the PR, so the repo is updated and I could use some testers before I put together a post. It looks like the documentation needs to be reflowed to start people out at an entry level too.

2 Likes

Hi Scott,

I finally had the time to setup my spare RPI (openhabian image) and used your repo guide to install jython. I haven’t done more than a test script and it’s working but I get several errors when OH restarts.

Also, the jar file had to be placed in /usr/share/openhab2/runtime/lib/boot for the rules engine to start. Error was this: “ScriptEngine for py not available” is this b/c I’m using the openhabian image?

2018-10-08 11:36:30.213 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_DirectoryTrigger.py': ImportError: No module named openhab in <script> at line number 8

2018-10-08 11:36:30.218 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_Esper.py'

2018-10-08 11:36:32.155 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_Esper.py': ImportError: No module named esper in <script> at line number 3

2018-10-08 11:36:32.160 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_JythonBindingInfoProvider.py'

2018-10-08 11:36:32.307 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_JythonBindingInfoProvider.py': ImportError: No module named openhab in <script> at line number 3

2018-10-08 11:36:32.310 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_JythonExtensionProvider.py'

2018-10-08 11:36:35.933 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_JythonExtensionProvider.py': ImportError: No module named openhab in <script> at line number 3

2018-10-08 11:36:35.938 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_JythonItemProvider.py'

2018-10-08 11:36:36.094 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_JythonItemProvider.py': ImportError: No module named openhab in <script> at line number 3

2018-10-08 11:36:36.098 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_JythonThingProvider.py'

2018-10-08 11:36:36.260 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_JythonThingProvider.py': ImportError: No module named openhab in <script> at line number 3

2018-10-08 11:36:36.263 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_JythonThingTypeProvider.py'

2018-10-08 11:36:36.403 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_JythonThingTypeProvider.py': ImportError: No module named openhab in <script> at line number 3

2018-10-08 11:36:36.406 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_JythonTransform.py'

2018-10-08 11:36:36.595 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_JythonTransform.py': ImportError: No module named openhab in <script> at line number 3

2018-10-08 11:36:36.599 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_OsgiEventTrigger.py'

2018-10-08 11:36:39.340 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_OsgiEventTrigger.py': ImportError: No module named openhab in <script> at line number 10

2018-10-08 11:36:39.345 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_StartupTrigger.py'

2018-10-08 11:36:39.486 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_StartupTrigger.py': ImportError: No module named openhab in <script> at line number 4

2018-10-08 11:36:39.489 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/100_ExampleExtensionProvider.py'

2018-10-08 11:36:39.608 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/100_ExampleExtensionProvider.py': ImportError: No module named openhab in <script> at line number 1

Thanks for the repo guide and I hope to have more time for testing soon.

I see an issue… probably what’s causing your problem. Apt installs don’t have a conf directory. Try this for your EXTRA_JAVA_OPTS, and put the Jython jar back. You’ll need to restart again too.

export EXTRA_JAVA_OPTS="-Xbootclasspath/a:${DIRNAME}/automation/jython/jython-standalone-2.7.0.jar \
-Dpython.home=${DIRNAME}/automation/jython \
-Dpython.path=${DIRNAME}/automation/lib/python"

README is updated.

Hi Scott,

Sorry I forgot to mention I didn’t use conf when adding to start.sh. I changed everything to match what you suggested above but only get error “ScriptEngine for py not available” and my test script no longer works. I also did not have a jython dir, like above(automation/jython), but created and placed a copy of the jar file. All permissions are good and also tried coping the jar file to /usr/share/openhab2/addons as well as /home/openhabian, restarting OH each time, but no luck.

Hmmm. If you followed the steps in the docs, then there’s something missing then. I’ll setup an apt install and test it out. Did you install the rule engine?

The modules go into /automation/lib/python/openhab"
The standalone jar goes into /automation/jython/
000_components goes into /automation/jsr223/, but you could skip these for now

image

I’m thinking I should just make a shell script…

Rule engine is installed.

2018-10-08 15:33:28.783 [INFO ] [basic.internal.servlet.WebAppServlet] - Stopped Basic UI

2018-10-08 15:33:34.783 [INFO ] [arthome.ui.paper.internal.PaperUIApp] - Stopped Paper UI

2018-10-08 15:33:34.828 [INFO ] [panel.internal.HABPanelDashboardTile] - Stopped HABPanel

2018-10-08 15:33:34.851 [INFO ] [ebuilder.internal.HomeBuilderServlet] - Stopped Home Builder

2018-10-08 15:33:34.905 [INFO ] [.dashboard.internal.DashboardService] - Stopped Dashboard

2018-10-08 15:33:57.119 [INFO ] [.internal.GenericScriptEngineFactory] - Activated scripting support for ECMAScript

2018-10-08 15:34:02.032 [INFO ] [ebuilder.internal.HomeBuilderServlet] - Started Home Builder at /homebuilder

2018-10-08 15:34:02.156 [INFO ] [panel.internal.HABPanelDashboardTile] - Started HABPanel at /habpanel

2018-10-08 15:34:02.581 [INFO ] [.core.internal.i18n.I18nProviderImpl] - Location set to '30.970544333076077,-88.28427383194284'.

2018-10-08 15:34:03.533 [INFO ] [.dashboard.internal.DashboardService] - Started Dashboard at http://10.0.1.26:8080

2018-10-08 15:34:03.539 [INFO ] [.dashboard.internal.DashboardService] - Started Dashboard at https://10.0.1.26:8443

2018-10-08 15:34:22.179 [INFO ] [thome.model.lsp.internal.ModelServer] - Started Language Server Protocol (LSP) service on port 5007

2018-10-08 15:34:26.864 [INFO ] [basic.internal.servlet.WebAppServlet] - Started Basic UI at /basicui/app

2018-10-08 15:34:27.215 [INFO ] [arthome.ui.paper.internal.PaperUIApp] - Started Paper UI at /paperui

2018-10-08 15:35:30.424 [INFO ] [rt.internal.loader.ScriptFileWatcher] - ScriptEngine for py not available

I can set the log to DEBUG and share, but it only list the contents in 000_components dir.

As mentioned earlier the test script itself worked without errors but on restart I would get the errors in post 14.

Well, the errors mean the scripts can’t find the modules in /automation//lib/python/openhab/, and the ScriptEngine for py not available error means either the Experimental Rules engine is not installed, or Jython is not in the classpath.

Please post the script that works for you. I don’t see how it could have possible worked! I’m baffled. :confused:

What do you get from feature:list |grep ruleengine?

Should be…

openhab-misc-ruleengine                     │ 2.4.0.SNAPSHOT   │ x        │ Started     │ openhab-addons-2.4.0-SNAPSHOT        │ Rule Engine (Experimental)

I used the test script in the doc’s.

from org.slf4j import LoggerFactory

LoggerFactory.getLogger("org.eclipse.smarthome.automation.examples").info("Hello world!")