DHT22 humidity out of range

Hi, what can I change in the following program so that humidity below 30% and above 55% do not appear? I am trying to get rid of false readings, e.g. 250% …
I tried to add
if humidity is not None and humidity> 30 and humidity <55:
but it didn’t work. of false readings, e.g. 250% …

Can you show the exact and complete code that failed to work as expected?

BTW, you should preferably use “is None” instead of “== None” because there are subtle differences between them. And I recommend to increase the sleep time to 2+ seconds.

This is the code from this tutorial:

That is only showing the original code, not the exact modified code… :wink:

Just to be sure: you added the 10k pull-up resistor correctly? Faulty readings can also be caused by wrong ‘wiring’.

I have a resistor added. Erroneous readings occur once every few hours and occur on a 20 cm long cable. Replacing the sensor with another one did not help. In another switchboard on openhab1 and RPi2 the formula helped:
if humidity is not None and humidity> 30 and humidity <55:
Now I have Openhab2 and RPi3 and unfortunately this code does not work here.

Not sure what exactly you want to get out of the script you showed, but if this is your code, it will not retry when you have a false reading, it will just not print it. Or are you capturing the output of the script?

BTW, why a sleep before printing the value? Or is that in case the script gets called again within 2 seconds?

This is the code that the site I gave earlier.

Understood, but like I said, that code should just exits the script in case of a false reading, without any output.

But what are you doing with the script or the output? How do you trigger it? And what output exactly is showing? If the humidity reading is false, it should not print anything, just exit.

The script runs automatically:

There is also a role

When the Python script you showed gets a false humidity reading, it should just exist without printing a value, so the exec result will be an empty string. Does it trigger an Item state change in that case?
You can add logging to the rules:

logInfo("HumidityUpdate", "Humidity item is [{}]", humidity_out)

to see what happens.

Also, can you post your complete and exact Python script as is, using python code fences because in python indentation does matter a lot.

This is the complete code:

#!/usr/bin/env python3
import Adafruit_BMP.BMP085 as BMP085
import Adafruit_DHT
import sys, time

DHT_PIN = 23                    # GPIO nr
DHT_SENSOR = Adafruit_DHT.DHT22     
if __name__ == '__main__':
    if len(sys.argv) > 1:
        call = sys.argv[1].lower()
        if call == 'temperature':
            temperature = None
            while temperature == None:
               _, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
               if temperature == None:
        elif call == 'humidity':
            humidity = None
            while humidity == None:
               humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
               if humidity > 30 and humidity > 55 :
        elif call == 'pressure':
            sensor = BMP085.BMP085()
            print(sensor.read_pressure() / 100.0)

This should be ‘<30 or >55’ I supose? And it doesn’t filter out false readings because either way the while loop terminates.
Try this:

while humidity is None:
    humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
    if humidity < 30 or humidity > 55:
        humidity = None
1 Like