How to Setup Jython

Scott. I am assuming this works because there is only one thread loading rules?

Correct. And files are loaded in a particular order.

If this is the problem, here is the issue for it. There was a recent change that made this better, and I thought it was resolved, but on slower hardware or a startup during high load, it can still happen…

The above error still pops up but the other ones are all gone

After a restart last night, I was kind of expecting that you’d have the same. :disappointed:

I’ll play with it… likely an issue with the loading order. Best to remove any components that you aren’t planning to use.

Will do. Thanks for your help and this awesome work!! I think it might make sense to maybe have a wiki page which collect all different Jython example rules. Similar to what we had for Openhab for the integrated rule engine. Will start testing a bit over the weekend

I agree. I’ve tried to include everything people have asked about into this readme, but I could also add a wiki for others to contribute to. I was also going to build out the example scripts too.

All I’ve done is piggyback on the much appreciated work of others and add some bling… and hopefully pump some life back into the new rule engine. But you are very welcome for anything I have been able to contribute!

@5iver I was looking at the error @Tomibeck is having and seems I have the similar. What I found that seems to work, move the delay script into the components directory, renumber all the components with the delay script as 000_startup.py. You may not need a different number for all components, just make sure 000_startup.py is the first to run.

Here’s the log after a restart and no more error’s.

2018-10-12 11:16:57.829 [DEBUG] [rg.eclipse.smarthome.automation.rest] - BundleEvent STARTED - org.eclipse.smarthome.automation.rest

2018-10-12 11:16:58.497 [INFO ] [basic.internal.servlet.WebAppServlet] - Started Basic UI at /basicui/app

2018-10-12 11:16:58.872 [INFO ] [arthome.ui.paper.internal.PaperUIApp] - Started Paper UI at /paperui

2018-10-12 11:17:13.069 [INFO ] [eclipse.smarthome.model.script.Rules] - JSR223: Startup: Checking for initialized context

2018-10-12 11:17:13.091 [INFO ] [eclipse.smarthome.model.script.Rules] - JSR223: Startup: context initialized... waiting 30s before allowing files to load

2018-10-12 11:17:43.098 [INFO ] [eclipse.smarthome.model.script.Rules] - JSR223: Startup: Complete

2018-10-12 11:17:43.108 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/000_startup.py

2018-10-12 11:17:43.121 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/001_StartupTrigger.py'

2018-10-12 11:17:43.603 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/001_StartupTrigger.py

2018-10-12 11:17:43.608 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/002_DirectoryTrigger.py'

2018-10-12 11:17:47.788 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/002_DirectoryTrigger.py

2018-10-12 11:17:47.795 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/003_OsgiEventTrigger.py'

2018-10-12 11:17:49.604 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/003_OsgiEventTrigger.py

2018-10-12 11:17:49.609 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/004_Esper.py'

2018-10-12 11:17:57.128 [DEBUG] [e.smarthome.automation.module.script] - ServiceEvent REGISTERED - {org.osgi.service.event.EventHandler}={service.id=371, service.bundleid=198, service.scope=singleton, event.topics=['*']} - org.eclipse.smarthome.automation.module.script

2018-10-12 11:17:57.202 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/004_Esper.py

2018-10-12 11:17:57.217 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/005_JythonExtensionProvider.py'

2018-10-12 11:17:59.588 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/005_JythonExtensionProvider.py

2018-10-12 11:17:59.598 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/006_ExampleExtensionProvider.py'

2018-10-12 11:17:59.663 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/006_ExampleExtensionProvider.py

2018-10-12 11:17:59.668 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/007_JythonThingProvider.py'

2018-10-12 11:17:59.812 [DEBUG] [e.smarthome.automation.module.script] - ServiceEvent REGISTERED - {org.eclipse.smarthome.core.thing.ThingProvider}={service.id=372, service.bundleid=198, service.scope=singleton} - org.eclipse.smarthome.automation.module.script

2018-10-12 11:17:59.840 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/007_JythonThingProvider.py

2018-10-12 11:17:59.846 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/008_JythonBindingInfoProvider.py'

2018-10-12 11:18:00.172 [DEBUG] [e.smarthome.automation.module.script] - ServiceEvent REGISTERED - {org.eclipse.smarthome.core.binding.BindingInfoProvider}={service.id=373, service.bundleid=198, service.scope=singleton} - org.eclipse.smarthome.automation.module.script

2018-10-12 11:18:00.183 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/008_JythonBindingInfoProvider.py

2018-10-12 11:18:00.195 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/009_JythonItemProvider.py'

2018-10-12 11:18:00.356 [DEBUG] [e.smarthome.automation.module.script] - ServiceEvent REGISTERED - {org.eclipse.smarthome.core.items.ItemProvider}={service.id=374, service.bundleid=198, service.scope=singleton} - org.eclipse.smarthome.automation.module.script

2018-10-12 11:18:00.368 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/009_JythonItemProvider.py

2018-10-12 11:18:00.372 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/010_JythonThingTypeProvider.py'

2018-10-12 11:18:00.500 [DEBUG] [e.smarthome.automation.module.script] - ServiceEvent REGISTERED - {org.eclipse.smarthome.core.thing.binding.ThingTypeProvider}={service.id=375, service.bundleid=198, service.scope=singleton} - org.eclipse.smarthome.automation.module.script

2018-10-12 11:18:00.518 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/010_JythonThingTypeProvider.py

2018-10-12 11:18:00.522 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/011_JythonTransform.py'

2018-10-12 11:18:00.649 [DEBUG] [e.smarthome.automation.module.script] - ServiceEvent REGISTERED - {org.eclipse.smarthome.core.transform.TransformationService}={service.id=376, service.bundleid=198, service.scope=singleton, smarthome.transform=JYTHON} - org.eclipse.smarthome.automation.module.script

2018-10-12 11:18:00.659 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/011_JythonTransform.py

2018-10-12 11:18:00.663 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_test.py'

2018-10-12 11:18:00.717 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_test.py

2018-10-12 11:18:00.721 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'test1.py'

2018-10-12 11:18:00.758 [INFO ] [clipse.smarthome.automation.examples] - Hello jython world!

2018-10-12 11:18:00.761 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: test1.py

Here you can see how the components are numbered.

Maybe the components can stay?

For those using the openHAB Debian Docker container on Ubuntu, I have created a guide of how I have setup Jython here.

@5iver, I just installed and set up Jython per your README in the OH-Jython-Scripters/openhab2-jython GitHub repository. I found your instructions quite clear and had no problem getting to the initial testing phase. I did see the following in the logs, which I suspect is not intended:

2018-10-14 13:44:13.716 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - in queue: {md=[file:/etc/openhab2/automation/jsr223/000_components/README.md]}

I have since removed README.md from /etc/openhab2/automation/jsr223/000_components/.

Thanks for getting after this, it looks quite promising!

1 Like

I just finished converting about 400 lines of DSL to python. What a difference! Code runs much faster and there are no problems with threading or other odd errors that were being generated by the DSL. Jython is a great option for doing rules!

@mjcumming I would be interested in seeing a few of the DSL rules you had, and the python code used, to help with learning code.

Thanks

@5iver, I’ve noted these errors during startup that seem to prevent my test script, group_members.py, from running:

2018-10-15 07:24:54.917 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_DirectoryTrigger.py'
2018-10-15 07:25:04.405 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'sun-elevation-azimuth.rules'
2018-10-15 07:25:07.732 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_DirectoryTrigger.py': TypeError: can't set attributes of built-in/extension type 'NoneType' in <script> at line number 8
2018-10-15 07:25:07.736 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/000_DirectoryTrigger.py
2018-10-15 07:25:07.739 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_Esper.py'
2018-10-15 07:25:09.015 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_Esper.py': TypeError: can't set attributes of built-in/extension type 'NoneType' in <script> at line number 5
2018-10-15 07:25:09.018 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/000_Esper.py
2018-10-15 07:25:09.020 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_JythonBindingInfoProvider.py'
2018-10-15 07:25:09.107 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_JythonBindingInfoProvider.py': TypeError: can't set attributes of built-in/extension type 'NoneType' in <script> at line number 3
2018-10-15 07:25:09.109 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/000_JythonBindingInfoProvider.py
2018-10-15 07:25:09.112 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_JythonExtensionProvider.py'
2018-10-15 07:25:11.334 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_JythonExtensionProvider.py': TypeError: can't set attributes of built-in/extension type 'NoneType' in <script> at line number 3
2018-10-15 07:25:11.337 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/000_JythonExtensionProvider.py
2018-10-15 07:25:11.340 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_JythonItemProvider.py'
2018-10-15 07:25:11.409 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_JythonItemProvider.py': TypeError: can't set attributes of built-in/extension type 'NoneType' in <script> at line number 3
2018-10-15 07:25:11.411 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/000_JythonItemProvider.py
2018-10-15 07:25:11.414 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_JythonThingProvider.py'
2018-10-15 07:25:11.483 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_JythonThingProvider.py': TypeError: can't set attributes of built-in/extension type 'NoneType' in <script> at line number 3
2018-10-15 07:25:11.485 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/000_JythonThingProvider.py
2018-10-15 07:25:11.488 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_JythonThingTypeProvider.py'
2018-10-15 07:25:11.566 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_JythonThingTypeProvider.py': TypeError: can't set attributes of built-in/extension type 'NoneType' in <script> at line number 3
2018-10-15 07:25:11.569 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/000_JythonThingTypeProvider.py
2018-10-15 07:25:11.570 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_JythonTransform.py'
2018-10-15 07:25:11.647 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_JythonTransform.py': TypeError: can't set attributes of built-in/extension type 'NoneType' in <script> at line number 3
2018-10-15 07:25:11.649 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/000_JythonTransform.py
2018-10-15 07:25:11.651 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_OsgiEventTrigger.py'
2018-10-15 07:25:12.436 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_OsgiEventTrigger.py': TypeError: can't set attributes of built-in/extension type 'NoneType' in <script> at line number 10
2018-10-15 07:25:12.439 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/000_OsgiEventTrigger.py
2018-10-15 07:25:12.441 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/000_StartupTrigger.py'
2018-10-15 07:25:12.503 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/000_StartupTrigger.py': TypeError: can't set attributes of built-in/extension type 'NoneType' in <script> at line number 4
2018-10-15 07:25:12.505 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/000_StartupTrigger.py
2018-10-15 07:25:12.508 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script '000_components/100_ExampleExtensionProvider.py'
2018-10-15 07:25:12.547 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/000_components/100_ExampleExtensionProvider.py': TypeError: can't set attributes of built-in/extension type 'NoneType' in <script> at line number 1
2018-10-15 07:25:12.550 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: 000_components/100_ExampleExtensionProvider.py
2018-10-15 07:25:12.553 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'group_members.py'
2018-10-15 07:25:12.620 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/group_members.py': TypeError: can't set attributes of built-in/extension type 'NoneType' in <script> at line number 2
2018-10-15 07:25:12.622 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: group_members.py

After I simply touched the the file group_members.py to force a reload, the script runs as expected. I attribute this behavior to the as yet unresolved OH2 startup clusterf**k on slower hardware (OH2 running on RPi 3B here.)

My question: Do you have a technique for preventing premature rule execution at startup? I see lucid/rules.py uses a “special” Item definition to determine when the loading of Items has been completed. I wonder if there might be a similar technique for testing when the loading of script dependencies is finished?

This may help…

Here is the ESH issue and the source of the script I use…

However, in my testing, this script was completing before the modules were loaded. But the 30s sleep at the end seems to be enough to get everything setup before the other scripts were loaded.

I had the same issue on RPI and solved with Scott’s script

import time

from org.slf4j import Logger, LoggerFactory
log = LoggerFactory.getLogger("org.eclipse.smarthome.model.script.Rules")

log.info("JSR223: Startup: Checking for initialized context")

while True:
    try:
        scriptExtension.importPreset("RuleSupport")
        if automationManager != None:
            log.info("JSR223: Startup: context initialized... waiting 30s before allowing files to load")
            break;
    except:
        pass
    log.info("JSR223: Startup: Context not initialized yet... waiting 10s before checking again")
    time.sleep(10)

time.sleep(30)
log.info("JSR223: Startup: Complete")

Place this in the 000_components directory and name it 000_startup.py then rename the other components to something higher than 000_othercomponets. You can see a description on post #94 of how I renamed everything. Restart OH and the errors should go away.

@5iver, thanks for the quick reply – egg on my face, all I had to do was search this topic (:astonished:)!

I should add this script to the readme…

@Scott testing with 000_startup.py placed in /automation/jsr223/ does not clear the errors.

Placing this script in /automation/jsr223/000_components and naming the other components as something higher than 000 does clear the errors.

Note I didn’t test without renaming the other components so that may not be required.

1 Like

@scottk if placing 000_startup.py in /automation/jsr223/ does not clear the errors, didn’t for me, please test by placing the 000_startup.py in /automation/jsr223/000_components directory and without changing the names of other components. If this works let me know, if not then changing the other components names does work. See picture in post #94 for example.

Thanks

Thank you for the recommendation, @H102. I named the script 000_0_delay_startup.py and placed it into /etc/openhab2/automation/jsr223/000_components/ (actually, /etc/openhab2/automation is symlinked to /usr/share/openhab2/automation.)

During my first OH2 restart for testing, startup appears to be hung some time after the message “delaying ‘Initialize key item states at system startup’ rule for 30 seconds” shows up in the log.

[Updates:]
I’m still troubleshooting… My initial assessment is that the hang was probably not related to JSR223 since I have some logging in my DSL rules that indicates rule globals were not correctly initialized.

I’m now not so sure that my initial assessment was correct, OH2 startup is stalled at the same ponit with the same two uninitialized DSL rule globals.

What version of OH are you using?

While testing I used a spare RPI with a fresh install running 2.4 snapshot build #1381. I also had no DSL rules in the rules file.