AM2302 ESPHome Compatibility Problem

So, I’ve been using these AM2302 sensors because I like having access to temperature and humidity readings all over the house and they seem to work great in ESPHome and Home Assistant. Many months ago I bought about five of these devices from AliExpress. They all work great with ESPHome and I ran out because they’re so awesome. I recently purchased two more from a different vendor on AliExpress (XY Electronic Components) and I can’t seem to get them to work in ESPHome. So, I’ve been debugging it and I’ve uncovered some interesting data.

I built a very simple Wemos D1 Mini breadboard test platform that just uses D3 (which has a built in pull up resistor)


When I flash it with ESPHome using a simple platform: dht sensor definition it works great with the old sensors and doesn’t work with the new ones.

With the new sensor:

With the old sensor:

I noticed that the new sensor looks pretty cheap (no markings and the holes aren’t very clean/defined).

Here’s where it gets crazy. If I use the Arduino IDE and upload a sketch to this simple D1 mini breadboard, both sensors work great!! So, does that mean something is wrong with ESPHome’s dht library?

#include "DHT.h"
#define DHTPIN 0
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");
  dht.begin();
}

void loop() {
  delay(2000);

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Unable to read DHT probe!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
}

Yields this output using the new sensor that doesn’t work in ESPHome:

11:10:43.810 -> Humidity: 39.80 %	Temperature: 25.40 *C 77.72 *F	Heat index: 25.03 *C 77.06 *F
11:10:45.814 -> Humidity: 39.70 %	Temperature: 25.40 *C 77.72 *F	Heat index: 25.03 *C 77.06 *F
11:10:47.817 -> Humidity: 39.60 %	Temperature: 25.40 *C 77.72 *F	Heat index: 25.03 *C 77.05 *F
11:10:49.820 -> Humidity: 39.50 %	Temperature: 25.40 *C 77.72 *F	Heat index: 25.03 *C 77.05 *F
11:10:51.819 -> Humidity: 39.50 %	Temperature: 25.40 *C 77.72 *F	Heat index: 25.03 *C 77.05 *F
11:10:53.823 -> Humidity: 39.50 %	Temperature: 25.40 *C 77.72 *F	Heat index: 25.03 *C 77.05 *F
11:10:55.822 -> Humidity: 39.50 %	Temperature: 25.40 *C 77.72 *F	Heat index: 25.03 *C 77.05 *F
11:10:57.853 -> Humidity: 39.60 %	Temperature: 25.30 *C 77.54 *F	Heat index: 24.92 *C 76.86 *F

Thanks,
-Greg

1 Like

That DHT in your picture labelled “broken” is clearly a counterfeit. Look at the quality of the case.

1 Like

Thanks @tom_l, I figure as much since I bought it from China and it came on the slow boat but why would it work with the Arduino IDE and not with ESPHome?

Not 100% sure, but sometimes increasing the delay between readings helps. In that arduino code, it is 2sec. I haven’t dug through the how the esphome libraries are implemented, but I doubt this can be done in yaml config… probably would need to modify the dht library that esphome uses.

Also, kind of is what it is… you get what you pay for and risk what you risk. I used to penny pinch a lot like that… but after so many fails I think a few $ more is well worth saving the headaches/delays of dealing with ‘cheaper’ parts. It may be easier to just toss those in the trash and learn from the experience, rather than trying hard to get them working like they should and diluting the lesson.

Isn’t that update_interval? I have it set to 5s in my ESPHome YAML file:

esphome:
  name: attictemp1
  platform: ESP8266
  board: d1_mini

wifi:
  networks:
  - ssid: !secret basement_wifi_ssid
    password: !secret wifi_password
  domain: !secret wifi_domain

logger:
  level: DEBUG

api:

ota:

sensor:
  - platform: dht
    pin: D3
    model: AM2302
    temperature:
      name: "Temperature"
    humidity:
      name: "Humidity"
    update_interval: 5s

On the Arduino IDE sample I used above it was 2s and that was working.

No I think update interval is just how often it deals with HA, not how the esp deals with the dht internally. For example, you can setup running average output, etc… for most sensors. That requires multiple reads in background before sending to HA. Timing of these things are controlled by the libraries, not yaml.

There is likely a tight delay used in the esphome dht library, that could be adjusted to work for you. This isn’t a desirable setup even of you do patch it… requires using custom libraries and keeping those up to date with the rest of the code. Frankly, if it was me those sensors would be returned or trashed.

@truglodite, if you’re right that’s probably what I’ll do. I’m not a fan of customizing stuff just to make it work.

Try changing the model in your ESPHOME config.

Are you talking about the board: tag or the platform: tag? I had tried using board: nodemcuv2 but that didn’t fix it. Any specific suggestions? It looks like there are quite a few options to choose from…

I just got a few in the mail but haven’t tried them out yet. I only went to China because they’re so pricey on Amazon. Hopefully the batch of lolin nodemcu’s i ordered aren’t garbage too :frowning:

This

model: AM2302

Try the other models. DHT22, or DHT11.

There is also DHT22_TYPE2 in dev release

1 Like

I tried AUTO_DETECT, DHT11, DHT22, RHT03, and SI7021 – all of them result in this type of response:

[06:35:20][W][dht:160]: Requesting data from DHT failed!
[06:35:20][W][dht:060]: Invalid readings! Please check your wiring (pull-up resistor, pin number).
[06:35:20][D][sensor:092]: 'Temperature': Sending state nan °C with 1 decimals of accuracy
[06:35:20][D][sensor:092]: 'Humidity': Sending state nan % with 0 decimals of accuracy
[06:35:25][W][dht:160]: Requesting data from DHT failed!
[06:35:25][W][dht:060]: Invalid readings! Please check your wiring (pull-up resistor, pin number).
[06:35:25][D][sensor:092]: 'Temperature': Sending state nan °C with 1 decimals of accuracy
[06:35:25][D][sensor:092]: 'Humidity': Sending state nan % with 0 decimals of accuracy

I wonder when this will make it into the prod release. I’ve not used dev before, do I need to remove prod and install dev or is dev an adder to the normal installation?

You need to stop the ESPHome add-on, install and start the dev one. Recompile the firmware and upload it.

If you want to go back, stop the dev addon and start the first one

Ok, I tried it out with the dev version of ESPHome (I really like the tighter layout of devices!!) and DHT_TYPE2 as the model lo and behold it works!

[22:06:47][D][dht:048]: Got Temperature=27.0°C Humidity=38.7%
[22:06:47][D][sensor:092]: 'Temperature': Sending state 27.00000 °C with 1 decimals of accuracy
[22:06:47][D][sensor:092]: 'Humidity': Sending state 38.70000 % with 0 decimals of accuracy
[22:06:52][D][dht:048]: Got Temperature=27.0°C Humidity=38.7%
[22:06:52][D][sensor:092]: 'Temperature': Sending state 27.00000 °C with 1 decimals of accuracy
[22:06:52][D][sensor:092]: 'Humidity': Sending state 38.70000 % with 0 decimals of accuracy
[22:06:57][D][dht:048]: Got Temperature=27.0°C Humidity=38.7%
[22:06:57][D][sensor:092]: 'Temperature': Sending state 27.00000 °C with 1 decimals of accuracy
[22:06:57][D][sensor:092]: 'Humidity': Sending state 38.70000 % with 0 decimals of accuracy

@makai, is there any drawback to running the dev version? I might have to do that from now on if I don’t run across any issues. Unfortunately, it still has the ESPhome freezes during compile. :cry:

Thanks for the suggestion!

2 Likes

Nice!
You may encounter some bugs maybe. I have no issue here.

There may be undocumented changes and possibly some stability issues, but I’ve never had an issue with the short periods of time that I used it.

Not beside the possibilities of broken things. But in general dev is quite stable.

It is even possible to run esphome and esphome dev in parallel (each has it’s own hassio addon). They will not conflict and having mixed versions registered in home assistant also has no drawbacks.

I’m running both versions in parallel since a long time because I always have some bleeding edge nodes running the dev version. No problems what so ever

1 Like

Is there a reason that the dev version reinstalls itself when I click restart? I noticed that the dev version takes a LOT longer to fire up than the prod version. Looking at the logs, it appears to reinstall the whole ESPHome dev app on a click of the restart button. Is that by design for some reason?

Yes that would be by design to keep up to date with any recently merged PR’s.