From c747d7d45d15469c111635045e2c95437bedccc1 Mon Sep 17 00:00:00 2001 From: Samuel Sieb Date: Wed, 26 Jun 2024 00:27:07 -0700 Subject: [PATCH] [dallas_temp] fix ds18s20 temp calc (#6988) --- .../components/dallas_temp/dallas_temp.cpp | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/esphome/components/dallas_temp/dallas_temp.cpp b/esphome/components/dallas_temp/dallas_temp.cpp index fe7c9a95ea..ae567d6a76 100644 --- a/esphome/components/dallas_temp/dallas_temp.cpp +++ b/esphome/components/dallas_temp/dallas_temp.cpp @@ -145,24 +145,21 @@ bool DallasTemperatureSensor::check_scratch_pad_() { float DallasTemperatureSensor::get_temp_c_() { int16_t temp = (this->scratch_pad_[1] << 8) | this->scratch_pad_[0]; if ((this->address_ & 0xff) == DALLAS_MODEL_DS18S20) { - if (this->scratch_pad_[7] != 0x10) - ESP_LOGE(TAG, "unexpected COUNT_PER_C value: %u", this->scratch_pad_[7]); - temp = ((temp & 0xfff7) << 3) + (0x10 - this->scratch_pad_[6]) - 4; - } else { - switch (this->resolution_) { - case 9: - temp &= 0xfff8; - break; - case 10: - temp &= 0xfffc; - break; - case 11: - temp &= 0xfffe; - break; - case 12: - default: - break; - } + return (temp >> 1) + (this->scratch_pad_[7] - this->scratch_pad_[6]) / float(this->scratch_pad_[7]) - 0.25; + } + switch (this->resolution_) { + case 9: + temp &= 0xfff8; + break; + case 10: + temp &= 0xfffc; + break; + case 11: + temp &= 0xfffe; + break; + case 12: + default: + break; } return temp / 16.0f;