[beta testers wanted!] Jython addon w/ helper libraries (requires OH 2.5.x)

Done the same on the OH 3.0.0 docker installation:

2021-01-06 12:45:03.405 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/core/000_Startup.py'
2021-01-06 12:45:05.507 [WARN ] [jython.startup                      ] - 
*******************************************************************************
Jython version:             2.7.2.final
Operating system:           Linux
OS Version:                 5.4.72-v7l+
Java vendor:                Azul Systems, Inc.
Java VM name:               OpenJDK Client VM
Java runtime name:          OpenJDK Runtime Environment
Java runtime version:       11.0.9+11-LTS
configuration.py installed: False
sys.path:                   /openhab/conf/automation/lib/python
                            /openhab/userdata/cache/org.eclipse.osgi/214/0/bundleFile/Lib
                            __classpath__
                            __pyclasspath__/
*******************************************************************************
2021-01-06 12:45:05.509 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/core/components/100_DirectoryTrigger.py'
2021-01-06 12:45:07.481 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/openhab/conf/automation/jsr223/python/core/components/100_DirectoryTrigger.py': java.lang.NullPointerException: java.lang.NullPointerException in <script> at line number 8
2021-01-06 12:45:07.483 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/core/components/100_StartupTrigger.py'
2021-01-06 12:45:10.073 [WARN ] [jython.core.log                     ] - The 'configuration.py' file is missing from teh python.path!
2021-01-06 13:00:52.784 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/personal/Hello.py'
2021-01-06 13:00:55.469 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/personal/Hello.py'
2021-01-06 13:01:22.180 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/personal/Hello.py'
2021-01-06 13:01:26.262 [WARN ] [jython.core.log                     ] - The 'configuration.py' file is missing from teh python.path!
2021-01-06 13:01:26.953 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/openhab/conf/automation/jsr223/python/personal/Hello.py': ImportError: No module named joda in <script> at line number 6

Only error messages no output at all.

The hello world example:

"""
This script uses a decorated cron rule that will generate logs every 10s and can be used to test your initial setup.
"""
from core.rules import rule
from core.triggers import when

@rule("Jython Hello World (cron decorators)", description="This is an example cron triggered rule using decorators", tags=["Test tag", "Hello World"])# description and tags are optional
@when("Time cron 0/10 * * * * ?")
def hello_world_cron_decorators(event):
hello_world_cron_decorators.log.info("Hello World!")

Hi @lukics - it looks like you haven’t set up configuration.py. It should be in /lib/python/configuration.py

RTFM!?, thanks

@danielwalters86 please post more of your log. Do you see warn entries like lukics?

@lukics you are indeed missing the configuration.py file, but that is not why the hello.py script is not loading. I see a warning in your log saying that joda cannot be imported, if you were indeed running the patch from PR #376 you would not see this error. Please make sure that you updated to the correct version.

@CrazyIvan359 I have updated my post with a longer log.

Edit: This is strange. I tried to debug what was going on so I ssh’d into the karaf console and issued the command log:set DEBUG at 13:55 and all of a sudden I had output in the logs…

2021-01-06 13:10:38.517 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/core/components/100_DirectoryTrigger.py'

2021-01-06 13:10:40.099 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/core/components/100_StartupTrigger.py'

2021-01-06 13:10:40.954 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/personal/cron.py'

2021-01-06 13:10:46.124 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/personal/hello_world.py'

2021-01-06 13:10:48.980 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/personal/new_hello_world.py'

2021-01-06 13:55:10.360 [INFO ] [port.EventAdminConfigurationNotifier] - Sending Event Admin nofification (configuration successful) to org/ops4j/pax/logging/Configuration

2021-01-06 13:55:20.088 [INFO ] [Jython Hello World (cron decorators)] - Hello World!

2021-01-06 13:55:20.226 [INFO ] [jsr223.jython.Rule Name             ] - Hello World!

2021-01-06 13:55:30.088 [INFO ] [Jython Hello World (cron decorators)] - Hello World!

2021-01-06 13:55:30.226 [INFO ] [jsr223.jython.Rule Name             ] - Hello World!

2021-01-06 13:55:38.269 [DEBUG] [e.osgi.LoggingCommandSessionListener] - Executing command: 'log:set INFO'

2021-01-06 13:55:38.277 [DEBUG] [org.apache.felix.configadmin        ] - getConfiguration(pid=org.ops4j.pax.logging, location=null)

2021-01-06 13:55:38.279 [DEBUG] [org.apache.felix.configadmin        ] - Found cached configuration org.ops4j.pax.logging bound to ?

2021-01-06 13:55:38.298 [DEBUG] [org.apache.felix.configadmin        ] - update(properties={felix.fileinstall.filename=file:/openhab/userdata/etc/org.ops4j.pax.logging.cfg, org.ops4j.pax.logging.log4j2.config.file=/openhab/userdata/etc/log4j2.xml, service.pid=org.ops4j.pax.logging})

2021-01-06 13:55:38.300 [DEBUG] [org.apache.felix.configadmin        ] - Updating config org.ops4j.pax.logging with {felix.fileinstall.filename=file:/openhab/userdata/etc/org.ops4j.pax.logging.cfg, org.ops4j.pax.logging.log4j2.config.file=/openhab/userdata/etc/log4j2.xml, service.pid=org.ops4j.pax.logging}

2021-01-06 13:55:38.304 [DEBUG] [org.apache.felix.configadmin        ] - No SynchronousConfigurationListeners to send CM_UPDATED event to.

2021-01-06 13:55:38.306 [DEBUG] [org.apache.felix.configadmin        ] - Scheduling task Fire ConfigurationEvent: pid=org.ops4j.pax.logging

2021-01-06 13:55:38.308 [DEBUG] [org.apache.felix.configadmin        ] - Running task Fire ConfigurationEvent: pid=org.ops4j.pax.logging

2021-01-06 13:55:38.310 [DEBUG] [org.apache.felix.configadmin        ] - Sending CM_UPDATED event for org.ops4j.pax.logging to [org.osgi.service.cm.ConfigurationListener, id=47, bundle=16/mvn:org.apache.karaf.shell/org.apache.karaf.shell.core/4.2.7]

2021-01-06 13:55:38.312 [DEBUG] [org.apache.felix.configadmin        ] - Sending CM_UPDATED event for org.ops4j.pax.logging to [org.osgi.service.cm.ConfigurationListener, id=122, bundle=9/mvn:org.apache.felix/org.apache.felix.configadmin/1.9.16]

2021-01-06 13:55:38.313 [DEBUG] [org.apache.felix.configadmin        ] - Sending CM_UPDATED event for org.ops4j.pax.logging to [org.openhab.core.karaf.internal.FeatureInstaller, org.osgi.service.cm.ConfigurationListener, id=292, bundle=163/mvn:org.openhab.core.bundles/org.openhab.core.karaf/3.1.0-SNAPSHOT]

2021-01-06 13:55:38.315 [DEBUG] [org.apache.felix.configadmin        ] - Sending CM_UPDATED event for org.ops4j.pax.logging to [org.osgi.service.cm.ConfigurationListener, id=133, bundle=46/mvn:org.apache.aries.jax.rs/org.apache.aries.jax.rs.whiteboard/1.0.9]

2021-01-06 13:55:38.316 [DEBUG] [org.apache.felix.configadmin        ] - Scheduling task Update: pid=org.ops4j.pax.logging

2021-01-06 13:55:38.319 [DEBUG] [org.apache.felix.configadmin        ] - Sending CM_UPDATED event for org.ops4j.pax.logging to [org.osgi.service.cm.ConfigurationListener, id=76, bundle=53/mvn:org.apache.karaf.config/org.apache.karaf.config.core/4.2.7]

2021-01-06 13:55:38.319 [DEBUG] [org.apache.felix.configadmin        ] - Running task Update: pid=org.ops4j.pax.logging

2021-01-06 13:55:38.320 [DEBUG] [org.apache.felix.configadmin        ] - UpdateConfiguration(org.ops4j.pax.logging) scheduled

2021-01-06 13:55:38.322 [DEBUG] [org.apache.felix.configadmin        ] - Updating configuration org.ops4j.pax.logging to revision #4

2021-01-06 13:55:38.324 [DEBUG] [org.apache.felix.configadmin        ] - canReceive=true; bundle=mvn:org.ops4j.pax.logging/pax-logging-log4j2/1.11.2; configuration=? (no SecurityManager)

2021-01-06 13:55:38.325 [DEBUG] [e.osgi.LoggingCommandSessionListener] - Command: 'log:set INFO' returned 'null'

2021-01-06 13:55:38.324 [DEBUG] [org.apache.felix.configadmin        ] - Sending CM_UPDATED event for org.ops4j.pax.logging to [org.osgi.service.cm.ConfigurationListener, id=134, bundle=46/mvn:org.apache.aries.jax.rs/org.apache.aries.jax.rs.whiteboard/1.0.9]

2021-01-06 13:55:38.432 [INFO ] [port.EventAdminConfigurationNotifier] - Sending Event Admin nofification (configuration successful) to org/ops4j/pax/logging/Configuration

2021-01-06 13:55:40.090 [INFO ] [Jython Hello World (cron decorators)] - Hello World!

2021-01-06 13:55:40.226 [INFO ] [jsr223.jython.Rule Name             ] - Hello World!

2021-01-06 13:55:50.089 [INFO ] [Jython Hello World (cron decorators)] - Hello World!

2021-01-06 13:55:50.226 [INFO ] [jsr223.jython.Rule Name             ] - Hello World!

2021-01-06 13:56:00.089 [INFO ] [Jython Hello World (cron decorators)] - Hello World!

2021-01-06 13:56:00.227 [INFO ] [jsr223.jython.Rule Name             ] - Hello World!

2021-01-06 13:56:10.089 [INFO ] [Jython Hello World (cron decorators)] - Hello World!

2021-01-06 13:56:10.228 [INFO ] [jsr223.jython.Rule Name             ] - Hello World!

2021-01-06 13:56:20.091 [INFO ] [Jython Hello World (cron decorators)] - Hello World!

2021-01-06 13:56:20.228 [INFO ] [jsr223.jython.Rule Name             ] - Hello World!

2021-01-06 13:56:30.090 [INFO ] [Jython Hello World (cron decorators)] - Hello World!

2021-01-06 13:56:30.228 [INFO ] [jsr223.jython.Rule Name             ] - Hello World!

2021-01-06 13:56:32.008 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/personal/hello_world.py'

2021-01-06 13:56:40.229 [INFO ] [jsr223.jython.Rule Name             ] - Hello World!

2021-01-06 13:56:40.908 [INFO ] [Jython Hello World (cron decorators)] - Hello World! (hello_world.py)

2021-01-06 13:56:50.229 [INFO ] [jsr223.jython.Rule Name             ] - Hello World!

2021-01-06 13:56:50.344 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/openhab/conf/automation/jsr223/python/personal/cron.py'

2021-01-06 13:56:50.906 [INFO ] [Jython Hello World (cron decorators)] - Hello World! (hello_world.py)

2021-01-06 13:57:00.681 [INFO ] [jsr223.jython.Rule Name             ] - Hello World! (cron.py)

2021-01-06 13:57:00.907 [INFO ] [Jython Hello World (cron decorators)] - Hello World! (hello_world.py)

2021-01-06 13:57:10.684 [INFO ] [jsr223.jython.Rule Name             ] - Hello World! (cron.py)

2021-01-06 13:57:10.907 [INFO ] [Jython Hello World (cron decorators)] - Hello World! (hello_world.py)

2021-01-06 13:57:20.683 [INFO ] [jsr223.jython.Rule Name             ] - Hello World! (cron.py)

2021-01-06 13:57:20.907 [INFO ] [Jython Hello World (cron decorators)] - Hello World! (hello_world.py)

2021-01-06 13:57:30.684 [INFO ] [jsr223.jython.Rule Name             ] - Hello World! (cron.py)

2021-01-06 13:57:30.909 [INFO ] [Jython Hello World (cron decorators)] - Hello World! (hello_world.py)

2021-01-06 13:57:40.684 [INFO ] [jsr223.jython.Rule Name             ] - Hello World! (cron.py)

2021-01-06 13:57:40.910 [INFO ] [Jython Hello World (cron decorators)] - Hello World! (hello_world.py)

2021-01-06 13:57:50.685 [INFO ] [jsr223.jython.Rule Name             ] - Hello World! (cron.py)

2021-01-06 13:57:50.908 [INFO ] [Jython Hello World (cron decorators)] - Hello World! (hello_world.py)

2021-01-06 13:58:00.688 [INFO ] [jsr223.jython.Rule Name             ] - Hello World! (cron.py)

2021-01-06 13:58:00.909 [INFO ] [Jython Hello World (cron decorators)] - Hello World! (hello_world.py)

@danielwalters86 I don’t see that anything would have changed, but glad it’s working now

True, did not pull the oh3-patch branch

After setting the debug level I see the “Hello World” output

Thanks for the help.

1 Like

Thanks, any ideas why I can’t import libraries into my personal scripts?

In OH2 I had my libraries in /lib/python/personal and would use something like

import personal.my_utils.py

but now I get the message

Error during evaluation of script ‘file:/openhab/conf/automation/jsr223/python/personal/hello_world_v2.py’: ImportError: No module named personal in at line number 6

You have to rename the file to _ init _ .py (remove the additional extension)

image

@danielwalters86

Assuming you have a file named my_utils.py in the personal folder, you would import and use it like this:

from personal import my_utils

my_utils.my_func()

Alternatively you can import thing from you file like this:

from personal.my_utils import my_func

my_func()

@lukics you don’t necessarily have to put your code in the __init__.py file, there just needs to be an __init__.py file for you to be able to import things from that folder.

Sure,
Maybe my statement above was not clear enough, that’s what I meant.

Yes, it definitely works, I just wanted to let you know that you don’t have to put all the module code in that file because it can get long and messy having a big module stuffed into a single file.

@lukics Thank you, this was what I was missing. My file was still called __init__.py.example

This was useful information too. I’m now up and running calling a basic library function so thank you both :+1:

1 Like

Sry if this is answered anywhere here in this thread, but it takes month to read all posts…

Am I able to get this running with OH3 or not? and if the answer is yes, what is required to get this working?

I got:

  • the 2.5 addon jar
  • the helper libs
  • the helper lib workaround for OH3
  • renamed configuration.py and init.py
  • using jython-standalone-2.7.0.jar

maybe I miss something or it isn’t possible for now?!

cheers, Dan

The add-on is not available in OH3, but there is an updated version of the Helper Libraries available. This post contains notes and instructions on getting that set up. Skip steps 10 and 11, they do not apply in OH3 because the new Python Automation add-on handles them for you.

1 Like

thanks but your link does not work… can you please correct it :wink:

I think it is here Dan

1 Like

thanx :wink: I think it’s running for me now, but VSCode still complains - can I ignore these errors?

Unable to import 'core.triggers'pylint(import-error)
Undefined variable 'items'pylint(undefined-variable)

etc…

1 Like

It helps if you paste the link location when making a link… Doh! Fixed now.

There is a .pylintrc file in the root of the repo, and a page in the documentation that covers setting up VSC to work a little better. I am working on stubs for all of openHAB and the Helper Libraries, hopefully I will be able to get the finishing touches on soon and get them released.

5 Likes

ok thanks a lot :slight_smile: