Permission denied when running script via exec binding

Hello,
My OpenHAB 3.x is on Raspberry Pi with openhabian. When I run my local script it outputs temperature from sensor in °C as a number. But when I run same script (it is whitelisted) via exec binding I get this in logs:

  File "/usr/local/bin/run-sht20-temp", line 6, in <module>

    sht = SHT20(1, resolution=SHT20.TEMP_RES_14bit)

  File "/usr/local/lib/python3.7/dist-packages/sht20/sht20.py", line 59, in __init__

    self.bus = SMBus(port)

  File "/usr/local/lib/python3.7/dist-packages/smbus2/smbus2.py", line 280, in __init__

    self.open(bus)

  File "/usr/local/lib/python3.7/dist-packages/smbus2/smbus2.py", line 310, in open

    self.fd = os.open(filepath, os.O_RDWR)

PermissionError: [Errno 13] Permission denied: '/dev/i2c-1'

Traceback (most recent call last):

  File "/usr/local/bin/run-sht20-temp", line 6, in <module>

    sht = SHT20(1, resolution=SHT20.TEMP_RES_14bit)

  File "/usr/local/lib/python3.7/dist-packages/sht20/sht20.py", line 59, in __init__

    self.bus = SMBus(port)

  File "/usr/local/lib/python3.7/dist-packages/smbus2/smbus2.py", line 280, in __init__

    self.open(bus)

  File "/usr/local/lib/python3.7/dist-packages/smbus2/smbus2.py", line 310, in open

    self.fd = os.open(filepath, os.O_RDWR)

PermissionError: [Errno 13] Permission denied: '/dev/i2c-1'

It looks like problems with permissions. I have already tried to give the script chmod +x and also chmod u+r+x but did not help.
Also tried to put “sudo” everywhere into script.
I am newbie in Linux could you please give me some hints how to get that script running via exec binding?

Edit: My whitelisted script has name “get-data.sh” and contains:

#!/bin/bash
echo $(run-sht20-temp)

It takes data from "run-sht20-temp " which contains:

#!/usr/bin/python3

from sht20 import SHT20
from time import sleep

sht = SHT20(1, resolution=SHT20.TEMP_RES_14bit)

temp = sht.read_temp()

#   or

data = sht.read_all()
temp = data[0]
print(temp)

I have only gave extra chmod permissions to my whitelisted script: “get-data.sh”.
Should I also give extra permissions to these python scripts which process sensor values?

The script needs to have rx permissions for user resp. group openhab as OH runs with that user privileges.
But it looks the script tries to access the I2C device and this raises the permission denied.
Which group is required to access the /dev/i2c-1 device ? ls -l /dev/i2c-1
Does the user OH belong to a group that is allowed to access the device ?

It looks like this:

[20:12:31] root@openhabian:/dev# ls -l /dev/i2c-1
crw-rw---- 1 root i2c 89, 1 Aug 19 19:09 /dev/i2c-1
[20:12:43] root@openhabian:/dev# 

The user i2c seems to have read/write access to /dev/i2c-1.
Add user openhab to group i2c.

:grinning: THANKS

Added user openhab to group i2c

sudo adduser openhab i2c

and then reboot.
It works fine then.

1 Like