How can I get data of DHT22 that connected to GPIO on Raspberry running Openhab

Could you write a demo for me (scripts, items, sitemaps)?!. Because I don’t understand how to use a Python script and MQTT. I a new in Openhab, I only control mode of LED via GPIO. Please help me @mr_eskildsen

Unfortunately writing demo scripts is a minor part of getting the suggested setup up and running OUTSIDE openHAB.
The DHT 22 sensor in’t straightforward, and I guess that knowledge about scripting (Python) would be helpfull.

First of all you will need a running instance of a MQTT server. That could be installed on you Raspberry,there is a lot of step by step guides around, here is one of them:

I haven’t tried that link, so I am not sure if it works, but in that case, Google is your best friend…

When MQTT is running you should decdie how you will read values from DHT22, a lot of samples for raspberry Pi is laying around, not just Python, but also others. Pick your favourite and test that it reads the sensor as expected. Then add MQTT publishing to the script.

Now you are ready to read the sensor values from MQTT. In openHAB, you find good examples under the Wiki for each Binding (MQTT Binding).

Hope that helps in pointing out a direction, but unfortunately it will involve a lot of work…

1 Like

Thank you for your time, I’m going to try it. See you again in other topic :slight_smile:

If you are having Openhab on the same raspberry pi where you have your sensor connected, why don’t you use the gpio binding? It’s the best solution for your setup!

1 Like

As far as i knowhow the DHT22 sensor can’t just jeg real by one reading. It need some processing , Which isn’t (IMHO) worth trying from a openhab rule, As far as I know, there isn’t any support in the GPIO Binding for reading the DHT22 sensor directly???

1 Like

Seriously? Why don’t you trying a little legwork for yourself. From this very forum: Read Temperature with Python and forward to MQTT. No you can’t use the GPIO binding because, to the best of my knowledge, that only detects high and low. Not enough to read a DHT22.


I have read DHT11 (or DHT22) successly.
This is my way that I have referred in other topic.
In items:

   Number Temp "Temperature [%.2f   C]" <temperature> {exec="<[/usr/bin/python /opt/openhab/configurations/scripts/*?))]"}


import sys
import Adafruit_DHT

sensor = Adafruit_DHT.DHT11
pin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
     print('Failed to get reading. Try again!')

I have installed AdafruitDHT library for Raspberry and used Exec binding

Thank you so much, I will try to read data of DHT with MQTT.



Do you have puthon script to read Humidity?

Hi @daskalov_daskalov,
Above, it will return both humidity and temperature. But I only use temperature
If you want to get humidity. you must replace this line


instead of


or you can get both by:

print('Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temperature, humidity))
1 Like

@Nguyen_Dang_Nhut_Tan Nguyen can you post a “complete” step by step tutorial?
i follow your instructions but nothing happens in oh2.
if i start the pythonscript manually via ssh/terminal on my win-computer, i can see temp/humidity …but nothing in oh2 :frowning:

You likely need to change the regex expression in the items file. Try just reading the temperature rather than both and see if that works. If it does then that is likely your problem. If you return both as demonstrated in the code the regex for temp will need to select the first number and the regex for humidity will need to select the second number.

sry im a absolutely openhab2 newbie :frowning:
all what i have done are the steps what referred from nguyen.

Number Temp "Temperature [%.2f C]" <temperature> {exec="<[/usr/bin/python /opt/openhab/configurations/scripts/*?))]"}
python script
import sys
import Adafruit_DHT

sensor = Adafruit_DHT.DHT11
pin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
     print('Failed to get reading. Try again!')

all what i see in openhab logs is "item temp removed…"
maybe you can help me

I have a similar approach and read the sensor values via SSH in a shell script. I paired the OpenHAB Server and the Raspberry with SSH-Keys and run something like that in a rule:

val TEMP = executeCommandLine("/x/x/openhab/runtime2/conf/scripts/", 5000)
if (TEMP.toString().length <= 5) tmp_keller.postUpdate(TEMP)

where the shell script is something like:

case $1 in
        TEMP) raw=$(ssh pi@192.168.x.x "sudo ./" | awk '{print $1}')
        HUMID) raw=$(ssh pi@192.168.x.x "sudo ./" | awk '{print $2}')
echo "$raw"

The python script on the Raspberry is the example from Adafruit. Very easy and works perfectly, maybe not the most professional way, but okay for me.


can you read local sensors with your ssh script?
maybe you can post a tutorial from your ssh setup

no, local on the raspberry, which has the DHTxx to his GPIO connected, i read the data with this example from adafruit:

then i pair the OpenHAB Server with the raspberry via ssh keypairs (German:

then i can execute that adafruit example remotely with my shell script from the OpenHAB Server and work with the answer and put them into my items via rules…


sorry, i didn’t see that you have the DHT connected on your openhab server…
Then you only need to take the adafruit example, put your sensor and sensorpin in there and execute it from within a rule!

you can build for example 2 *.py files, one for the temperature and one for the humidity and then get your values like i do within a rule

thats what i trying…but it doesnt works.
can you give me a rule and item example?
i can read all data via ssh and the example adafruit script, no problem.
but there are no data in oh2.

Try this:


Number	tmp_keller					"Keller [%.1f °C]"	
Number	humidity_keller		  			"Keller [%.1f %%]"


rule "Keller"
	Time cron "0 */5 * * * ?"
	val TEMP = executeCommandLine("/", 5000)
	if (TEMP.toString().length <= 5) tmp_keller.postUpdate(TEMP)
	val HUMID = executeCommandLine("/", 5000)
	if (HUMID.toString().length <= 5) humidity_keller.postUpdate(HUMID)
	logInfo("Keller", "Temperatur: " + TEMP.toString() + "°C, Feuchte: " + HUMID.toString() + "%")

wtf…it works now :scream:
1000 x thx fo your help.
next step: some graphs, but not today anymore :slight_smile:

sry but these are my first steps with openhab.
i understand the item, sitemap things but i should work with rules-wiki.

also dann danke nochmals stefan!!!

Cool! Glad i could help! Feel free to ask if there are questions within that small code!

Gerne, dafür ist das Ganze hier :wink:

Today i test the script, and i have strange reading from DHT11 sensor, the value of temperature and humidity from the sensors are floating. If temperature is 9 degree Celsius one reading is 9 next reading is 4. and humidity is the same if humidity is 35% one reading is 35% some readings after is 140%. I use DHT11 reading every 5 seconds, power sensors with 3.3V and i have resistor 4K8 to VCC and Signal. Trying also to read sensor every 60sek… the same think, down on the link is picture of the chart. Is anybody have idea whats wrong? what have to check?