HABApp - Easy automation with openHAB

I’ve done a full reboot now, but still get the same error.

How can I check that it is available to the python in the virtual environment?

openhabian@openhab:~ $ pip show requests
Name: requests
Version: 2.25.1
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: /usr/local/lib/python3.7/dist-packages
Requires: urllib3, idna, certifi, chardet
Required-by:

I’m on openhabian and installed HABapp through the openhabian menu.

You are installing the requests package in the system wide python and not in the virtual environment of HABApp.

You have to activate the virtual environment first to install into it.
Follow the steps in installation, except step 2, 6 and 7 (since everything is already available).
Then do
python3 -m pip install requests
and restart HABApp

OK, understand. However now I’m getting an error

(habapp) openhabian@openhab:/opt/habapp $ python3 -m pip install  requests
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting requests
  Using cached https://www.piwheels.org/simple/requests/requests-2.25.1-py2.py3-none-any.whl (61 kB)
Collecting idna<3,>=2.5
  Using cached https://www.piwheels.org/simple/idna/idna-2.10-py2.py3-none-any.whl (58 kB)
Collecting urllib3<1.27,>=1.21.1
  Using cached https://www.piwheels.org/simple/urllib3/urllib3-1.26.3-py2.py3-none-any.whl (137 kB)
Collecting certifi>=2017.4.17
  Using cached https://www.piwheels.org/simple/certifi/certifi-2020.12.5-py2.py3-none-any.whl (147 kB)
Requirement already satisfied: chardet<5,>=3.0.2 in ./lib/python3.7/site-packages (from requests) (3.0.4)
Installing collected packages: urllib3, idna, certifi, requests
ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: '/opt/habapp/lib/python3.7/site-packages/urllib3'
Consider using the `--user` option or check the permissions.

Hmm, this seems to be a permission error.
Try “sudo python3 -m pip install requests”

Then it goes back out of the virtual env?

(habapp) openhabian@openhab:/opt/habapp $ sudo python3 -m pip install requests
[sudo] password for openhabian:
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (2.25.1)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from requests) (2018.8.24)
Requirement already satisfied: chardet<5,>=3.0.2 in /usr/lib/python3/dist-packages (from requests) (3.0.4)
Requirement already satisfied: idna<3,>=2.5 in /usr/lib/python3/dist-packages (from requests) (2.6)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/lib/python3/dist-packages (from requests) (1.24.1)

What happens if you restart HABApp now?

Somewhat related, and mybe flawed;
Because my head is teflon coated and polished, I made a script to handle the venv stuff.
I do not know venv good enuogh to say it is a good idea, but I hope I can get some feedback if not :wink:

nils@xps13:~/habapp$ cat upgrade.sh 
#!/bin/bash
source bin/activate
pip install -r lokallibs.txt
python3 -m pip install --upgrade habapp
deactivate

nils@xps13:~/habapp$

It installs/upgrades the libs listed in
lokallibs.txt before it upgrades HABApp.
I run the script as the user HABApp is running as.

This might be the key! Could you show the call as an example?

Still the same unfortunately.

On my laptop, where I work on the rules, HABApp is running as me:

cd habapp
./upgrade.sh

For my OpenHAB server:

  • I ssh into On my OpenHAB machine as “habapp” (The user HABApp is running as)

  • Stop the systemd service “habapp”

  • cd into the directory where HABApp is installed.

  • Run the update script

  • If everything is OK, start the systemd service “habapp”

Where can I find the password? I don’t think I’ve ever seen or chosen it.

I think you are running HABApp as the user “openhab” since you installed through openhabian.
Could you try installing requests while being in the folder /opt/habapp/bin ?
It seems to not pick up the venv :confused:

I figured it out!

I again thought about the “Permission denied” error.
It did try to install it in the correct directory! So it must have picked up the venv.

(habapp) openhabian@openhab:/opt/habapp $ pip install requests
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting requests
  Using cached https://www.piwheels.org/simple/requests/requests-2.25.1-py2.py3-none-any.whl (61 kB)
Requirement already satisfied: chardet<5,>=3.0.2 in ./lib/python3.7/site-packages (from requests) (3.0.4)
Collecting idna<3,>=2.5
  Using cached https://www.piwheels.org/simple/idna/idna-2.10-py2.py3-none-any.whl (58 kB)
Collecting urllib3<1.27,>=1.21.1
  Using cached https://www.piwheels.org/simple/urllib3/urllib3-1.26.3-py2.py3-none-any.whl (137 kB)
Collecting certifi>=2017.4.17
  Using cached https://www.piwheels.org/simple/certifi/certifi-2020.12.5-py2.py3-none-any.whl (147 kB)
Installing collected packages: urllib3, idna, certifi, requests
ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: '/opt/habapp/lib/python3.7/site-packages/urllib3'
Consider using the `--user` option or check the permissions.

It also didn’t work with the --user

(habapp) openhabian@openhab:/opt/habapp $ pip install --user requests
ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.

So I checked the folder permissions.
It was missing the “w” write permission for group and others:

(habapp) openhabian@openhab:/opt/habapp $ deactivate
openhabian@openhab:/opt/habapp $ ls -l
total 20
drwxr-xr-x 2 root root 4096 Feb  1 16:23 bin
drwxr-xr-x 2 root root 4096 Feb  1 16:22 include
drwxr-xr-x 3 root root 4096 Feb  1 16:22 lib
-rw-r--r-- 1 root root   69 Mar  9 13:54 pyvenv.cfg
drwxr-xr-x 3 root root 4096 Feb  1 16:22 share

So I changed that (using chmod -R recursive mode)

openhabian@openhab:/opt/habapp $ sudo chmod -R u=rwx,g=rwx,o=rwx /opt/habapp/
openhabian@openhab:/opt/habapp $ sudo ls -l
total 20
drwxrwxrwx 2 root root 4096 Feb  1 16:23 bin
drwxrwxrwx 2 root root 4096 Feb  1 16:22 include
drwxrwxrwx 3 root root 4096 Feb  1 16:22 lib
-rwxrwxrwx 1 root root   69 Mar  9 13:54 pyvenv.cfg
drwxrwxrwx 3 root root 4096 Feb  1 16:22 share

Try install again - IT WORKS!

openhabian@openhab:/opt/habapp $ source bin/activate
(habapp) openhabian@openhab:/opt/habapp $ pip install requests
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting requests
  Using cached https://www.piwheels.org/simple/requests/requests-2.25.1-py2.py3-none-any.whl (61 kB)
Collecting idna<3,>=2.5
  Using cached https://www.piwheels.org/simple/idna/idna-2.10-py2.py3-none-any.whl (58 kB)
Collecting urllib3<1.27,>=1.21.1
  Using cached https://www.piwheels.org/simple/urllib3/urllib3-1.26.3-py2.py3-none-any.whl (137 kB)
Collecting certifi>=2017.4.17
  Using cached https://www.piwheels.org/simple/certifi/certifi-2020.12.5-py2.py3-none-any.whl (147 kB)
Requirement already satisfied: chardet<5,>=3.0.2 in ./lib/python3.7/site-packages (from requests) (3.0.4)
Installing collected packages: urllib3, idna, certifi, requests
  Attempting uninstall: idna
    Found existing installation: idna 3.1
    Uninstalling idna-3.1:
      Successfully uninstalled idna-3.1
Successfully installed certifi-2020.12.5 idna-2.10 requests-2.25.1 urllib3-1.26.3

Also shows up in the pip list now:

(habapp) openhabian@openhab:/opt/habapp $ pip list
Package            Version
------------------ ---------
aiohttp            3.7.3
aiohttp-sse-client 0.2.0
astral             2.2
async-timeout      3.0.1
attrs              20.3.0
bidict             0.21.2
certifi            2020.12.5
chardet            3.0.4
EasyCo             0.2.3
HABApp             0.20.0
idna               2.10
multidict          5.1.0
paho-mqtt          1.5.1
pip                21.0.1
pkg-resources      0.0.0
pydantic           1.7.3
pytz               2020.5
requests           2.25.1
ruamel.yaml        0.16.12
ruamel.yaml.clib   0.2.2
setuptools         40.8.0
stackprinter       0.2.5
typing-extensions  3.7.4.3
tzlocal            2.1
ujson              4.0.1
urllib3            1.26.3
voluptuous         0.12.1
watchdog           1.0.2
yarl               1.6.3

Thanks again for the efforts!

3 Likes

Hi,
what is the smartest way to build a common state over group items. I would like to have an item that indicates, for example, whether all the lights (gLights) are off or all windows (gWindow) are closed.

A simple way is to group the items within a group. One example. I have on some windows windows contacts for the left and the right wing. So i creates a group with an Or function. So if one of the window wings is open the group (window) is open too.


Hello Thomas,
thanks, that helped a lot! The best rule is the one that is not needed! :smile:

Hey Seb, so I dived into the HABApp event code, and to be honest I was lost for quite a while. I started by tracing to the source file containing ValueUpdateEvent and ValueChangeEvent which doesn’t map clearly to the various OH item events. It took me a while to stumble on item_events.py which contains the complete mappings to all OH events.

You have lots of abstraction there, and it was a bit hard for me to follow without design doc. I was essential looking forthe class ItemCommandEvent.

In any case, I’ve created a pull request to clarify the doc a bit.

Thanks,

Hai

Depends what you want to do. If you just want to get the state and show it then a group aggregation is fine.
But if you want to do some logic you’ll typically already have a rule set up with both items being listened to so there is no need for the group aggregation because you trigger on every change anyway.

Hmmm, if you use an IDE it provides auto complete to and under HABApp.openhab.events.
That’s where you find the event filters, too. Are you using an IDE?
There is no need to import directly from a file in HABApp, everything is mapped to the module.
But you are right, the openHAB events are missing from the docs.

Yea - it took quite some time to build :wink:

Now my plan is to aggregate the state of all items via group aggregation and set an eventlisten on that group item. With this setup I can avoid to create an eventlisten for every single group member.

Seb,

I think the equality check you have for some items is too selected and can cause hard to debug error.

For example, for SwitchItem, you have this:

    def __eq__(self, other):
        if isinstance(other, SwitchItem):
            return self.value == other.value
        elif isinstance(other, str):
            return self.value == other
        elif isinstance(other, int):
            if other and self.is_on():
                return True
            if not other and self.is_off():
                return True
            return False

        return NotImplemented

Your equality only concerns about the state of the switch. However, in the many cases we also care about the backed OpenHab items, which is inherently part of the object’s state.

This design allows two totally different switch items to be equals if their states are equals. I stumbled on this bug once couple months back, and I just stumbled on it again today. It took me a while to figure out the cause. I believe that equality should at least be the state and the item name.

What do you think?