[SOLVED] DHT22 - populating values in OH

Spent some time working on getting the DHT22 connected into oh. I have it connected directly into the pi and used the Adafruit script:

import Adafruit_DHT


while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

    if humidity is not None and temperature is not None:
        print("Temp={0:0.1f}*C  Humidity={1:0.1f}%".format(temperature, humidity))
        print("Failed to retrieve data from humidity sensor")

This works great. I can run the script in the console and receive the values.

Started down the path of adding it to oh. Followed the thread here

Created the sensor items as numbers and also tried as strings:

Number	greenhouse_DHT22Temp  "Greenhouse Temp [%.1f °C]"	
Number	greenhouse_DHT22Humidity "Greenhouse Humidity [%.1f %%]"

I used this rule from @spy0r:

rule "Greenhouse DHT22"
	Time cron "0 */5 * * * ?"
	val TEMP = executeCommandLine("python3 /etc/openhab2/scripts/AdafruitDHT.py", 5000)
	if (TEMP.toString().length <= 5) greenhouse_DHT22Temp.postUpdate(TEMP)
	val HUMID = executeCommandLine("python3 /etc/openhab2/scripts/AdafruitDHT.py", 5000)
	if (HUMID.toString().length <= 5) greenhouse_DHT22Humidity.postUpdate(HUMID)
	logInfo("Greenhouse Temperature", "Temperature: " + TEMP.toString() + "°C, Humidity: " + HUMID.toString() + "%")

Get this in the logs:

Cannot convert '' to a state type which item 'greenhouse_DHT22Temp' accepts: [DecimalType, QuantityType, UnDefType].

2020-03-06 13:30:15.148 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert '' to a state type which item 'greenhouse_DHT22Humidity' accepts: [DecimalType, QuantityType, UnDefType].

2020-03-06 13:30:15.157 [INFO ] [.model.script.Greenhouse Temperature] - Temperature: °C, Humidity: %

From what I have learned in my past endeavors is I need to specify the string “as a Number” or in this case assuming “as a DecimalType”. I tried a few things like this:

if ((TEMP.toString) as a DecimalType) ().length <= 5) greenhouse_DHT22Temp.postUpdate(TEMP)

No luck. I know, coding should not be luck but thats where I am.

Any help would be great!


If you would use a logInfo line to show the contents of TEMP after the executeCommand it would reveal the problem.
Do you really think that the same script would return once a temperature and after that a humidity?

Can you publish your values to mqtt? Then you may not need a rule just receive the values.

seems like your answer from the “executeCommandLine” command is empty. Maybe you have problems with the correct rights of your script and your openhab user?

You should try to do what @opus said and use a loginfo line after the executeCommandLine line.

Like @Thedannymullen said you also could use a different approach and broadcast your values with mqtt or even the rest interface of openhab to update your items (more or less the push version instead of the pull via script)

1 Like

You can post strings to Number Items. But they need to be numeric e.g “3.1417”. “Temp 21.0” would fail, as would “22.5 67.0”.

As they say, log out the return from your script and do some string manipulation to make it into something(s) suitable.

if it is standard Ada library https://github.com/adafruit/Adafruit_Python_DHT/blob/master/examples/AdafruitDHT.py
you have to parse your output in order to receive values.

standard output is

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

which you can’t use as you are trying to.

Thanks for all the input! I decided to go down the mqtt path. Oh boy. I managed to get a script running on the pi and it produces this:

./mqtt.dhtsensor.py 'greenhouse/temperature1' 'greenhouse/humidity1' 4
Mosquitto Temp MSG greenhouse/temperature1
Mosquitto Humidity MSG greenhouse/humidity1
Args length: 4
Logging sensor measurements to MQTT every 300 seconds.
Press Ctrl-C to quit.
Connecting to MQTT on
Date Time:   2020-03-09 23:33:32
Temperature: 21.50 C
Humidity:    41.80 %
Updating greenhouse/temperature1
Updating greenhouse/humidity1
MQTT Updated result 0 and 0
Wrote a message tp MQQTT
Date Time:   2020-03-09 23:38:34
Temperature: 21.40 C
Humidity:    42.40 %
Updating greenhouse/temperature1
Updating greenhouse/humidity1
MQTT Updated result 0 and 0

I used MQTT Fx and connected to the broker, subscribed and it publishes the values so that all looks good. That was a win, big win…

So going to openhab I have a broker setup and online. mqtt:broker:7e442392. All good there.

Try to setup a generic mqtt thing and I cannot get get it to connect. I add the thing, select the broker and put in greenhouse/temperature1 in the topic and leave the payload blank. I do not get the thing to go online. I did not create a thing file. I dont need it do i?


1 Like

Was able to get the “things” setup. But getting errors. This is what I have so far…

Bridge mqtt:broker:7e442392 "7e442392" [ host="", port=1883, secure=false]
    Thing topic temperature1 "Temperature" {
        Type number : temperature         "Temperature"     [ stateTopic="greenhouse/temperature1", transformationPattern="JSONPATH:$.DHT22.Temperature" ]
	Thing topic humidity1 "Humidity" {
        Type number : humidity         "Humidity"     [ stateTopic="greenhouse/humidity1", transformationPattern="JSONPATH:$.DHT22.Humidity" ]


Number greenhouse_temp "Greenhouse Temp [%.1f °C]" <temperature> {channel="mqtt:topic:7e442392:temperature1:temperature"}
Number greenhouse_humidity "Greenhouse Humidity [%.1f °C]" <humidity> {channel="mqtt:topic:7e442392:humidity1:humidity"}


2020-03-11 14:43:31.234 [WARN ] [t.generic.ChannelStateTransformation] - Executing the JSONPATH-transformation failed: Invalid path '$.DHT22.Temperature' in '20.3999996185'

2020-03-11 14:43:32.236 [WARN ] [t.generic.ChannelStateTransformation] - Executing the JSONPATH-transformation failed: Invalid path '$.DHT22.Humidity' in '50.0999984741'

Things are online. I researched through the JSONPATH error but not finding anything that relates.


So, what payload are you sending over MQTT now, may we see what you saw in mqtt.fx?

yes for sure, hope this is what you want…
Google Photos
Google Photos

Okay, That’s not JSON. Why would you run JSONPATH on it, it will … give an error that it cannot find the field you asked for in the plain number that you fed it.

1 Like

My guess is, the OP copied the setup from.an example meant for a Tasmota device, which his isn’t.

We’ll that was good to know! Thanks @rossko57. I stripped out the JSONPATH and it works just fine now. In retrospect that makes allot of sense. I will post the final here in a bit.

@opus you are correct. I do allot of cutting and pasting. I did a good bit of research on this as usual and did learn a good bit about JSONPATH but never really connected that this was not. Each time I try one of these I learn something new.

Once again, thanks for the help. Great to have this up and running!

1 Like

round your output to some meaningful decimals … like one max two, otherwise your item will be updating basically always…

Good point to consider. Look at “both ends”.

If you’re updating on a clock at the device end, you’ll be generating unnecessary network traffic. Consider only sending an update when value changes by more than 0.1 or whatever.

The openHAB end will process whatever it gets sent, so there will be an Item update per message.
However, if the Item update is to the same value it won’t get logged, you can control if it triggers rules (use changed not updated triggers) etc. So just reducing input decimals will help reduce workload a bit.

You can apply a transform in the binding to round incoming numbers, to get that small advantage without changing the sender device.

I hate to “throw data away” but c’mon the DHT22 is nowhere near that accurate, it’s rubbish data to begin with :wink:
I’d round to integer.