Openhab 2 jython support

I’m very interested in this question too.
Migrating jython rules is the only thing that prevents me from migrating to OH2.

Any guidance to do so ?

Thanks !

Jim

Any news to this topic?

Doesn’t seem to interest a lot of people :frowning:
Weird because jython is quite powerful for advanced rules.

I play around with the new rule engine for java script. You can download the latest nightly build of OH2.
Then you should create a folder automation\jsr223 under your conf directory and put in your JavaScript files.

A good starting point is this repository

do you know if jython support is also available in the latest deb packets in the repository (version 2.1.0~20170519040644-1).

@smerschjo Can you tell us how to set up jython with the snapshot builds?

I just came across this doc written by @lewie: https://github.com/eclipse/smarthome/wiki/Scripted-Rule-Support. This in combination with https://github.com/smerschjohann/docker-oh2-jsr223/blob/master/start.sh got me this far:

2017-05-22 15:00:00.855 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'test.py'

To get this far I did:

  • Install jython to /opt/jython (not really, it is already installed there for OH1/jython)
  • add the following to /etc/default/openhab2
EXTRA_JAVA_OPTS=" -Dpython.home=/opt/jython -Dpython.path=/etc/openhab2/automation/pylib "
CLASSPATH="/opt/jython/jython.jar"
  • add this to /etc/init.d/openhab2 if using SysV init in the section after the USER_AND_GROUP block:
if [ -n "${EXTRA_JAVA_OPTS}" ]; then
  export EXTRA_JAVA_OPTS
fi
if [ -n ${CLASSPATH} ]; then
  export CLASSPATH
fi

I just ran into the next roadblock. I’m currently on:

2.1.0-SNAPSHOT / Build #924

220 | Active   |  80 | 0.9.0.201705120951     | Eclipse SmartHome Automation API                       | org.eclipse.smarthome.automation.api
221 | Active   |  80 | 0.9.0.201705120951     | Eclipse SmartHome Automation commands                  | org.eclipse.smarthome.automation.commands
222 | Active   |  80 | 0.9.0.201705120951     | Eclipse SmartHome Automation Core                      | org.eclipse.smarthome.automation.core
223 | Active   |  80 | 0.9.0.201705120951     | Eclipse SmartHome Automation Module Core               | org.eclipse.smarthome.automation.module.core
224 | Active   |  80 | 0.9.0.201705120951     | Eclipse SmartHome Automation Media Modules             | org.eclipse.smarthome.automation.module.media
225 | Active   |  80 | 0.9.0.201705120951     | Eclipse SmartHome Automation Module Script             | org.eclipse.smarthome.automation.module.script
226 | Active   |  80 | 0.9.0.201705120951     | Eclipse SmartHome Automation Script Globals            | org.eclipse.smarthome.automation.module.script.defaultscope
227 | Active   |  80 | 0.9.0.201705120951     | Eclipse SmartHome Automation Script RuleSupport        | org.eclipse.smarthome.automation.module.script.rulesupport
228 | Active   |  80 | 0.9.0.201705120951     | Eclipse SmartHome Automation Module Timer              | org.eclipse.smarthome.automation.module.timer
229 | Active   |  80 | 0.9.0.201705120951     | Eclipse SmartHome Automation GSON Parser               | org.eclipse.smarthome.automation.parser.gson
230 | Active   |  80 | 0.9.0.201705120951     | Eclipse SmartHome Automation Providers                 | org.eclipse.smarthome.automation.providers
231 | Active   |  80 | 0.9.0.201705120951     | Eclipse SmartHome Automation REST API                  | org.eclipse.smarthome.automation.rest

When loading a non-empty jython file (the test.py above was empty :slight_smile: ) I get this error:

2017-05-22 15:26:03.755 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'test.py': NameError: name 'ScriptExtension' is not defined in <script> at line number 1

…with line 1 being:

ScriptExtension.importPreset("RuleSupport")

Please test to replace ScriptExtension with se or scriptExtension.

se.importPreset("RuleSupport")

or

scriptExtension.importPreset("RuleSupport")

The Property name has changed.

1 Like

Thanks Helmut! Now the script loads!

Helmut, on github Scripted Rule Support · eclipse-archived/smarthome Wiki · GitHub you explain how to setup the IDE as well.

Install in IDE
This is how I got the scripted rule support running within the IDE.
Include the following bundles in the launch config:

org.eclipse.smarthome.automation.module.core
org.eclipse.smarthome.automation.api
org.eclipse.smarthome.automation.provider.file
org.eclipse.smarthome.automation.rest
org.eclipse.smarthome.automation.module.script.rulesupport
org.eclipse.smarthome.automation.module.timer
org.eclipse.smarthome.automation.providers
org.eclipse.smarthome.automation.parser.gson
org.eclipse.smarthome.automation.core
org.eclipse.smarthome.automation.module.script

I do not get where to find the launch config. Is it hidden anywhere in the Eclipse SmartHome Designer Preferences or do you mean the file /usr/share/openhab2/runtime/bin/setenv or …

Same for

Jython
Download jython-standalone-x.y.z.jar from http://www.jython.org/downloads.html

Add this library to the “Bootstrap entries” on the main page of the launch config by simply putting it with its full path there.

Where do I find the “Bootstrap entries” on the main page of the launch config

I’m lost … please help

Hello Bodo,

I do not get where to find the launch config.

In eclipse, go to Run->Run Configurations… In Tab Plug-ins, search for “org.eclipse.smarthome.automation” and enable the needed bundles:

There you’ll find the Bootstrap entries too:

1 Like

Thanks so much for the fast answer! I’ll test it this evening …

Hi all,
I started to play with the Jython rule support: I tryed lo load 2 simple rules, and seems that the second loaded script overwrite the first one:

12:54:11.097 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'jsr223_Script01.py'
12:54:13.065 [INFO ] [smarthome.event.RuleAddedEvent      ] - Rule 'scripted_000000000000001F' has been added.
12:54:13.068 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: INITIALIZING
12:54:13.120 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'jsr223_test.py'
12:54:13.130 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: IDLE
12:54:13.146 [INFO ] [smarthome.event.RuleAddedEvent      ] - Rule 'scripted_000000000000001F' has been added.
12:54:13.156 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: INITIALIZING
12:54:13.161 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: IDLE
12:54:14.022 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: RUNNING
12:54:14.025 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: IDLE
12:54:15.011 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: RUNNING
12:54:15.014 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: IDLE

and I wondering from where the name

'scripted_000000000000001F'

come from?

The scripts are:

1° script

scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleSimple")


class myRule(SimpleRule):
    def execute(self, modules, inputs):
        print "Hello World from Jython"

sRule = myRule()
sRule.setTriggers([Trigger("aTimerTrigger", "timer.GenericCronTrigger", Configuration({"cronExpression": "0/15 * * * * ?"}))])

automationManager.addRule(sRule)

2° script

scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleSimple")


class myRule01(SimpleRule):
    def execute(self, modules, inputs):
        print "BYE BYE"

sRule = myRule01()
sRule.setTriggers([Trigger("aTimerTrigger", "timer.GenericCronTrigger", Configuration({"cronExpression": "* * * * * ?"}))])

automationManager.addRule(sRule)

Thanks in advance.


Anyway, for want to use Jython support on windows, the following line should be added to the file “openhab2 folder\runtime\bin\karaf.bat”

set JYTHON_HOME=C:\jython2.7.0

set EXTRA_JAVA_OPTS=-Dpython.home=%JYTHON_HOME% -Dpython.path=conf\automation\jsr223 
set CLASSPATH="C:\jython2.7.0\jython.jar"

did you enable any special logging? I don’t get any such log messages when I paste your code to a .py file on my OH2 instance:

my actual log level:

openhab> log:get
Logger | Level
--------------
ROOT   | WARN
openhab> 

Scripts are in

openhab2\conf\automation\jsr223

I just found out I have the messages as well, but only in log:display, not in the openhab.log file on disk.

I’m not quite sure because I can not test.
But must Trigger have a clear name too?
aTimerTrigger01 and aTimerTrigger02 etc.?

In the first test triggers had same name.

I modified the 2 script in order to have unique triggers name, but same behavior.

Furthermore, if I modify the script, the log shows that the script is loaded again, but seems that no changes are loaded. I need to reboot openhab to load the correct modified script.
Also, if the file is deleted from the automation\jsr223 folder openhab does not realize it, and the script continues to run.

I’m trying this too with a recent snapshot build and seeing similar behavior. That’s unfortunate because it was working quite well a few months before the PR was merged but I know there were a lot of last minute changes. Has anybody submitted the related bug reports to the SmartHome issue tracker? I’m seeing two issues. Script being loaded multiple times and no scriptUnloaded callback when script is removed (and rule objects are not being removed during unload).

Hello @steve1,
is it this?