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
DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4
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))
else:
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"
when
Time cron "0 */5 * * * ?"
then
val TEMP = executeCommandLine("python3 /etc/openhab2/scripts/AdafruitDHT.py", 5000)
if (TEMP.toString().length <= 5) greenhouse_DHT22Temp.postUpdate(TEMP)
Thread::sleep(5000)
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() + "%")
end
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!
Thanks!