Openhab 2 jython support

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 https://github.com/eclipse/smarthome/wiki/Scripted-Rule-Support 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?

Thanks.That definitely looks like it could be the cause of the problem. If I can get a clean build with the latest code I’ll try to do some experimentation.

Not sure I understand you right.
Do you know this mini wiki for automation? https://github.com/eclipse/smarthome/wiki/Scripted-Rule-Support

I’ll send you the newest bundles, but these can be found in nightly builds too.

I meant I was going to try to step through code in the IDE debugger, but I need an Eclipse project that will build cleanly. Do you know how to force code generation for the xtext files? I’m seeing some build errors that look like code might need to be regenerated.

The name “scripted_XXXXX” error comes from this change:

That has to be changed, but I’m currently not at my development PC. I will open an issue for that

Thanks @smerschjo, ftr: https://github.com/eclipse/smarthome/issues/3528

Issue is fixed and I have just built a new distro (#930) - it works smoothly with at least a js example:

09:21:24.401 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'example.js'
09:21:25.059 [INFO ] [smarthome.event.RuleAddedEvent      ] - Rule 'rule_1' has been added.
09:21:25.062 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - rule_1 updated: INITIALIZING
09:21:25.096 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'example2.js'
09:21:25.098 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - rule_1 updated: IDLE
09:21:25.175 [INFO ] [smarthome.event.RuleAddedEvent      ] - Rule 'rule_2' has been added.
09:21:25.175 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - rule_2 updated: INITIALIZING
09:21:25.181 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - rule_2 updated: IDLE

So please go ahead with testing and writing docs for this feature!

1 Like

It looks like the file loading (file watcher?) issues are still there. I see my test script loaded once but it is not reloaded when I update it. Helmut thought it might be related to the issue he referenced earlier in this thread.