Jython helper libraries for OH3

@Malti1812 this looks like you are using JavaScript or a JavaScript Transformation, please open a new topic for that. Also please use code fences when posting code.

My problem is only in DateTime timestart
and DateTime timeend which was called earlier from joda

@CrazyIvan359’s repository here: GitHub - CrazyIvan359/openhab-helper-libraries: JSR223-Jython scripts and modules for use with openHAB has some more updates to the helper libraries, so I would recommend using his repo instead, at least until there’s an update from the original repository.

1 Like

Dear all,
I still try to get jython running the first time on OH3.0.1.
What I have doen:

  • Installed the automation package as documented
  • Copied and replaced the files in automation/jsr223 and automation/lib by the OH3-patch files from github
  • adjusted the hello_world.py file to “from java.time import ZonedDateTime”

from java.time import ZonedDateTime
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!")

The result in the log is still:

[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 at line number 7

Can someone help?
Thank you

I did exactly the same as you and everything works fine, so that should be a solvable problem…

What exactly is line 7 in your rule file?

And just to be sure and because I sometimes make this mistake myself: Are you sure, that you edited the correct file? Maybe you have changed the file in /etc/openhab2 and not /etc/openhab, e.g. because a network share still points to the old location?

The hello_world.py example script, and all the other example scripts have not received updates for OH3.

Give the lack of activity on the main Helper Libraries repo, I have started maintaining my own fork with updates to make them compatible with OH3 among other things. The example scripts have not been updated their either, but the libraries have been tested to work with OH3.

1 Like

Line 7 says: from core.triggers import when
I’m very sure that I work on the right instance

But it seems that I have an issue with the import of core.triggers. I thought that was patched 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.