Port JSR223 bundle to openHAB 2

Tags: #<Tag:0x00007f182a7a2710> #<Tag:0x00007f182a7a24e0>

(steve1) #61

If you change the script timestamp after the first script load (Linux touch command or edit-and-save), does the script successfully load?

(Martin Eskildsen) #62

Also Raspberry Pi 3. I started my experiments by adding the extra opts in the /etc/default/openhab2 file. I noticed that the file didn’t exist, so I created it. But when I start it doesn’t load Python (the appropriate log líne is missing).

(Martin Eskildsen) #63

No problem.

I am wondering why it doesn’t catch up the settings from /etc/default/openhab2, is it because i am installing from nightly builds (using wget / unzip and then copying config I want to preserve)?

(steve1) #64

I think the /etc/default/openhab2 file is only loaded when openhab2 is installed using the automated installers rather than manual installation. I usually look for the shell script that starts the JVM and look for any environment variables that are used to build the JVM option list. I’ve been using the /etc/default/openhab2 file with the installers because AFAIK that file is not overwritten by an upgrade. For experimentation, you could just edit the start shell script directly.

As we find the right way to do this for various installation methods, operating systems and so on we should update the related documentation at docs.openhab.org.

(Martin Eskildsen) #65

I have now done some more research.
I have managed to get it working using the EXTRA_JAVA_OPTS approach as @steve1 proposed. I am now able to use a python module, which itself is argument enough for me, to do this small edit of the setenv file (I am aware this isn’t a good solution :-))
I still get problems with errors from python until I touch the python scripts in the conf/automation folder.
Status is that I have managed to get something up and running, I have even downloaded the openhab2-jython lib @steve1 has contributed and I am planning to use that (it looks like it can do most of what I will need out of the box).
My goal is to get python scripts up and running in oh2, so that I can (FINALLY) kill my oh1 instance.

Thanks for this excellent and long awaited bundle!

(steve1) #66


Same here. As of this morning, I’m running on OH2 (snapshot) so we’ll see how it goes.

(Axel Ruder) #67

If I touch the script, it is being loaded again and apparently with no error, but unfortunately the rule is not executed…

(steve1) #68

Do you have debug (or trace) logging turned on for org.eclipse.smarthome.automation? If not, that might show some messages that explain why the script is not being loaded. Is this a simple “Hello World” logging script or something more complex, like a rule definition. If it’s the latter, I suggest trying to get the simplest possible script working first.

Also, you might be seeing a Groovy-specific issue. Loading Jython in the bootstrap classloader sidesteps the many potential issues with the OSGi class loading restrictions. Is Groovy being provided by an OSGi bundle?

I currently have numerous Jython rules and other functionality running in an OH2 snapshot installation so the core scripting architecture appears to be working.

(Axel Ruder) #69

Hurray, it works!!!
I have to touch the file after restart, but then it works!
Actually it worked before, but I somehow expected the “Hello world from python” to appear in the logs… but of course it is just printed to stdout. I activated the debug log as you suggested @steve1and saw that the rule is being executed.
Thanks a lot for your help, I will now try to do something more sophisticated :grinning:!

(Jürgen Wissing) #70

I also got jython in openhab running and the simple test.py is working.
When trying to define rules i’m seeing some unexpected results when i update a rule.
For one update i get two rules in PaperUI. I can disable one not working one but can not delete the rule (404 http response.
I get this reponse also when i try to delete the rule trying to use the REST API.

Here are the corresponding log records at time of updating the script

14:33:10.708 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'OnItemUpdate.py'
14:33:10.873 [INFO ] [smarthome.event.RuleAddedEvent      ] - Rule 'rule_14' has been added.
14:33:10.877 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - rule_14 updated: INITIALIZING
14:33:10.894 [DEBUG] [pse.smarthome.automation.module.core] - ServiceEvent REGISTERED - {org.eclipse.smarthome.core.events.EventSubscriber}={event.topics=smarthome/items/Z_Socket_2_Power/*, service.id=398, service.bundleid=238, service.scope=singleton} - org.eclipse.smarthome.automation.module.core
14:33:10.899 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - rule_14 updated: IDLE
14:33:10.901 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: OnItemUpdate.py
14:33:10.903 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - in queue: {donotuse=[file:/etc/openhab2/automation/jsr223/sample.js.donotuse]}
14:33:10.905 [INFO ] [ort.shared.ScriptedAutomationManager] - removeAll added handlers
14:33:10.907 [WARN ] [ore.common.registry.AbstractRegistry] - Could not remove element: null
        at java.lang.String.replace(String.java:2240)[:1.8.0_131]
        at org.eclipse.smarthome.automation.events.RuleEventFactory.buildTopic(RuleEventFactory.java:167)[235:org.eclipse.smarthome.automation.api:]
        at org.eclipse.smarthome.automation.events.RuleEventFactory.buildTopic(RuleEventFactory.java:171)[235:org.eclipse.smarthome.automation.api:]
        at org.eclipse.smarthome.automation.events.RuleEventFactory.createRuleRemovedEvent(RuleEventFactory.java:146)[235:org.eclipse.smarthome.automation.api:]
        at org.eclipse.smarthome.automation.core.internal.RuleRegistryImpl.postRuleRemovedEvent(RuleRegistryImpl.java:303)[237:org.eclipse.smarthome.automation.core:]
        at org.eclipse.smarthome.automation.core.internal.RuleRegistryImpl.notifyListenersAboutRemovedElement(RuleRegistryImpl.java:365)[237:org.eclipse.smarthome.automation.core:]
        at org.eclipse.smarthome.automation.core.internal.RuleRegistryImpl.notifyListenersAboutRemovedElement(RuleRegistryImpl.java:1)[237:org.eclipse.smarthome.automation.core:]
        at org.eclipse.smarthome.core.common.registry.AbstractRegistry.removed(AbstractRegistry.java:157)[98:org.eclipse.smarthome.core:]
        at org.eclipse.smarthome.automation.module.script.rulesupport.shared.ScriptedRuleProvider.removeRule(ScriptedRuleProvider.java:59)[242:org.eclipse.smarthome.automation.module.script.rulesupport:]
        at org.eclipse.smarthome.automation.module.script.rulesupport.shared.ScriptedRuleProvider.removeRule(ScriptedRuleProvider.java:54)[242:org.eclipse.smarthome.automation.module.script.rulesupport:]
        at org.eclipse.smarthome.automation.module.script.rulesupport.shared.RuleSupportRuleRegistryDelegate.removeAllAddedByScript(RuleSupportRuleRegistryDelegate.java:118)[242:org.eclipse.smarthome.automation.module.script.rulesupport:]
        at org.eclipse.smarthome.automation.module.script.rulesupport.shared.ScriptedAutomationManager.removeAll(ScriptedAutomationManager.java:99)[242:org.eclipse.smarthome.automation.module.script.rulesupport:]
        at org.eclipse.smarthome.automation.module.script.rulesupport.internal.RuleSupportScriptExtension.unload(RuleSupportScriptExtension.java:197)[242:org.eclipse.smarthome.automation.module.script.rulesupport:]
        at org.eclipse.smarthome.automation.module.script.internal.ScriptExtensionManager.dispose(ScriptExtensionManager.java:110)[240:org.eclipse.smarthome.automation.module.script:]
        at org.eclipse.smarthome.automation.module.script.internal.ScriptEngineManagerImpl.removeScriptExtensions(ScriptEngineManagerImpl.java:147)[240:org.eclipse.smarthome.automation.module.script:]
        at org.eclipse.smarthome.automation.module.script.internal.ScriptEngineManagerImpl.removeEngine(ScriptEngineManagerImpl.java:141)[240:org.eclipse.smarthome.automation.module.script:]
        at org.eclipse.smarthome.automation.module.script.rulesupport.internal.loader.ScriptFileWatcher.removeFile(ScriptFileWatcher.java:133)[242:org.eclipse.smarthome.automation.module.script.rulesupport:]
        at org.eclipse.smarthome.automation.module.script.rulesupport.internal.loader.ScriptFileWatcher.importFile(ScriptFileWatcher.java:140)[242:org.eclipse.smarthome.automation.module.script.rulesupport:]
        at org.eclipse.smarthome.automation.module.script.rulesupport.internal.loader.ScriptFileWatcher.processWatchEvent(ScriptFileWatcher.java:123)[242:org.eclipse.smarthome.automation.module.script.rulesupport:]
        at org.eclipse.smarthome.core.service.WatchQueueReader.run(WatchQueueReader.java:206)[98:org.eclipse.smarthome.core:]
        at java.lang.Thread.run(Thread.java:748)[:1.8.0_131]
14:33:10.917 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'OnItemUpdate.py'
14:33:11.033 [INFO ] [smarthome.event.RuleAddedEvent      ] - Rule 'rule_15' has been added.
14:33:11.036 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - rule_15 updated: INITIALIZING

The script itself


from org.slf4j import LoggerFactory

class OnItemUpdate(SimpleRule):
    def __init__(self):
        self.name = 'OnItemUpdate'
        self.id = 'itemupdaterule'
        self.decription = 'rule to execute when an item is updated'
        self.triggers = [
             Trigger("OnUpdate", "core.ItemStateUpdateTrigger",
                    Configuration({ "itemName": "Z_Socket_2_Power"}))

    def execute(self, module, input):
        LoggerFactory.getLogger("org.eclipse.smarthome.automation.itemupdate").info("Item Z_Socket_2_Power was updated")
        #events.postUpdate("TestString2", "some data")


Is this expected or bugs in openhab or my script?

(steve1) #71

Are you using a very recent snapshot version of OH2?

(Jürgen Wissing) #72

build #986

(steve1) #73

The fix for that problem was added by @smerschjo recently. I’m running snapshot builds and I don’t know the age of the #986 build. If you see the same error when a script file is reloaded it is very likely the bug that has been fixed in later builds.

(Jürgen Wissing) #74

The snapshot i had was a week old. I have updated to the latest snapshot and the exception on change and duplication of rules is gone.
I can still not delete a rule using PaperUI or REST API.

(Jürgen Wissing) #75

Probably deleting via UI or REST is not desirable as one can delete a script just by deleting from the automation/jsr223 folder.

(Helmut Lehmeyer) #76

If you return “list” in openHAB console you’ll get somthing like:

176 | Active | 80 | | Eclipse SmartHome Automation API
177 | Active | 80 | | Eclipse SmartHome Automation commands
178 | Active | 80 | | Eclipse SmartHome Automation Core
179 | Active | 80 | | Eclipse SmartHome Automation Module Core
180 | Active | 80 | | Eclipse SmartHome Automation Media Modules
181 | Active | 80 | | Eclipse SmartHome Automation Module Script
182 | Active | 80 | | Eclipse SmartHome Automation Script Globals
183 | Active | 80 | | Eclipse SmartHome Automation Script RuleSupport
184 | Active | 80 | | Eclipse SmartHome Automation Module Timer
185 | Active | 80 | | Eclipse SmartHome Automation GSON Parser
186 | Active | 80 | | Eclipse SmartHome Automation Providers
187 | Active | 80 | | Eclipse SmartHome Automation REST API
188 | Active | 80 | | Eclipse SmartHome Paper UI, Fragments: 194

The date is interesting “201707110923” to see if you get the changes. I had a installation where i had nightly openhab bundles but Eclipse SmartHome bundles about one month old.

(Jürgen Wissing) #77

189 | Active | 80 | | Eclipse SmartHome Automation API
190 | Active | 80 | | Eclipse SmartHome Automation commands
191 | Active | 80 | | Eclipse SmartHome Automation Core
192 | Active | 80 | | Eclipse SmartHome Automation Module Core
193 | Active | 80 | | Eclipse SmartHome Automation Media Modules
194 | Active | 80 | | Eclipse SmartHome Automation Module Script
195 | Active | 80 | | Eclipse SmartHome Automation Script Globals
196 | Active | 80 | | Eclipse SmartHome Automation Script RuleSupport
197 | Active | 80 | | Eclipse SmartHome Automation Module Timer
198 | Active | 80 | | Eclipse SmartHome Automation GSON Parser
199 | Active | 80 | | Eclipse SmartHome Automation Providers
200 | Active | 80 | | Eclipse SmartHome Automation REST API
These seem to be pretty recent.

(Martin Eskildsen) #78

I have been working on a port of my OH1 jsr223 scripts. So far I am very happy about the new script engine. I have also taken the jsr223 samples that @steve1 has provided on github - very cool!
One thing that I am not able to figure out is how to import an action. Specifically I need the Astro action to calculate if it is day or night. I am quite sure it can be done, I just can figure out how :slight_smile:

BTW. @steve1 in our openhab jsr223 library, you have provided wrappers for all/most triggers. your StartupTrigger has a problem:

class StartupTrigger(Trigger):
    def __init__(self, triggerName=None):
        triggerName = triggerName or uuid.uuid1().hex
        Trigger.__init__(self, triggerName, openhab.STARTUP_MODULE_ID, Configuration())

The problem is that STARTUP_MODULE_ID isn’t declared.
But I really like your library, hope that it will find its way to the script engine :slight_smile:

Thanks to everybody has has worked hard on the JSR223 support in openHAB2!

(steve1) #79

Is this the legacy (OH1) Astro action or a new one?

Did you install the components/000_StartupTrigger.py “component” script? That script defines the custom trigger module and initializes the associated module ID. Assuming that’s the issue, I need to add some documentation and make the error message more helpful.

The StartupTrigger class in openhab.triggers is just the user-friendly Jython wrapper around the custom startup trigger module defined in the component script. Note that the custom module is also visible to rules defined in the Paper UI.

(Martin Eskildsen) #80

It is the legacy oh1. I might also want to use other actions (eg. for notifications)

Nope, only the lib part.
Will try that

Yes, and it is really usefull