How to Setup Jython

Not yet, but I’m sure @RRoe will be wanting to include it after any issues get flushed out. Lucid hasn’t been updated for the API changes, so will only work with 2.3.

1 Like

Please use the other thread for asking question about the new decorators. I’m excited to have some more people using Jython and able to test them out! Thank you!

1 Like

Not that I can think of. It was the install that I wasn’t sure of, but I think we got it all squared away.

thank you for the help - will post questions in the thread you linked above

1 Like

I was going to suggest I start over with a fresh install, I made lots of moves and permission changes, but I think @mjcumming proved the install is good.

Thanks

1 Like

Hi i restarted and followed your new Instructions. When i get to the point to restart i get the following error message for multiple of the files and in different lines:

2018-10-11 19:54:40.964 [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

However in the end they seem to be all loaded.

Testing with the hello world script works as well

If you aren’t going to use the component scripts, then there is no need to install them. I think I should move them into a separate folder, like the Script Examples.

Line 8 is import openhab, so my guess is that the scripts are loading before the modules are fully loaded. This is a known bug, similar to the Rules DSL loading scripts before tiems are fully loaded. You could try adding this script to delay the loading of the other scripts (including the components). Name it 000_startup.py and put it in the root of /automation/jsr223/. Hopefully that helps. I use this script and do not see the same errors you are reporting. If this resolves it, I’ll add it to the repo.

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")

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.