[beta testers wanted!] Jython addon w/ helper libraries (requires OH 2.5.x)

I did and I got it to work with that now after also following the pointers from @noppes123 . I had messed up the path in my post and written $OPENHAB_USERDATA, but as you said it should be $OPENHAB_HOME, so I corrected my post you referenced here.

One more question, which is a bit off topic of setting this up: I have written a python script for controlling by beamer screen (move it up/down). Where would I put this script (which is not a rule), so that I can call it from a rule? A link to the relevant info for getting started would be much appreciated!

I suggest making this into a module and putting it into $OPENHAB_CONF/automation/lib/python/personal/. Or maybe just add it to your script as a function that is called from the rule. If you have more questions about using Jython, please start a new topic (tag it with jython) and I’ll be glad to help. There is some info here.

I’m reporting on a fresh first time install on openhabian/RPi4. hello_world.py runs and the only error in the log was mentioned in January as a non critical error of the OsgiEventTrigger:

2020-04-30 21:38:00.994 [ERROR] [ion.module.script.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/python/core/components/100_OsgiEventTrigger.py': ImportError: cannot import name osgi_triggers in <script> at line number 17
1 Like

Hello, coming from [JSR223] [JYTHON] Jython with requests successful? my question is how to add support for the requests module to this beta jython installation?

I have tried

pip install --target=/etc/openhab2/automation/lib/jython requests

and it installs successfully. However, it seems to make no difference to the jython environment coming with the beta? Once I add import requests I get an error message in the logs saying requests is not available.

Thanks for a pointer.

See this thread for more info on requests. After going to a bit of trouble installing it, I found that are some incompatibilities that made it not useful for what I wanted - authorization. Fortunately @JimT came up with a way to get extra headers into the sendHttpGetRequest. So, the question is what do you want to do with the requests library?

Yes, I had referenced that thread and worked through it :frowning:

requests is what I believe is the most general library for doing HTTP request in python? I have seen the tweaks to sendHttpGetRequest() discussed, but I actually would prefer to not use a proprietary extension for a pretty general topic if not necessary.

I have working python code using requests I’d like to integrate into my openHAB installation. It utilizes POST and GET requests with data and headers. I have tried urllib2 and got it working for python. However, it bombs when using the same script in the jython environment (urllib2 is available in the jython instance coming with the beta). I have tried sendHttpGetRequest() too, but it didn’t work for me either. I’m pretty sure it is about how data and headers are passed into the requests, but I have tried what I could think of…

So that’s the reason I asked for instructions how to install requests to the beta jython.

@HarryS1, I’d be happy to help with this, but please open a new topic for it. When installing requests, use this which will reinstall any dependencies into the location specified…

sudo -u openhab pip2 install --ignore-installed --install-option="--prefix=/etc/openhab2/automation/lib/python" requests

/etc/openhab2/automation/lib/jython means nothing to the beta Jython addon, since it uses a path inside the addon for python.home.

So is the command listed supposed to make requests available in the beta environment or not? I ran it and restarted openHAB in addition to be sure. But it is still not available.

I have one stupid question just to have a shortcut for a solution. When I restart my openhab it loads all DSL rules but does not load my new Python rules from C:\openhab2\conf\automation\jsr223\python\personal When I open and save my rules, they are reloded and works, but I need to do it after all startup. What should I check to make it loaded automated?

This is a bug that was fixed, but will not be included in a release until OH 3.0. However, I posted a jar with the fix for OH 2.5…

1 Like

Beta is running fine and I’m using it to actively migrate my DSL rules before moving across to a prod Jython environment.

I’m now looking at using some additional libraries that I’d normally install with pip (or pip3), but although they appear to install, I am unable to reference/import them in my code.

I see that Scott mentioned:

So how do I use pip install to ‘push’ libraries into the Beta python.home?

In that post I provided a way to install to /automation/lib/python/, which is already included in python.path by the addon. Another option is to install the package normally and add the path to it into the python.path through EXTRA_JAVA_OPTS.

Ah, OK, so that command will then allow Jython Beta to import the libraries. Thanks

1 Like

I would like to create rule what works with hour of the day: now.getHourOfDay()
I try to use DateTime() in several way with .hour but I receive error message:
AttributeError: ‘org.joda.time.DateTime’ object has no attribute ‘hour’

I have the following imports in my rule:

from core.rules import rule
from core.triggers import when
from core.actions import ScriptExecution
from org.joda.time import DateTime

Test rule looks like this:

rule("ZWaveNode120ZW100Multisensor6AlarmMotion trigger ON", description="Turn on SofaLight_Switch after ZWaveNode120ZW100Multisensor6AlarmMotion sensor is triggered", tags=["Tag 1", "Tag 2"])
@when("Item ZWaveNode120ZW100Multisensor6AlarmMotion changed to ON")

def hello_world_cron_decorators(event):
    if items["ZWaveNode120ZW100Multisensor6AlarmMotion"] == ON:
        hello_world_cron_decorators.log.info("Hello World!")
        hello_world_cron_decorators.log.info(DateTime())
2020-06-21 06:56:29.128 [INFO ] [00Multisensor6AlarmMotion trigger ON] - Hello World!
2020-06-21 06:56:29.128 [INFO ] [00Multisensor6AlarmMotion trigger ON] - 2020-06-21T06:56:29.128+02:00

and I get back the DateTime just I can’t extract the hour from that expression.

Since this has nothing to do with the beta Jython addon, please open a new topic (DateTime().getHourOfDay() or DateTimeType().zonedDateTime.hourOfDay()).

1 Like

Thanks! It works now. Next time I’ll open separate topic.

Oddly, this didn’t work for me. When issuing the command, I get

[12:29:49] openhabian@openhab:~$ sudo -u openhab pip2 install --ignore-installed --install-option="--prefix=/etc/openhab2/automation/lib/python" requests
[sudo] password for openhabian: 
/usr/lib/python2.7/dist-packages/pip/_internal/commands/install.py:222: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options.
  cmdoptions.check_install_build_global(options)
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting requests
  Downloading https://files.pythonhosted.org/packages/da/67/672b422d9daf07365259958912ba533a0ecab839d4084c487a5fe9a5405f/requests-2.24.0.tar.gz (115kB)
    100% |████████████████████████████████| 122kB 942kB/s 
Collecting certifi>=2017.4.17 (from requests)
  Downloading https://files.pythonhosted.org/packages/40/a7/ded59fa294b85ca206082306bba75469a38ea1c7d44ea7e1d64f5443d67a/certifi-2020.6.20.tar.gz (157kB)
    100% |████████████████████████████████| 163kB 958kB/s 
Collecting chardet<4,>=3.0.2 (from requests)
  Using cached https://files.pythonhosted.org/packages/fc/bb/a5768c230f9ddb03acc9ef3f0d4a3cf93462473795d18e9535498c8f929d/chardet-3.0.4.tar.gz
Collecting idna<3,>=2.5 (from requests)
  Using cached https://files.pythonhosted.org/packages/cb/19/57503b5de719ee45e83472f339f617b0c01ad75cba44aba1e4c97c2b0abd/idna-2.9.tar.gz
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 (from requests)
  Using cached https://files.pythonhosted.org/packages/05/8c/40cd6949373e23081b3ea20d5594ae523e681b6f472e600fbc95ed046a36/urllib3-1.25.9.tar.gz
Skipping bdist_wheel for requests, due to binaries being disabled for it.
Skipping bdist_wheel for certifi, due to binaries being disabled for it.
Skipping bdist_wheel for chardet, due to binaries being disabled for it.
Skipping bdist_wheel for idna, due to binaries being disabled for it.
Skipping bdist_wheel for urllib3, due to binaries being disabled for it.
Installing collected packages: certifi, chardet, idna, urllib3, requests
  Running setup.py install for certifi ... done
  Running setup.py install for chardet ... done
  Running setup.py install for idna ... done
  Running setup.py install for urllib3 ... done
  Running setup.py install for requests ... done
Successfully installed certifi-2020.6.20 chardet-3.0.4 idna-2.9 requests-2.24.0 urllib3-1.25.9

Now, adding import requests into a script yields this error in the openhab log:

2020-06-21 12:32:35.246 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/python/personal/default.py': ImportError: No module named requests in <script> at line number 12

What do you see in /etc/openhab2/automation/lib/python/, and what do you see in sys.path? I recently added a script that may be helpful (not in add-on yet)…

1 Like

This is the output of 000_startup.py:

2020-06-22 20:58:27.912 [INFO ] [me.core.service.AbstractWatchService] - Loading script 'python/core/000_startup.py'
2020-06-22 20:58:28.048 [WARN ] [jython.Startup                      ] - 
*******************************************************************************
Jython version:       2.7.0.final
Operating system:     Linux
OS Version:           4.19.97-v7l+
Java vendor:          Azul Systems, Inc.
Java VM name:         OpenJDK Client VM
Java runtime name:    OpenJDK Runtime Environment
Java runtime version: 1.8.0_252-b225
sys.path:             /etc/openhab2/automation/lib/python
                      /var/lib/openhab2/cache/org.eclipse.osgi/203/0/bundleFile/Lib
                      __classpath__
                      __pyclasspath__/
*******************************************************************************

Not sure I’m on the latest beta version… The directory I copied 000_startup.py to had the file 000_startup_delay.py for my installation. This file is not in this folder in GitHub…