esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
290 stars 34 forks source link

dht22 wrongly reported negative temperatures #1744

Closed nettle-m closed 2 years ago

nettle-m commented 3 years ago

Operating environment/Installation (Hass.io/Docker/pip/etc.):

Home Assistant OS 5.9 as KVM virtual ESP (ESP32/ESP8266, Board/Sonoff):

esp32doit-devkit-v1 ESPHome version (latest production, beta, dev branch)

ESPHome version 1.15.3 Affected component:

DHT22 https://esphome.io/components/sensor/dht.html

Description of problem: sensor is working fine when temp is above 0C but after temp drops below zero it starts to output wrong values. for example -3276.80005C but those values are correlated with temperature. I think it is some kind of conversion/overflow bug.

Problem-relevant YAML-configuration entries:

esphome:
  name: puda_mala_teplomer
  platform: ESP32
  board: esp32doit-devkit-v1

wifi:
  ssid: "***"
  password: "***"

  # Optional manual IP
  manual_ip:
    static_ip: 192.168.2.41
    gateway: 192.168.2.254
    subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Garaz Teplomer Fallback Hotspot"
    password: "***"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "***"

ota:
  password: "***"

sensor:
  - platform: dht
    pin: GPIO05
    temperature:
      name: "mapa_puda Temperature"
    humidity:
      name: "mala_puda Humidity"
    update_interval: 20s
    model: DHT22

Logs (if applicable):

INFO Reading configuration /config/esphome/puda_mala_teplomer.yaml...
INFO Starting log output from 192.168.2.41 using esphome API
INFO Connecting to 192.168.2.41:6053 (192.168.2.41)
INFO Successfully connected to 192.168.2.41
[17:47:40][I][app:105]: ESPHome version 1.15.3 compiled on Jan  7 2021, 17:09:49
[17:47:40][C][wifi:443]: WiFi:
[17:47:40][C][wifi:303]:   SSID: 

[redacted]
[17:47:40][C][wifi:304]:   IP Address: 192.168.2.41
[17:47:40][C][wifi:306]:   BSSID: 

[redacted]
[17:47:40][C][wifi:307]:   Hostname: 'puda_mala_teplomer'
[17:47:40][C][wifi:311]:   Signal strength: -43 dB ▂▄▆█
[17:47:40][C][wifi:315]:   Channel: 1
[17:47:40][C][wifi:316]:   Subnet: 255.255.255.0
[17:47:40][C][wifi:317]:   Gateway: 192.168.2.254
[17:47:40][C][wifi:318]:   DNS1: 0.0.0.0
[17:47:40][C][wifi:319]:   DNS2: 0.0.0.0
[17:47:40][C][logger:185]: Logger:
[17:47:40][C][logger:186]:   Level: DEBUG
[17:47:40][C][logger:187]:   Log Baud Rate: 115200
[17:47:40][C][logger:188]:   Hardware UART: UART0
[17:47:40][C][dht:017]: DHT:
[17:47:40][C][dht:018]:   Pin: GPIO5 (Mode: INPUT)
[17:47:40][C][dht:024]:   Model: DHT22 (or equivalent)
[17:47:40][C][dht:027]:   Update Interval: 20.0s
[17:47:40][C][dht:029]:   Temperature 'mapa_puda Temperature'
[17:47:40][C][dht:029]:     Unit of Measurement: '°C'
[17:47:40][C][dht:029]:     Accuracy Decimals: 1
[17:47:40][C][dht:029]:     Icon: 'mdi:thermometer'
[17:47:40][C][dht:030]:   Humidity 'mala_puda Humidity'
[17:47:40][C][dht:030]:     Unit of Measurement: '%'
[17:47:40][C][dht:030]:     Accuracy Decimals: 0
[17:47:40][C][dht:030]:     Icon: 'mdi:water-percent'
[17:47:40][C][captive_portal:169]: Captive Portal:
[17:47:40][C][ota:029]: Over-The-Air Updates:
[17:47:40][C][ota:030]:   Address: 192.168.2.41:3232
[17:47:40][C][ota:032]:   Using Password.
[17:47:40][C][api:095]: API Server:
[17:47:40][C][api:096]:   Address: 192.168.2.41:6053
[17:47:46][D][dht:048]: Got Temperature=12.7°C Humidity=94.5%
[17:47:46][D][sensor:092]: 'mapa_puda Temperature': Sending state 12.70000 °C with 1 decimals of accuracy
[17:47:46][D][sensor:092]: 'mala_puda Humidity': Sending state 94.50000 % with 0 decimals of accuracy
[17:48:06][D][dht:048]: Got Temperature=13.8°C Humidity=93.8%
[17:48:06][D][sensor:092]: 'mapa_puda Temperature': Sending state 13.80000 °C with 1 decimals of accuracy
[17:48:06][D][sensor:092]: 'mala_puda Humidity': Sending state 93.80000 % with 0 decimals of accuracy
[17:48:26][D][dht:048]: Got Temperature=14.3°C Humidity=80.3%
[17:48:26][D][sensor:092]: 'mapa_puda Temperature': Sending state 14.30000 °C with 1 decimals of accuracy
[17:48:26][D][sensor:092]: 'mala_puda Humidity': Sending state 80.30000 % with 0 decimals of accuracy
[17:48:46][D][dht:048]: Got Temperature=13.2°C Humidity=56.6%
[17:48:46][D][sensor:092]: 'mapa_puda Temperature': Sending state 13.20000 °C with 1 decimals of accuracy
[17:48:46][D][sensor:092]: 'mala_puda Humidity': Sending state 56.60000 % with 0 decimals of accuracy
[17:49:06][D][dht:048]: Got Temperature=11.6°C Humidity=52.4%
[17:49:06][D][sensor:092]: 'mapa_puda Temperature': Sending state 11.60000 °C with 1 decimals of accuracy
[17:49:06][D][sensor:092]: 'mala_puda Humidity': Sending state 52.40000 % with 0 decimals of accuracy
[17:49:26][D][dht:048]: Got Temperature=10.0°C Humidity=51.3%
[17:49:26][D][sensor:092]: 'mapa_puda Temperature': Sending state 10.00000 °C with 1 decimals of accuracy
[17:49:26][D][sensor:092]: 'mala_puda Humidity': Sending state 51.30000 % with 0 decimals of accuracy
[17:49:46][D][dht:048]: Got Temperature=8.5°C Humidity=52.8%
[17:49:46][D][sensor:092]: 'mapa_puda Temperature': Sending state 8.50000 °C with 1 decimals of accuracy
[17:49:46][D][sensor:092]: 'mala_puda Humidity': Sending state 52.80000 % with 0 decimals of accuracy
[17:50:06][D][dht:048]: Got Temperature=7.2°C Humidity=56.1%
[17:50:06][D][sensor:092]: 'mapa_puda Temperature': Sending state 7.20000 °C with 1 decimals of accuracy
[17:50:06][D][sensor:092]: 'mala_puda Humidity': Sending state 56.10000 % with 0 decimals of accuracy
[17:50:26][D][dht:048]: Got Temperature=6.1°C Humidity=58.0%
[17:50:26][D][sensor:092]: 'mapa_puda Temperature': Sending state 6.10000 °C with 1 decimals of accuracy
[17:50:26][D][sensor:092]: 'mala_puda Humidity': Sending state 58.00000 % with 0 decimals of accuracy
[17:50:46][D][dht:048]: Got Temperature=5.0°C Humidity=59.9%
[17:50:46][D][sensor:092]: 'mapa_puda Temperature': Sending state 5.00000 °C with 1 decimals of accuracy
[17:50:46][D][sensor:092]: 'mala_puda Humidity': Sending state 59.90000 % with 0 decimals of accuracy
[17:51:06][D][dht:048]: Got Temperature=4.2°C Humidity=63.4%
[17:51:06][D][sensor:092]: 'mapa_puda Temperature': Sending state 4.20000 °C with 1 decimals of accuracy
[17:51:06][D][sensor:092]: 'mala_puda Humidity': Sending state 63.40000 % with 0 decimals of accuracy
[17:51:26][D][dht:048]: Got Temperature=3.4°C Humidity=65.3%
[17:51:26][D][sensor:092]: 'mapa_puda Temperature': Sending state 3.40000 °C with 1 decimals of accuracy
[17:51:26][D][sensor:092]: 'mala_puda Humidity': Sending state 65.30000 % with 0 decimals of accuracy
[17:51:46][D][dht:048]: Got Temperature=2.7°C Humidity=67.1%
[17:51:46][D][sensor:092]: 'mapa_puda Temperature': Sending state 2.70000 °C with 1 decimals of accuracy
[17:51:46][D][sensor:092]: 'mala_puda Humidity': Sending state 67.10000 % with 0 decimals of accuracy
[17:52:06][D][dht:048]: Got Temperature=2.2°C Humidity=69.1%
[17:52:06][D][sensor:092]: 'mapa_puda Temperature': Sending state 2.20000 °C with 1 decimals of accuracy
[17:52:06][D][sensor:092]: 'mala_puda Humidity': Sending state 69.10000 % with 0 decimals of accuracy
[17:52:26][D][dht:048]: Got Temperature=1.7°C Humidity=70.9%
[17:52:26][D][sensor:092]: 'mapa_puda Temperature': Sending state 1.70000 °C with 1 decimals of accuracy
[17:52:26][D][sensor:092]: 'mala_puda Humidity': Sending state 70.90000 % with 0 decimals of accuracy
[17:52:46][D][dht:048]: Got Temperature=1.3°C Humidity=72.5%
[17:52:46][D][sensor:092]: 'mapa_puda Temperature': Sending state 1.30000 °C with 1 decimals of accuracy
[17:52:46][D][sensor:092]: 'mala_puda Humidity': Sending state 72.50000 % with 0 decimals of accuracy
[17:53:06][D][dht:048]: Got Temperature=1.0°C Humidity=73.9%
[17:53:06][D][sensor:092]: 'mapa_puda Temperature': Sending state 1.00000 °C with 1 decimals of accuracy
[17:53:06][D][sensor:092]: 'mala_puda Humidity': Sending state 73.90000 % with 0 decimals of accuracy
[17:53:26][D][dht:048]: Got Temperature=0.7°C Humidity=75.1%
[17:53:26][D][sensor:092]: 'mapa_puda Temperature': Sending state 0.70000 °C with 1 decimals of accuracy
[17:53:26][D][sensor:092]: 'mala_puda Humidity': Sending state 75.10000 % with 0 decimals of accuracy
[17:53:46][D][dht:048]: Got Temperature=0.4°C Humidity=76.0%
[17:53:46][D][sensor:092]: 'mapa_puda Temperature': Sending state 0.40000 °C with 1 decimals of accuracy
[17:53:46][D][sensor:092]: 'mala_puda Humidity': Sending state 76.00000 % with 0 decimals of accuracy
[17:54:06][D][dht:048]: Got Temperature=0.2°C Humidity=76.8%
[17:54:06][D][sensor:092]: 'mapa_puda Temperature': Sending state 0.20000 °C with 1 decimals of accuracy
[17:54:06][D][sensor:092]: 'mala_puda Humidity': Sending state 76.80000 % with 0 decimals of accuracy
[17:54:26][D][dht:048]: Got Temperature=0.0°C Humidity=77.7%
[17:54:26][D][sensor:092]: 'mapa_puda Temperature': Sending state 0.00000 °C with 1 decimals of accuracy
[17:54:26][D][sensor:092]: 'mala_puda Humidity': Sending state 77.70000 % with 0 decimals of accuracy
[17:54:46][D][dht:048]: Got Temperature=-3276.8°C Humidity=78.5%
[17:54:46][D][sensor:092]: 'mapa_puda Temperature': Sending state -3276.80005 °C with 1 decimals of accuracy
[17:54:46][D][sensor:092]: 'mala_puda Humidity': Sending state 78.50000 % with 0 decimals of accuracy
[17:55:06][D][dht:048]: Got Temperature=-3276.7°C Humidity=79.0%
[17:55:06][D][sensor:092]: 'mapa_puda Temperature': Sending state -3276.69995 °C with 1 decimals of accuracy
[17:55:06][D][sensor:092]: 'mala_puda Humidity': Sending state 79.00000 % with 0 decimals of accuracy
[17:55:26][D][dht:048]: Got Temperature=-3276.5°C Humidity=79.5%
[17:55:26][D][sensor:092]: 'mapa_puda Temperature': Sending state -3276.50000 °C with 1 decimals of accuracy
[17:55:26][D][sensor:092]: 'mala_puda Humidity': Sending state 79.50000 % with 0 decimals of accuracy
[17:55:46][D][dht:048]: Got Temperature=-3276.4°C Humidity=80.0%
[17:55:46][D][sensor:092]: 'mapa_puda Temperature': Sending state -3276.40015 °C with 1 decimals of accuracy
[17:55:46][D][sensor:092]: 'mala_puda Humidity': Sending state 80.00000 % with 0 decimals of accuracy
[17:56:06][D][dht:048]: Got Temperature=-3276.3°C Humidity=80.4%
[17:56:06][D][sensor:092]: 'mapa_puda Temperature': Sending state -3276.30005 °C with 1 decimals of accuracy
[17:56:06][D][sensor:092]: 'mala_puda Humidity': Sending state 80.40000 % with 0 decimals of accuracy
[17:56:26][D][dht:048]: Got Temperature=-3276.2°C Humidity=80.7%
[17:56:26][D][sensor:092]: 'mapa_puda Temperature': Sending state -3276.19995 °C with 1 decimals of accuracy
[17:56:26][D][sensor:092]: 'mala_puda Humidity': Sending state 80.70000 % with 0 decimals of accuracy
[17:56:46][D][dht:048]: Got Temperature=-3276.2°C Humidity=81.2%
[17:56:46][D][sensor:092]: 'mapa_puda Temperature': Sending state -3276.19995 °C with 1 decimals of accuracy
[17:56:46][D][sensor:092]: 'mala_puda Humidity': Sending state 81.20000 % with 0 decimals of accuracy
[17:57:06][D][dht:048]: Got Temperature=-3276.1°C Humidity=82.0%
[17:57:06][D][sensor:092]: 'mapa_puda Temperature': Sending state -3276.10010 °C with 1 decimals of accuracy
[17:57:06][D][sensor:092]: 'mala_puda Humidity': Sending state 82.00000 % with 0 decimals of accuracy
[17:57:26][D][dht:048]: Got Temperature=-3276.0°C Humidity=82.3%
[17:57:26][D][sensor:092]: 'mapa_puda Temperature': Sending state -3276.00000 °C with 1 decimals of accuracy
[17:57:26][D][sensor:092]: 'mala_puda Humidity': Sending state 82.30000 % with 0 decimals of accuracy

Additional information and things you've tried: I tried to change sensor type to all possible values: AUTO_DETECT, DHT11, DHT22, DHT22_TYPE2, AM2302, RHT03, SI7021

none of them solved the issue

nettle-m commented 3 years ago

as workaround i used this filter:

filters:
        - lambda: !lambda if ( x < -1000.0 ) { return (-1) * x - 3277.0; } else { return x; }

now temperatures in home-assistant show as they should

probot-esphome[bot] commented 3 years ago

dht source dht issues dht recent changes (message by IssueLinks)

nettle-m commented 3 years ago

just update: problem is still present in ESPHome version 1.16.0

SenexCrenshaw commented 3 years ago

Can you try the latest dev branch?

nettle-m commented 3 years ago

Hello, i dont want to mess with my current Home Assistant i will try to setup new instance with ESPHome dev version, but it will take some time

nettle-m commented 3 years ago

i was unsuccessful in dev esphome deployment. Could you guide me how can i upgrade ESPHome HO addon to dev version?

SenexCrenshaw commented 3 years ago

Have you tried adding a 4.7k pullup like in the docs? https://esphome.io/components/sensor/dht.html

nettle-m commented 3 years ago

yes, i have dht22 connected with pull up resistor from the start

agners commented 3 years ago

This very much matches what is described in this comment: https://github.com/esphome/esphome/pull/926#issuecomment-602110509

That got fixed with the DHT22 Type 2 introduction. Are you sure DHT22_TYPE2 doesn't fix your problem?

gb53smith commented 3 years ago

You may want try this custom sensor from my post. It uses a newer and perhaps better DHT library. Your issue appears to be the same as found in a much older DHT library. Esphome folks may have missed a bug fix and not copied it into their DHT component.

Looking at the dht.cpp I see:

 if (this->model_ != DHT_MODEL_DHT22_TYPE2 && (raw_temperature & 0x8000) != 0)
      raw_temperature = ~(raw_temperature & 0x7FFF);

That should convert the sign-magnitude binary value to 2's complement required to represent a negative temperature for the conversion to int16_t later in code. This will give you a large negative number if model = DHT22_TYPE2 is used.

gb53smith commented 3 years ago

@nettle-m You may want to test your DHT using the new external component integration

  - source: github://gb53smith/esphome@devsweep
    components: [dht]

It is based on github://esphome/esphome@dev with the added test of sweeping the start delay between 800 and 20000 us. (To prove a theory that the start delay was never the true cause bad readings.) The model: must be set to AM2302 for the test to be activated.

When github://esphome/esphome@dev get released use model: DHT22 Never use model: DHT22_TYPE2 unless your DHT datasheet indicates 2's complement representation of negative temperatures

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.