diff --git a/esphome/components/sht4x/sht4x.cpp b/esphome/components/sht4x/sht4x.cpp index d9d6a71630..43c2436a56 100644 --- a/esphome/components/sht4x/sht4x.cpp +++ b/esphome/components/sht4x/sht4x.cpp @@ -79,7 +79,9 @@ void SHT4XComponent::dump_config() { } void SHT4XComponent::update() { + // Send command if (!this->write_command(MEASURECOMMANDS[this->precision_])) { + // Warning will be printed only if warning status is not set yet this->status_set_warning(LOG_STR("Failed to send measurement command")); return; } @@ -87,7 +89,9 @@ void SHT4XComponent::update() { this->set_timeout(10, [this]() { uint16_t buffer[2]; + // Read measurement if (!this->read_data(buffer, 2)) { + // Using ESP_LOGW to force the warning to be printed ESP_LOGW(TAG, "Sensor read failed"); this->status_set_warning(); return; @@ -95,12 +99,15 @@ void SHT4XComponent::update() { this->status_clear_warning(); + // Evaluate and publish measurements if (this->temp_sensor_ != nullptr) { + // Temp is contained in the first result word float temp = TEMPERATURE_OFFSET + TEMPERATURE_SPAN * static_cast(buffer[0]) / RAW_MAX; this->temp_sensor_->publish_state(temp); } if (this->humidity_sensor_ != nullptr) { + // Relative humidity is in the second result word float rh = HUMIDITY_OFFSET + HUMIDITY_SPAN * static_cast(buffer[1]) / RAW_MAX; this->humidity_sensor_->publish_state(rh); }