[RESOLVED]:JSR223 Jython Hello World example not working

jsr223
rules
jython
Tags: #<Tag:0x00007f1e5aa96bc0> #<Tag:0x00007f1e5aa96a80> #<Tag:0x00007f1e5aa96918>
(Slaviša Lukić) #1

I started today to investigate migrating from DSL to jsr223.

I followed instructions here: https://github.com/OH-Jython-Scripters/openhab2-jython/blob/master/Docs/Getting-Started.md

So far everything setup as suggested (though being in docker environment, managed it somehow).

I was then trying the hello-world.py example

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 hellowWorldCronDecorators(event):
    hellowWorldCronDecorators.log.info("Hello World!?!?!?")
   

but I only log entries that the script was execute, no “hello world” at all

==> /openhab/userdata/logs/openhab.log <==

2019-05-15 11:44:30.040 [DEBUG] [omation.core.internal.RuleEngineImpl] - The trigger 'Cron-hellowWorldCronDecorators-32a2678076f511e9839d2d236f51b29d' of rule '50f0c88c-9fcf-416b-b765-578b845303b3' is triggered.
2019-05-15 11:44:30.049 [DEBUG] [omation.core.internal.RuleEngineImpl] - The rule '50f0c88c-9fcf-416b-b765-578b845303b3' is executed.

running this one too:

from org.slf4j import Logger, LoggerFactory
log = LoggerFactory.getLogger("org.eclipse.smarthome.model.script.Rules")
import sys
log.debug("JSR223: version=[{}]".format(sys.version))
log.debug("JSR223: path=[{}]".format(sys.path)) 
log.debug("Hello World")

No output either

2019-05-15 11:47:49.264 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - in queue: {md=[file:/openhab/conf/automation/jsr223/personal/README.md, file:/openhab/conf/automation/jsr223/community/README.md]}
2019-05-15 11:47:49.271 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'personal/test.py'
2019-05-15 11:47:49.333 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: personal/test.py

What I am doing here wrong?

0 Likes

(marcel_erkel) #2

I haven’t done any JSR223 scripting lately and neither kept track of the changes made so I don’t know why it is not working. However, I’m sure Scott’s first question will be what version of openHAB you are running.

0 Likes

(Scott Rushworth) #3

Try…

log:set DEBUG jsr223.jython

I just updated the docs! Logging was an area I planned to focus on this weekend.

0 Likes

(Slaviša Lukić) #4

image

same result:

2019-05-15 14:52:18.066 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - in queue: {md=[file:/openhab/conf/automation/jsr223/personal/README.md, file:/openhab/conf/automation/jsr223/community/README.md]}
2019-05-15 14:52:18.083 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'personal/test.py'
2019-05-15 14:52:18.254 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: personal/test.py
0 Likes

(Scott Rushworth) #5

I’ll switch over to a 2.4 test environment and test it out…

0 Likes

(Slaviša Lukić) #6

WORKS!!!

For standard rules and logging with :

......
def hellowWorldCronDecorators(event):
    hellowWorldCronDecorators.log.info("Hello World!?!?!?")
.....
log:set DEBUG jsr223.jython

For

log = LoggerFactory.getLogger("org.eclipse.smarthome.model.script.Rules")
log.debug("Hello World!!")
log:set DEBUG org.eclipse.smarthome.model.script.Rules

is actualy logical, I guess

0 Likes

(Scott Rushworth) #7

:slightly_smiling_face: yes! For logging in your rule, the rule decorator adds a log attribute to the function you’ve defined for the action. So, as in the hello_world.py example, you can use…

@rule("Test rule")
@when("Item Test_Item changed")
def testRule(event)
    testRule.log("Test is a test")

If you want to log outside of a rule, you can use slf4j, but there is also a module for that. If you do something like this, you can just append to the default logger so that you don’t need to mess with the log levels…

from core.log import logging, LOG_PREFIX
log = logging.getLogger(LOG_PREFIX + ".TEST")
log.info("This is a test")
1 Like