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

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!")
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.
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

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…

And this is the lib/python directory:

[20:57:23] openhabian@openhab:~$ ls -axl /etc/openhab2/automation/lib/python
total 36
drwxr-xr-x 5 openhabian openhab 4096 May 7 12:50 .
drwxr-xr-x 4 openhabian openhab 4096 May 7 12:48 ..
-rw-r--r-- 1 openhabian openhab 1499 Jan 28 14:42 bolbro.py
drwxr-xr-x 2 openhabian openhab 4096 Dec 8 2019 community
-rw-r--r-- 1 openhabian openhab 148 Jan 17 12:31 configuration.py
drwxr-xr-x 3 openhabian openhab 4096 Dec 8 2019 core
drwxr-xr-x 2 openhabian openhab 4096 Jan 18 16:56 personal

bolbro.py is a module that probably should be in personal.

The scripts are currently copied from the repo manually and periodically need to be copied again to get the latest. I have updated some scripts recently and the 000_startup_delay.py script has been removed. I haven’t seen a need for this script in a long while, so you could remove it. This may need to go back in if people report issues with it not being there, which may be the case for people running DSL rules too. I will create a release when the updates are all pushed, update the beta Jython addon, and then get some forum posts out. I am working on including the core scripts inside the addon, to make updates easier.

It does not look like requests and its dependencies were installed into /etc/openhab2/automation/lib/python. If you find where it went, just copy it in there. You could also copy it from your Python2 directory. Please open a new topic to discuss further, as this does not pertain to the addon.

I have created a new topic here: Installing external modules to jython beta

I will try the straight copy for one of the instances found but hope to get this sorted out in general.

1 Like

@5iver Scott, installed the bundle on a new OH install (2.5.6) and am getting the error below when the date module is imported.

2020-07-02 17:57:55.712 [ERROR] [eclipse.smarthome.model.script.Rules] - Traceback (most recent call last):

  File "<script>", line 8, in <module>

  File "/etc/openhab2/automation/lib/python/personal/occupancy/occupancy_manager.py", line 28, in <module>

    import personal.occupancy.areas.area_manager

  File "/etc/openhab2/automation/lib/python/personal/occupancy/areas/area_manager.py", line 13, in <module>

    import personal.occupancy.areas.area

  File "/etc/openhab2/automation/lib/python/personal/occupancy/areas/area.py", line 22, in <module>

    from core.date import seconds_between

  File "/etc/openhab2/automation/lib/python/core/date.py", line 43, in <module>

    from org.openhab.core.library.types import DateTimeType as LegacyDateTimeType

ImportError: No module named library

Try running this script and post the log…

I can’t reproduce this, with or without a 1.x binding installed.