DHT22 Temperature Sensor executeCommandLine

Hi,

i again have some issues withe the executeCommandLine command.
i use a DHT22 sensor for temperature and humidity. i have a python script for getting the temperature. when starting the python script in the command line with " AdafruitDHTTemp.py 22 5" i get the expected result:

openhabian@raspberrypi:~ $ AdafruitDHTTemp.py 22 5
12.6

if i start the command with a rule:

rule "DHT Aussen Luftfeuchtigkeit"
when 
	Item Test_Schalter_36 changed to ON 
then 
	var String humiOut = executeCommandLine(Duration.ofSeconds(5), "AdafruitDHTTemp.py", "22", "5");
	var double humiOutAsDouble = Float::parseFloat(humiOut);
	postUpdate(Luftfeuchtigkeit_Aussen, String.format("%.1f", humiOutAsDouble ));
end

the result in the log looks like this:

==> /var/log/openhab/openhab.log <==

2023-09-23 10:51:09.382 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'smartwomo-5' failed: For input string: "Traceback (most recent call last):

  File "/usr/local/bin/AdafruitDHTTemp.py", line 24, in <module>

    import Adafruit_DHT

ModuleNotFoundError: No module named 'Adafruit_DHT'" in smartwomo

what could be the problem? other scripts are working.

My guess: You’ll have to call python

executeCommandLine(Duration.ofSeconds(5), "/usr/bin/python3", "AdafruitDHTTemp.py", "22", "5")

also doesnt work. this is the log:

==> /var/log/openhab/openhab.log <==

2023-09-23 12:44:22.649 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'smartwomo-5' failed: For input string: "/usr/bin/python3: can't open file '/var/lib/openhab/AdafruitDHTTemp.py': [Errno 2] No such file or directory" in smartwomo

if i move the python file to /var/lib/openhab/ i get the same messege as before

  • as you can execute AdafruitDHTTemp.py as user openhabian without need to call python3 on command line I assume the first line of the script contains the path to the related python interpreted ( e.g. #!/usr/bin/python3 or any other installed version
  • this is why that also should work for openhab user
  • ModuleNotFoundError: No module named 'Adafruit_DHT' means the related python module Adafruit_DHT cannot be found by user openhab. If that module is installed relative to user environment of user openhabian it cannot be found by user openhab. The module needs either to be installed globally or relative to user openhab so that it can be found

ok first i installed the python modul with, which brought that error

sudo pip3 install --user adafruit-circuitpython-dht

now i tried

sudo pip3 install --system adafruit-circuitpython-dht

which doesnt change anything.
any other idea?

run

sudo su - openhab -s /bin/bash

You should have a prompt with openhab user privileges.Then run the python script.
What is the error message that you get ?

i did and installed the modul for user openhab. now i get the following error:

==> /var/log/openhab/openhab.log <==

2023-09-24 08:33:50.500 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'smartwomo-1' failed: For input string: "Traceback (most recent call last):

  File "/usr/local/bin/GetTempOut.py", line 21, in <module>

    temperature, humidity = dhtDevice.temperature, dhtDevice.humidity

  File "/root/.local/lib/python3.9/site-packages/adafruit_dht.py", line 273, in temperature

    self.measure()

  File "/root/.local/lib/python3.9/site-packages/adafruit_dht.py", line 257, in measure

    raise RuntimeError("Checksum did not validate. Try again.")

RuntimeError: Checksum did not validate. Try again.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "/usr/local/bin/GetTempOut.py", line 28, in <module>

    temperature, humidity = dhtDevice.temperature, dhtDevice.humidity

  File "/root/.local/lib/python3.9/site-packages/adafruit_dht.py", line 273, in temperature

    self.measure()

  File "/root/.local/lib/python3.9/site-packages/adafruit_dht.py", line 257, in measure

    raise RuntimeError("Checksum did not validate. Try again.")

RuntimeError: Checksum did not validate. Try again." in smartwomo

i also tried to do it with “sudo python /usr/local/bin/GetTempOut.py” this leads to an error, beause i get ask for the PW by changing visudo, so openhab doesnt need a PW, nothing changes.

Problem solved. I had to add the user openhab to group gpio.

1 Like