[SOLVED]Cant get data for DHT22 sensor

I am trying to add a temperature sensor to openhab but i cant get data from the script when executing it in openhab.

The last image is form the terminal when openhab is running and not getting any data.

Thanks for any help :slight_smile:

I’m pretty sure you have to call python for using python scripts. You could set the item Temp to String to see, if there is any output from the script, even errors.

I am still getting the same result

Please try to post text, it’s way easier to read (especially if at phone
)

I think you didn’t get the point :wink: your Item should be like

Number Temp "Temperature [%.1f °C]" <temperature> (Kjeller) {exec="<[/usr/bin/python /opt/openhab/configurations/scripts/TempTest.py:10000:REGEX((.*?))]"}

so call the python interpreter and consign the script (the complete filename, please)

Maybe it’s necessary to replace the space with @@, so it would look like [..]in/python@@/opt/openhab/conf[..]

1 Like

Thank you so much for your help. I am now getting the data into openhab :smiley: I am new to openhab/programming in general and there is much to learn :wink:

Could you please write down the solution? just in case, another user has similar problems


I did just what you wrote above. adding the [..] /usr/bin/python@@/opt/openhab/conf[..] and it worked.

Thanks!

1 Like

Hi sir, How can I write a demo.scipts instead of TempTest.py. I want to get data of DHT22 that connected with GPIO on Raspberry running Openhab

An other approach is to run the default python script and add to it some lines of Python to publish temperature and pressure to an mqtt server.
Then install mqtt binding at OH and add the items temperature and pressure as numbers.

Step by step

  1. install adafruit dht library
    https://github.com/adafruit/Adafruit_Python_DHT

  2. install mtqq server
    https://github.com/eclipse/paho.mqtt.python

  3. copy adafruit example AdafruitDHT.py to /home/pi
    https://github.com/adafruit/Adafruit_Python_DHT/tree/master/examples

and add these lines at the bottom:

import paho.mqtt.client as mqtt

mqttc = mqtt.Client(“mosquitto_pub”)

mqttc.connect(“localhost”, 1883)

mqttc.publish(“home/temperature”, “{0:0.1f}”.format(temperature, humidity))

mqttc.publish(“home/humidity”, “{1:0.1f}”.format(temperature, humidity))

mqttc.loop(2)

if humidity is not None and temperature is not None:
print ‘Temperature={0:0.1f}* Humidity={1:0.1f}%’.format(temperature, humidity)

else:
print 'Failed to get reading. Try again!'
sys.exit(1)

  1. add mqtt binding at openhab ADDONS directory and configure mqtt at openhab.cfg

mqtt:mymosquitto.url=tcp://localhost:1883

mqtt:mymosquitto.retain=true

  1. Add .py script to cron for every 5 minutes and define the gpio pins that sensor is connected at the end e.g. “22 4” by typing
    sudo crontab - l

5,10,15,20,25,30,35,40,45,50,55 * * * * sudo /home/pi/AdafruitDHT.py 22 4

  1. Add the items to home.items

Number HomeTemperature “Temperature [%.2f °C]” (sensors) { mqtt="<[mymosquitto:home/temperature:state:default]" }

and

Number HomeHumidity “Humidity [%.2f %]” (sensors) { mqtt="<[mymosquitto:home/humidity:state:default]" }

Enjoy