How I use DHT22 in Openhab 3

How can I use the DHT22 sensor with openhab3?

After trying some things with the DHT22 in openhab3 (openHAB 3.0.2), I turn to you.

I tried to bring the output value from my bash script, which is stored in /etc/openhab/scripts, with items and things into my openhab.
I also used the EXEC binding, but it only gives me strings, which is of little use for a temperature sensor, because I want to display the values in a diagram.

Unfortunately all without success.

That’s why I ask myself the question:
“How can I include the temperature sensor DHT22 or DHT11 in Openhab3 without much trouble?”.

What have you got the DHT22 connected to to be able to grab the environmental data?

Thanks @hafniumzinc for your quick feedback.

I have connected the DHT22 to a RaspberryPi 3 via the pins. To read the sensor I use the library “loldht” (https://github.com/technion/lol_dht22) to get stable values.

PIN=7
INPUT=$(/etc/openhab/lol_dht22/loldht $PIN | grep "Temperature")
NUM=1.00
HUMST=$(echo $INPUT|cut -d " " -f3)
TEMPST=$(echo $INPUT|cut -d " " -f7)
HUM=$(awk '{print $1*$2}' <<<"${NUM} ${HUMST}")
TEMP=$(awk '{print $1*$2}' <<<"${NUM} ${TEMPST}")

if [ "$1" = "HUM" ]; then
   echo $HUM
fi

if [ "$1" = "TEMP" ]; then
   echo $TEMP
fi

I call the script in the shell of the RaspberryPi (under raspbian) with the command:

/bin/bash /etc/openhab/scripts/dht22.sh TEMP

As result I get depending on which parameter I enter, “HUM” for the humidity or “TEMP” for my temperature. For example

55.7 (HUM) 
23.2 (TEMP) 

The Exec binding will only support String as an output. So you’ll need to transform the String to a Number, even better a Number:Temperature. You can use a profile for that I think though.

You’ll need to install the JS transformation and use a transformation along the lines of

(function(i) {
    return i + " °C" ;
})(input)

Make the Item be a Number:Temperature and I believe it will parse the string returned by the transformation.

If not you’ll have to write a Rule and use a proxy Item.

Hello @rlkoshak !
Thanks for this quick feedback.

You wrote

above, but what is meant by “proxy item”?

Do I call here as described in the doc of JS Transformation Service

String <itemName> { channel="<channelUID>"[profile="transform:JS", function="<filename>", sourceFormat="<valueFormat>"]}

or how is this meant?


I saved your code in a .js file named dht22.js in the transform folder.

I also created an item output_dht22.items in the items folder.
With this code:

String dht22 {channel="<channelUID>"[profile="transform:JS", function="dht22.js"]}

There I linked my file as described in the documentation. Am I correct so far?

By the way, my item that is not supposed to be the proxy item looks like this:

Number TEMP_AUSSEN "Temperatur Aussen [%.1f  °C]" <temperature> { exec="<[/etc/openhab/scripts/dht22.sh TEMP:REGEX((.*?))]" }

This forum’s search features are useful

Yes

No, you need to apply this to the Exec binding Channel.

That’s the OH 1.x exec binding config. It will not work with the 2.x/3.x version of the binding and it will not work in OH 3 at all. Review the Exec binding’s docs. You will see in that the example Items in the doc do not resemble that Item’s config in any way.

Do you mean the binding Exec - Bindings | openHAB or the transformation Exec - Transformation Services | openHAB here?
Which is the right one? I find it a little confusing.

The binding.

Bindings connect to technologies. In this case it’s connecting to a command line script and external programs.

A Transformation is used to transform the states of Items. The Exec transformation lets you pass the value to an external script or program to transform it to something else. You are not using an external script to transform the Item’s state.

Thank you @rlkoshak for the explanation.
It is a little more conclusive for me as a beginner in Openhab.
I think I will try binding

I have now adjusted the binding to my requirements. To keep you up to date here is the code:

Thing

Thing exec:command:myscript [command="bash /etc/openhab/scripts/dht22.sh TEMP %2$s", transform="REGEX((.*?))", interval=15, timeout=5]

Item

String APCRaw "[%s]" (All) {channel="exec:command:myscript:output"}

Hey @rlkoshak it works, or at least pretends to work.
When I have the thing like this I only get 0 out and not the numerical value of my script.
I think it’s because of REGEX unfortunately I have no idea about it because I’m a beginner.

I hope you can also help me in this case to get the dht22 running.
Greetings @3D_Schorsch

Make sure the REGEX transform is installed. I don’t think it’s required though.

I don’t mess with.things files so if you have an error there I can’t really help beyond saying almost all syntax errors are impossible when using the UI.

When things don’t work simplify. Keep the Item as String without any transformation. If that works add the transformation. If that works only then change to a Number:Temperature. If that doesn’t work then you’ll need to use a rule.

Sorry to be off topic but reading dht22 with raspberry pi 3 is not a bit of an overkill?
I would suggest using wemos d1 mini running tasmota with mqtt and everything become both cheap and easy to use

Sounds like they also run openHAB on the Pi, and are interested in the ambient temperature in the same area. Sounds appropriate - I’m doing the same as my Pi lives in the loft, but also runs other services.

@hafniumzinc yes that’s exactly how it is! i’m running openhab on the Raspberry Pi with the dht22 sensor connected to it.
If I want to use more DHT22 in the house in the future this is certainly a good idea @Marian_Grigore, I’ll keep that in mind.

@3D_Schorsch did you ever manage to solve the issue with the 0 as the response instead of the proper temperature?
I have tried to do the same but am stuck there.