First steps in jython

All,

I want to give a try to jython rules. I installed the automation plugin and also folloed the instructions posted here.

When reaching the verification step with the hello_world.py, I get an error in the log

2021-01-24 23:15:04.069 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/etc/openhab/automation/jsr223/python/personal/hello_world.py'
2021-01-24 23:15:07.647 [DEBUG] [ipt.internal.ScriptEngineManagerImpl] - Added ScriptEngine for language 'py' with identifier: file:/etc/openhab/automation/jsr223/python/personal/hello_world.py
2021-01-24 23:15:12.121 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/python/personal/hello_world.py': ImportError: No module named joda in <script> at line number 6

I have not customized configuration.py (except for the email address), nor set EXTRA_JAVA_OPTS (according to this).

So, what am I missing?

FYI, the addition error appears at the start - see below.

Thanks!
Max

2021-01-24 23:08:17.452 [WARN ] [jython.startup                      ] - 
*******************************************************************************
Jython version:             2.7.2.final
Operating system:           Linux
OS Version:                 5.4.83-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: True
sys.path:                   /etc/openhab/automation/lib/python
                            /var/lib/openhab/cache/org.eclipse.osgi/263/0/bundleFile/Lib
                            __classpath__
                            __pyclasspath__/
*******************************************************************************
2021-01-24 23:08:17.459 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/etc/openhab/automation/jsr223/python/core/components/100_DirectoryTrigger.py'
2021-01-24 23:08:19.845 [WARN ] [mmon.WrappedScheduledExecutorService] - Scheduled runnable ended with an exception: 
java.lang.NullPointerException: null
	at org.openhab.binding.mqtt.generic.MqttChannelTypeProvider.derive(MqttChannelTypeProvider.java:132) ~[?:?]
	at org.openhab.binding.mqtt.homeassistant.internal.discovery.HomeAssistantDiscovery.publishResults(HomeAssistantDiscovery.java:197) ~[?:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at java.lang.Thread.run(Thread.java:834) [?:?]
2021-01-24 23:08:21.802 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/python/core/components/100_DirectoryTrigger.py': java.lang.NullPointerException: java.lang.NullPointerException in <script> at line number 8

With openhab 3 you no longer need to modify the EXTRA_JAVA_OPTS. The error you are seeing is because the helper libraries have not been upgraded to work with OH3 yet, but there is an open Pull Request #376 with a version that works.

have a look here

@CrazyIvan359 , @HaKuNa

thanks for pointing out to the issue - I am not sure if I want to wait for the official release or going for the cutting edge… I am not a big fan of DSL rules :wink:

Max

@Max77 I finally migrated to OH3 with the Jithon helper libs workaround one week ago. No issues so far. I have 70 things, around 700 items and around 100 Jithon rules. There is only one DSL rule remaining (timeLinePicker).

@Max77 the changes required to get the libraries working on OH3 are actually quite small. I’ve done previous work on the Helper Libraries and went over Jim’s work and fixed a few things that were missed (that PR contains those updates). I have been running that version ever since with no issues, you should not be concerned about stability.

@HaKuNa , @CrazyIvan359

thanks a lot, patch installed and working :slight_smile:

Max

Next (small?) issue - I have tried the hello_world.py as instructed

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

and I can see the rule is triggered by the cron event (in 10s +/- 2ms :sunglasses:)

2021-01-27 22:55:31.362 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'iPadMax_Location' changed...
2021-01-27 22:55:40.372 [DEBUG] [e.automation.internal.RuleEngineImpl] - The trigger 'Time_cron_0_10_27eeb05e60ea11ebb38bdca63251b192_28834f4060ea11ebbc57dca63251b192' of rule '8377f5bd-38a0-4ab7-887f-40965cc56b74' is triggered.
2021-01-27 22:55:40.375 [DEBUG] [e.automation.internal.RuleEngineImpl] - The rule '8377f5bd-38a0-4ab7-887f-40965cc56b74' is executed.
2021-01-27 22:55:50.374 [DEBUG] [e.automation.internal.RuleEngineImpl] - The trigger 'Time_cron_0_10_27eeb05e60ea11ebb38bdca63251b192_28834f4060ea11ebbc57dca63251b192' of rule '8377f5bd-38a0-4ab7-887f-40965cc56b74' is triggered.
2021-01-27 22:55:50.378 [DEBUG] [e.automation.internal.RuleEngineImpl] - The rule '8377f5bd-38a0-4ab7-887f-40965cc56b74' is executed.

But why I do not see any “Hello World!” message? Am I missing something? I have looked to the different OH logging capabilities, but no explanation came to my mind.

Thanks for your help!
Max

Did you create a configuration.py file? If so, you may need to set the log level for whatever LOG_PREFIX is set to in karaf.

try log.warn or log.error

I have renamed configured.py and just left as-is

# -*- coding: utf-8 -*-
LOG_PREFIX = "jsr223.jython"

admin_email = "notimportant@gmail.com"

openhabHost = "localhost"
openhabPort = "8080"# "8443"

My karaf reports

openhab> log:list                                                                                                                                  
Logger                                             │ Level
───────────────────────────────────────────────────┼──────
ROOT                                               │ ERROR
javax.jmdns                                        │ ERROR
javax.mail                                         │ ERROR
openhab.event                                      │ INFO
openhab.event.AddonEvent                           │ ERROR
openhab.event.InboxUpdatedEvent                    │ ERROR
openhab.event.ItemAddedEvent                       │ ERROR
openhab.event.ItemChannelLinkAddedEvent            │ ERROR
openhab.event.ItemChannelLinkRemovedEvent          │ ERROR
openhab.event.ItemRemovedEvent                     │ ERROR
openhab.event.ItemStateEvent                       │ ERROR
openhab.event.RuleAddedEvent                       │ ERROR
openhab.event.RuleRemovedEvent                     │ ERROR
openhab.event.RuleStatusInfoEvent                  │ ERROR
openhab.event.StartlevelEvent                      │ ERROR
openhab.event.ThingAddedEvent                      │ ERROR
openhab.event.ThingRemovedEvent                    │ ERROR
openhab.event.ThingStatusInfoEvent                 │ ERROR
openhab.event.ThingUpdatedEvent                    │ ERROR
org.apache.cxf.jaxrs.sse.SseEventSinkImpl          │ ERROR
org.apache.karaf.jaas.modules.audit                │ INFO
org.apache.karaf.kar.internal.KarServiceImpl       │ ERROR
org.apache.karaf.shell.ssh.SshUtils                │ ERROR
org.apache.karaf.shell.support                     │ OFF
org.apache.sshd                                    │ WARN
org.eclipse.lsp4j                                  │ OFF
org.jupnp                                          │ ERROR
org.openhab                                        │ INFO
org.openhab.core.automation                        │ DEBUG
org.ops4j.pax.url.mvn.internal.AetherBasedResolver │ ERROR
org.ops4j.pax.web.pax-web-runtime                  │ OFF

I am not really sure where to go from here…

Run log:set DEBUG jsr223.jython and see if that makes the log entries appear.

1 Like

Thanks, it works now!

Now I can start working with JS223 rules - they seem the right way to get access to semantic data (as compared to DSL rules) and achieve what I want to do :slight_smile:

1 Like