Avoid the Invalid readings! in a DTH sensor

hi, i have some dht22 connected to an esp32 that sometimes freeze and go into error.
Esphome detects the error with " Invalid readings! Please check your wiring (pull-up resistor, pin number) ".
Is there a way to use a lambda expression and the on_value function to avoid sending the Invalid reading error to homeassistant and instead remains the last value read correctly?
i try with:

- platform: dht
    model: DHT22_TYPE2
    pin: GPIO19
    temperature:
      name: "Temperatura ${name}" 
      id: dht22_t1
      on_value:
        lambda: |-
          if (!(isnan(x))) {
            id(dht22_t1).publish_state(x);
          }
    humidity:
      name: "Umidita ${name}" 
      id: dht22_h1
    update_interval: 120s

but the esp32 explodes and becomes unreachable

Did you try
filter_out: nan
?

1 Like