Ivan’s Helper Libraries - OH3, Python, JavaScript

Thank you very much, that was really helpful. I have my first Javascript Rule up and running :wink:
Great Work!

1 Like

@nick_wootton thank you! I will see about getting that fixed tonight.

1 Like

@nick_wootton turns out it was just a missing ), all fixed now. Thanks again for spotting that.

1 Like

Just one additional question: I noticed that the file based JS rules are not displayed correctly in the OH Main UI. Only the trigger is displayed correctly, but not the source code:

Contrary to this, all my old file based Rules DSL rules are recognized by their type (vnd.openhab.dsl.rule) and show the source code:

The same problem exists when I access the rules via the REST API (which of course is the same what the Main UI does). The info for source code (actionscript) and rule type (actiontype) are not filled. Is there a chance to improve this? For JS rules that are defined via Main UI this information is displayed correctly, the problem exists only with file based JS rules with the helper libraries.

Just to clarify: The rules run perfectly, they are just not accessible through the UI or the REST API.

@vossivossi that is going to require some digging… Could you open an issue on github for this, I will take a closer look when I have time.

1 Like

@CrazyIvan359

Thank you for the work on this - I did a PR to update the documentation from referring to openhab2 in the directory structure.

It seems that the directory watcher for .py files only watches under the automation directory vs the lib directory.

Mike

@mjcumming thank you. Looks like your PR ended up on the original repo, Github will automatically compare to the origin of the fork so you have to change it when making a PR to my repo.

I had a look at your PR and it’s not quite right. You are correct that the directory changed, but there are several other changes to those instructions for OH3. In the first post of this topic I list the differences. I have not had time to change the docs in the repo, so if you wanted to do that for Python the PR would be welcome!

As for the directory watcher, I have noticed that it doesn’t actively watch in OH3, but it never looked in lib. The path to lib is, however, included in the sys.path of the Python emulator in OH2 and OH3 so that libraries there can be imported.

Any update on this?

I just finished a fresh 3.1 install. Here are the steps I used to get Jython working.

  1. Enable Jython scripting. From the UI, go to settings, automation add ons, enable Jython Scripting.

  2. Download the contents of Ivan’s library, copy the contents of the core into the configuration directory.

  3. Restart OpenHAB

  4. If needed, change the logging options as outlined here

1 Like

@mjcumming that’s basically the gist of it. The typings folder needs to be in automation though

Can you please provide your code! I would love to have my alarm back in working order. Thanks!

I have successfully installed the helper libraries and I am now porting my DSL rules.

I have one issue with org.joda.time. It is mentioned here:

Use org.joda.time.DateTime
from org.joda.time import DateTime
start = DateTime.now()

When I try to use it, I am getting the error:

Error during evaluation of script ‘file:/etc/openhab/automation/jsr223/python/personal/Squeeze.py’: ImportError: No module named joda in /etc/openhab/automation/jsr223/python/personal/Squeeze.py at line number 3

Could anyone help me how to fix this?

Thanks a lot in advance!

I think joda isn’t supported anymore. This is what I am using. e.g.

from java.time import LocalDate, LocalTime
now = LocalTime.now()
nowtime = LocalTime.of(now.getHour(), now.getMinute())

Hope that helps,
Stefan

Well, thanks!

The problem is that the timer I want to create according to this example gives this error:

TypeError: createTimer(): 1st arg can’t be coerced to java.time.ZonedDateTime

I am a little bit surprised because joda.time is explicitly mentioned in this tutorial:

https://openhab-scripters.github.io/openhab-helper-libraries/Guides/But%20How%20Do%20I.html

How can I get around this?

OK. I found to use this:

from java.time import ZonedDateTime

And in the code, I am using
ZonedDateTime.now()
instead of
DateTime.now()

Seems to work.

@schup011 the issue you are having is actually because you have moved to OH3, not directly to do with the libraries. You are correct in using ZonedDateTime like you show in your last post, I recommend using that everywhere for consistency.

The documentation you have linked is actually from the original repo not mine, but you would find the same on mine at this time. I have not had time to update any of the documentation to also include examples for OH3.

Ok, thanks a lot, Ivan, for clarifying, that’s fine.
I was just wondering whether my installation was somehow wrong or broken.
I can live with that without a problem, knowing now how to correct the code.

For anyone who needs to adapt his rules: I was using
DateTime.now()
before.
ZonedDateTime.now()
did not work either, because one has to provide the zone. So, one needs to use
from java.time import ZonedDateTime
from java.time import ZoneId
and then use
ZonedDateTime.now(ZoneId.systemDefault())

I posted another timer related question here:

Anyone who could help here? Thanks in advance!!

Would be very nice if you could share your changes :slight_smile: