Closed rippe77 closed 4 months ago
With the fixed implementation f.e. following data is logged. [20:03:09][I][dht:220]: data[2] 0x0F, data[3] 0xED, raw_temperature 0x0FED [20:03:09][D][dht:048]: Got Temperature=-1.9°C Humidity=48.0% [20:03:09][D][sensor:094]: 'dht22_temp': Sending state -1.90000 °C with 1 decimals of accuracy [20:03:09][D][sensor:094]: 'dht22_hum': Sending state 48.00000 % with 1 decimals of accuracy
That doesn't match the datasheet for the sensor and no one has mentioned this before. Are you sure that's the sensor you have?
The sensors are freshly ordered from china and should be DHT22/AM2302. True the data format does not match the datasheet as it seems to be 12-bit 2-complement format. And the DHT_MODEL_DHT22_TYPE2 assumes 16-bit 2-complement format. I searched the internet, and did not find any DHT22 "variants" with this 12-bit 2-compelement format specified in the datasheet.
Maybe the correct fix would anyway be on to the "TYPE2" branch...
Revised fix is to add following lines:
//Some DHT22 models seem to be returning 12-bit 2-complement numbers, this fixes them to 16-bit, but this also works with models with 16-bit 2-complement data.
if (this->model_ == DHT_MODEL_DHT22_TYPE2 && (raw_temperature & 0x0800) != 0)
raw_temperature = raw_temperature | 0xF000;
And then use model: DHT22_TYPE2 in the configuration Yaml.
I've noticed this issue as well. Is there any plan to commit a change?
Can you find any reference to why the sensor is providing values like that? The commit for the TYPE2 variant is very specific about that negative calculation. Maybe this needs to be a TYPE3.
I have not yet found any datasheet that explains the sensor behaviour.
This could also be TYPE3, but I think the code I provided in comment https://github.com/esphome/issues/issues/5322#issuecomment-1873844830 would work with both the original TYPE2 sensors and these sensors behaving wierdly (as the datarange is only -40 -- 80 C deg).
However I can not test this with normal TYPE2 sensors as I do not have any.
I noticed the same behavior. Temperatures dropped below zero for the first time since I started playing with ESPhome a couple of months ago. Values around 408 returned. ESPhome version 2023.12.5.
Please test the PR with any DHT22 sensors you have.
Tested the PR against ESPHome 2023.12.7. Works fine.
The problem
It seems the negative temperatures are calcalated incorrectly. The problem seems to be with dht.cpp lines 220-221: if (this->model_ != DHT_MODEL_DHT22_TYPE2 && (raw_temperature & 0x8000) != 0) raw_temperature = ~(raw_temperature & 0x7FFF);
Correct implementation seems to be: if (this->model_ != DHT_MODEL_DHT22_TYPE2 && (raw_temperature & 0x0800) != 0) raw_temperature = raw_temperature | 0xF000;
Which version of ESPHome has the issue?
2023.12.5
What type of installation are you using?
Home Assistant Add-on
Which version of Home Assistant has the issue?
2023.12.4
What platform are you using?
ESP32
Board
ESP-32 30PIN
Component causing the issue
dht22
Example YAML snippet
Anything in the logs that might be useful for us?
Additional information
No response