From 7c0d777173fb55b435cb2fc8588392b5dd17dcdc Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Tue, 12 Nov 2019 19:04:11 +0100 Subject: [PATCH] Check DHT sensor exists before publishing (#850) Fixes https://github.com/esphome/issues/issues/841 --- esphome/components/dht/dht.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/esphome/components/dht/dht.cpp b/esphome/components/dht/dht.cpp index 1e28246bee..23d8c1d3e2 100644 --- a/esphome/components/dht/dht.cpp +++ b/esphome/components/dht/dht.cpp @@ -47,8 +47,10 @@ void DHT::update() { if (error) { ESP_LOGD(TAG, "Got Temperature=%.1f°C Humidity=%.1f%%", temperature, humidity); - this->temperature_sensor_->publish_state(temperature); - this->humidity_sensor_->publish_state(humidity); + if (this->temperature_sensor_ != nullptr) + this->temperature_sensor_->publish_state(temperature); + if (this->humidity_sensor_ != nullptr) + this->humidity_sensor_->publish_state(humidity); this->status_clear_warning(); } else { const char *str = ""; @@ -56,8 +58,10 @@ void DHT::update() { str = " and consider manually specifying the DHT model using the model option"; } ESP_LOGW(TAG, "Invalid readings! Please check your wiring (pull-up resistor, pin number)%s.", str); - this->temperature_sensor_->publish_state(NAN); - this->humidity_sensor_->publish_state(NAN); + if (this->temperature_sensor_ != nullptr) + this->temperature_sensor_->publish_state(NAN); + if (this->humidity_sensor_ != nullptr) + this->humidity_sensor_->publish_state(NAN); this->status_set_warning(); } }