Jython - System started, calling other scripts, JSR223 vs Jython

Hi :grinning:

I just moved my rules from rules DSL to Jython and I have a few questions that I would like to ask.

  1. I cant get @when("System started") to work, jython script:
from openhab.rules import rule
from openhab.triggers import when

@rule("Initial setup")
@when("System started")
def initialSetupRule(event):
	log.debug("JSR223: System started.")
	createTimer( DateTime.now().plusMinutes( 1 ), initialSetup )

Error that I get:

[ERROR] [omation.core.internal.RuleEngineImpl] - @when: Exception [@when: "System started" could not be parsed because rule triggers do not currently support target_type "System"]: [Traceback (most recent call last):

  File "/etc/openhab2/automation/lib/python/openhab/triggers.py", line 333, in when

    raise ValueError("@when: \"{}\" could not be parsed because rule triggers do not currently support target_type \"System\"".format(target))

ValueError: @when: "System started" could not be parsed because rule triggers do not currently support target_type "System"

]

[ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/coop_rules.py': TypeError: 'NoneType' object is not callable in <script> at line number 100

And looking here it should be working :confused:

  1. I have uC (microcontroller) that I want “talk” to via i2c, I got this part working in Python before I even started with OpenHAB. Before I switched to Jython I got advised here the best option would be to run my uC script as a service sort of independent of OH because I want every X seconds read data from it and send data to it when some OH rules are triggered. Since I have now switched to Jython wouldn’t it be better to just import my uC class (with all i2c methods) from that script and call them in cron rule?

  2. JSR223 and Jython. I would like to know if I understood everything right. JSR223 is a “functionality” that lets other scripting languages comunicate with Java (objects/classes/etc) and Jython is just Python using those “Java things”, right? So Jython = JSR223 but JSR223 ≠ Jython ?

No, it won’t work yet. The docs also say it’s not ready


@when(“System started”)# ‘System shuts down’ cannot currently be used as a trigger, and ‘System started’ needs to be updated to work with Automation API updates

In the meantime, just run the function directly from the script, and it will run when the script is loaded or saved. I hope to get to it soon, if someone else doesn’t get to it first!

Since it is already written, it is probably not worth the effort, unless there is some advantage to having it within OH.

There are a few good links here. JSR223 is a Java scripting API that has been included in the new rule engine. Jython is a Python implementation for the JVM, and is a scripting language compatible with JSR223.

1 Like