DHT22 GPIO not reading in sitemap via Exec Binding

Hi.

Today connected DHT22 temp & humidity sensor and get readings via ssh on openhabian.

image

But I can not get readings in sitemap. Exec binding installed, on beginning I had and issue with whitelist but solved by adding command to the list. Command itself checked via ssh.

Thing:

Thing exec:command:dht [command="/usr/bin/python3 /home/fox/mydht2.py", interval=15, transform="REGEX((.*?))", autorun=true]

Item:

String Dht11 "[%.1f °C]" {channel="exec:command:dht:output"}

Sitemap:

Text item=Dht11 label="Dhtttt [%.1f °C]"

Added also last execution to check but also nothing appear
DateTime Dht111 "Last time executed [%1$tH:%1$tM]" {channel="exec:command:dht:lastexecution"}

I’m beginner so MQTT a little bit complicated for me - I would like to access that readings with simplest method. Spent all day reading posts but I can not figure out what’s wrong.

  • does the openhab user ( the user the openhab java process is running under ) have read access to /home/fox/mydht2.py ?
  • your python script seems to run in an endless loop; I would suggest to make it returning a single value
  • in first step use a script that emulates your script and returns a single ( random ) value; once that works continue using your script that reads from GPIO
  • openhab user has access privilegs to GPIO ?

Thank You for your support.

  • I have no idea how to check read access for openhab user
  • I changed my .py script, now looks like that

print ("20.3")

Via ssh return single value

image

  • openhab user has access privileges to GPIO

That all above changed results:


Now I have reading - but “0”.
items

Switch Dht22 {channel="exec:command:dht:run"}
DateTime Dht111 "Last time executed [%1$tH:%1$tM]" {channel="exec:command:dht:lastexecution"}
thing
```Thing exec:command:dht [command="/usr/bin/python3 /home/fox/mydht2.py", interval=15, transform="REGEX((.*?))", autorun=true]
sitemap
``` Text item=Dht11 label="Przyklad [%.0f]"
        Text item=Dht111
        Switch item=Dht22


So after script change something changed in sitemap but still no expected results??? Which script can be more simple?

I would suggest to modify the python script and update the item via REST API with your required frequency. I”m using the same method with a Wemos and a DS18B20. Another method is updating via mqtt.

Make the item in the sitemap:

Text item=Dht11 label="Przyklad [%s °C]"

and the item itself:

String Dht11 "[%s °C]" {channel="exec:command:dht:output"}

then you will see the values.

I was not able to format them as Number. According to other thread it looks like others have the same problem so it seems to be a bug.

Thank You Wolfgang_S :slight_smile:

things:

Thing exec:command:dht [command="/usr/bin/python3 /home/fox/mydht2.py", interval=30, transform="REGEX((.*?))", autorun=true]
    Thing exec:command:dht3 [command="/usr/bin/python3 /home/fox/mydht3.py", interval=10, transform="REGEX((.*?))", autorun=true]

items:

String Dht11 "Temperature [%s °C]"  {channel="exec:command:dht:output"}
String Dht33 "Humidity [%s %%]"     {channel="exec:command:dht3:output"}

sitemaps:

Text item=Dht11 label="Temperature [%s °C]"
Text item=Dht33 label="Humidity [%s %%]"

mydht2.py:

import Adafruit_DHT
 
# Set sensor type : Options are DHT11,DHT22 or AM2302
sensor=Adafruit_DHT.DHT22
 
# Set GPIO sensor is connected to
gpio=18
 
# Use read_retry method. This will retry up to 15 times to
# get a sensor reading (waiting 2 seconds between each retry). 
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
 
# Reading the DHT11 is very sensitive to timings and occasionally
# the Pi might fail to get a valid reading. So check if readings are valid.
if humidity is not None and temperature is not None:
  print('{0:0.1f}'.format(temperature, humidity))
else:
  print('Failed to get reading. Try again!')

mydht3.py:

import Adafruit_DHT
 
# Set sensor type : Options are DHT11,DHT22 or AM2302
sensor=Adafruit_DHT.DHT22
 
# Set GPIO sensor is connected to
gpio=18
 
# Use read_retry method. This will retry up to 15 times to
# get a sensor reading (waiting 2 seconds between each retry).
# humidity, 
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
 
# Reading the DHT11 is very sensitive to timings and occasionally
# the Pi might fail to get a valid reading. So check if readings are valid.
if humidity is not None and temperature is not None:
  print('{1:0.1f}'.format(temperature, humidity))
else:
  print('Failed to get reading. Try again!')`

Now working :grinning:

1 Like