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.
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:
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.
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.
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
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?
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.
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
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?