How to Setup Jython

Tags: #<Tag:0x00007f616d8a7e58> #<Tag:0x00007f616d8a7d90> #<Tag:0x00007f616d8a7cc8>

Found the issue, the correct import is

from core.jsr223.scope import events

I am having trouble importing events into a module.

from core.osgi import events 

but events.postUpdate does not work.

AttributeError: 'module' object has no attribute 'postUpdate'

Am I doing the import correctly?

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!