Jython helper libraries for OH3

In that case you are not running the oh3-patch branch that you think you are. Look at my post that I linked, you will find notes on installing in OH3 and a link to my fork where you will not need to checkout a different branch to get the updates.

1 Like

Hi Ivan,
thank you. With your fork that worked for me!
I thought that oh3-patch had all neccessary adjustments, but seems that there are still issues.
Thank you for your support!

That oh3-patch branch does have all necessary changes, I checked and updated it and then used those changes in my fork. I think maybe you had not checked out the branch when you copied the files to your installation.

Regardless, it is now outdated and you should use my fork until the main repo becomes active again and I get everything merged back.

1 Like

Does anyone know an easy way to get a time-zone aware datetime.now() from the Jython helper libraries?

ZonedDateTime.now()

Better to use the Java date stuff, the Python ones will just get converted to Java then back to Python anyway.

I was really hoping to stick to what I know with Python rather than have to learn how to use Java libraries. Ah, well!

Have you seen my Python stubs? They offer typing hints right in your IDE for the Helper Libraries, openHAB Core, and Java.

2 Likes

That’s a VSCode thing, right? I’m just starting to get my feet under me in Eclipse.

That screenshot is, I exclusively use VSCode now. There are other Python code highlighters and autocompletion tools though, I just don’t know what is available for Eclipse.

If you do choose to get that setup for Eclipse, please consider submitting instructions for that to the stubs repo!

Hi Ivan,
first rules are working, great!
Now I want to to start implementing a rule with the telegram binding. Therefore I try to import the lib to my code:

from core.actions import Telegram

But there was an error:

ImportError: cannot import name Telegram in at line number 3

How can I install such files?

@japhias looks like you are trying using the OH1.x Telegram binding action. In OH3 Telegram is using the new style Thing Actions. There are other examples of the MQTT2 binding if you want more examples, here is what you need for Telegram.

telegram = actions.get("telegram", "telegram:telegramBot:YOUR_BOT_ID")

telegram.sendTelegram(chatId, "message")
2 Likes

Is this manual patch still required? If so, any plans or threads about getting OH3 Jython working officially?

I didn’t add the patch yet, but I added the Jython Automation Add-on via: ->Settings->Automation->+ -> Jython Scripting, and am seeing errors with my 2.5 jython scripts, so guessing the patch is still needed…

This patch is not for Jython itself. It is for the jython helper libraries. It is required if you use the jython helper libraries. You can use Jython without the helper libraries if you like.

Yes, I misspoke. Finally got it working. Thanks so much for this! Had a tough time, but got my HelloWorld: (Any comments please chime in!)

#This script runs every 10 sec
from core.rules import rule
from core.triggers import when
from org.slf4j import LoggerFactory
@rule("Jython Hello World")
@when("Time cron 0/2 * * * * ?")
def hello_world_cron_decorators(event):
    LoggerFactory.getLogger("org.openhab.HelloWorldExample").info("Hello People") #works

You could do this instead:

#This script runs every 10 sec
from core.rules import rule
from core.triggers import when
@rule("Jython Hello World")
@when("Time cron 0/2 * * * * ?")
def hello_world_cron_decorators(event):
    hello_world_cron_decorators.log.info("Hello People") #works

See Logging — openHAB Helper Libraries documentation

I’m seeing these errors upon startup - Should these files be deleted or did I do something wrong? I admit I’m very hazy on the install - I came from OH2.5, then copied over your patch on top of what I already had (perhaps this is wrong).

Side note, anyway we can “easily” change this error to something more meaningful (this is maybe incorrect, but error that instead says “Method or Import does not exist”). I"m not sure if this can be done in the helper libraries or has to be done in openhab core…

Thanks!

2021-03-11 12:17:21.830 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/python/core/components/100_OsgiEventTrigger.py': java.lang.NullPointerException: java.lang.NullPointerException in <script> at line number 5
2021-03-11 12:17:21.833 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/etc/openhab/automation/jsr223/python/core/components/100_StartupTrigger.py'
2021-03-11 12:17:22.465 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/etc/openhab/automation/jsr223/python/core/components/200_JythonBindingInfoProvider.py'
2021-03-11 12:17:22.928 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/python/core/components/200_JythonBindingInfoProvider.py': java.lang.NullPointerException: java.lang.NullPointerException in <script> at line number 5
2021-03-11 12:17:22.929 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/etc/openhab/automation/jsr223/python/core/components/200_JythonExtensionProvider.py'
2021-03-11 12:17:23.413 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/python/core/components/200_JythonExtensionProvider.py': java.lang.NullPointerException: java.lang.NullPointerException in <script> at line number 6
2021-03-11 12:17:23.415 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/etc/openhab/automation/jsr223/python/core/components/200_JythonItemChannelLinkProvider.py'
2021-03-11 12:17:23.893 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/python/core/components/200_JythonItemChannelLinkProvider.py': java.lang.NullPointerException: java.lang.NullPointerException in <script> at line number 6
2021-03-11 12:17:23.895 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/etc/openhab/automation/jsr223/python/core/components/200_JythonItemProvider.py'
2021-03-11 12:17:24.334 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/python/core/components/200_JythonItemProvider.py': java.lang.NullPointerException: java.lang.NullPointerException in <script> at line number 6
2021-03-11 12:17:24.335 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/etc/openhab/automation/jsr223/python/core/components/200_JythonThingProvider.py'
2021-03-11 12:17:24.778 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/python/core/components/200_JythonThingProvider.py': java.lang.NullPointerException: java.lang.NullPointerException in <script> at line number 5
2021-03-11 12:17:24.780 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/etc/openhab/automation/jsr223/python/core/components/200_JythonThingTypeProvider.py'
2021-03-11 12:17:25.201 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/python/core/components/200_JythonThingTypeProvider.py': java.lang.NullPointerException: java.lang.NullPointerException in <script> at line number 5
2021-03-11 12:17:25.202 [INFO ] [ab.core.service.AbstractWatchService] - Loading script '/etc/openhab/automation/jsr223/python/core/components/200_JythonTransform.py'
2021-03-11 12:17:25.676 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/jsr223/python/core/components/200_JythonTransform.py': java.lang.NullPointerException: java.lang.NullPointerException in <script> at line number 7

Looks like you have leftover files from an older version. Wipe everything in automation/jsr223/core and automation/lib/core then follow the install instructions here Ivan’s Helper Libraries - OH3, Python, JavaScript

First, great work on the adaption and the binding :slight_smile:

Second, my question, i have a module which is under lib/personal. from this module i can’t user the Telegram action defined as it keeps saying global name ‘actions’ is not defined which is expected, but when i use “from core import actions” it also doesn’t work with the error “‘module’ object has no attribute ‘get’” which i can imagine is getting mixed with another module.

In short, is there a possibility to use binding actions from a module placed in lib/personal folder?
I tried importing “scope” but didn’t help.
By the way, it was working fine with OH2.5 and Telegram binding-1

Ok, i found out how to do it. I needed to import Things as in:

from core.actions import Things
telegram = Things.getActions("telegram", "telegram:telegramBot:Bot-UUID")
telegram.sendTelegram("Hello From Telegram")
4 Likes

@Mohamed_Negm I’m glad you found the solution! Sorry I didn’t get back to you sooner, I don’t have much time for the forum right now.

1 Like