Ivan’s Helper Libraries - OH3, Python, JavaScript

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:

Anyone else getting these problems with newest snapshot? Some rules don’t load anymore…

  File "/etc/openhab/automation/lib/python/core/triggers.py", line 353, in when

    elif target_type == "Item" and trigger_target not in ["added", "removed", "updated"] and old_state is not None and trigger_type == "changed" and TypeParser.parseState(itemRegistry.getItem(trigger_target).acceptedDataTypes, old_state) is None:

ClassCastException: java.lang.ClassCastException: class org.openhab.core.library.types.OnOffType cannot be cast to class org.openhab.core.types.State (org.openhab.core.library.types.OnOffType is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @6052c10b; org.openhab.core.types.State is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @64c724e2)

Edit: Ok just downgraded to 3.4-M4 but the problems still there. Maybe it is not related to M6.
But still anyone else having these issues?

Have you restarted the service at least once after upgrading/downgrading manually ?

Which service do you mean? If openhab as a whole, yes several times.

only as info:
i have openhabian with openhab 3.4.0.m5 installed and jython version 2.7.2.final running without problems.

Thanks for reporting. I updated to M6 this morning and then it started.
Unfortunatly problems persist now after downgrading to M5 again :frowning:

I had the same error after upgrading to M6. The mentioned line was 357 instead of 353, so I may have a different version of the helper libraries.
I did another restart of OH and than the error was gone. Btw. after a previous update I had the same error which was also gone “automagically”.

yesterday i updated to m6 and then i saw the same errors as you and @usambara reported in my log:

  File "/etc/openhab/automation/lib/python/core/triggers.py", line 357, in when
    elif target_type == "Item" and trigger_target not in ["added", "removed", "updated"] and new_state is not None and trigger_type == "received command" and TypeParser.parseCommand(itemRegistry.getItem(trigger_target).acceptedCommandTypes, new_state) is None:
ClassCastException: java.lang.ClassCastException: class org.openhab.core.library.types.DecimalType cannot be cast to class org.openhab.core.types.Command (org.openhab.core.library.types.DecimalType is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @4f4b1a; org.openhab.core.types.Command is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @46630b)

  File "/etc/openhab/automation/lib/python/core/triggers.py", line 353, in when
    elif target_type == "Item" and trigger_target not in ["added", "removed", "updated"] and old_state is not None and trigger_type == "changed" and TypeParser.parseState(itemRegistry.getItem(trigger_target).acceptedDataTypes, old_state) is None:
ClassCastException: java.lang.ClassCastException: class org.openhab.core.library.types.StringType cannot be cast to class org.openhab.core.types.State (org.openhab.core.library.types.StringType is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @4f4b1a; org.openhab.core.types.State is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @46630b)


  File "/etc/openhab/automation/lib/python/core/triggers.py", line 355, in when
    elif target_type == "Item" and trigger_target not in ["added", "removed", "updated"] and new_state is not None and (trigger_type == "changed" or trigger_type == "received update") and TypeParser.parseState(itemRegistry.getItem(trigger_target).acceptedDataTypes, new_state) is None:
ClassCastException: java.lang.ClassCastException: class org.openhab.core.library.types.OnOffType cannot be cast to class org.openhab.core.types.State (org.openhab.core.library.types.OnOffType is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @4f4b1a; org.openhab.core.types.State is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @46630b)
``
but i can confirm what usambara wrote: after another reboot of my rpi the errors dont appear again.

@CrazyIvan359 are you still maintaining the repo GitHub - CrazyIvan359/openhab-helper-libraries: JSR223-Jython scripts and modules for use with openHAB?
There is no activity there since 2 years?

I really like the feature - and the documentations - that is available there.

Or did I miss that it is now merged into OH3 - completely?