diff --git a/esphome/components/scd4x/scd4x.cpp b/esphome/components/scd4x/scd4x.cpp index 681324fa18..117b92b901 100644 --- a/esphome/components/scd4x/scd4x.cpp +++ b/esphome/components/scd4x/scd4x.cpp @@ -149,9 +149,9 @@ void SCD4XComponent::update() { } if (this->ambient_pressure_source_ != nullptr) { - float pressure = this->ambient_pressure_source_->state / 1000.0f; + float pressure = this->ambient_pressure_source_->state; if (!std::isnan(pressure)) { - set_ambient_pressure_compensation(this->ambient_pressure_source_->state / 1000.0f); + set_ambient_pressure_compensation(pressure); } } @@ -254,12 +254,15 @@ bool SCD4XComponent::factory_reset() { return true; } -// Note pressure in bar here. Convert to hPa -void SCD4XComponent::set_ambient_pressure_compensation(float pressure_in_bar) { +void SCD4XComponent::set_ambient_pressure_compensation(float pressure_in_hpa) { ambient_pressure_compensation_ = true; - uint16_t new_ambient_pressure = (uint16_t)(pressure_in_bar * 1000); - // remove millibar from comparison to avoid frequent updates +/- 10 millibar doesn't matter - if (initialized_ && (new_ambient_pressure / 10 != ambient_pressure_ / 10)) { + uint16_t new_ambient_pressure = (uint16_t) pressure_in_hpa; + if (!initialized_) { + ambient_pressure_ = new_ambient_pressure; + return; + } + // Only send pressure value if it has changed since last update + if (new_ambient_pressure != ambient_pressure_) { update_ambient_pressure_compensation_(new_ambient_pressure); ambient_pressure_ = new_ambient_pressure; } else { diff --git a/esphome/components/scd4x/scd4x.h b/esphome/components/scd4x/scd4x.h index 23c3766e60..22055e78d0 100644 --- a/esphome/components/scd4x/scd4x.h +++ b/esphome/components/scd4x/scd4x.h @@ -26,7 +26,7 @@ class SCD4XComponent : public PollingComponent, public sensirion_common::Sensiri void set_automatic_self_calibration(bool asc) { enable_asc_ = asc; } void set_altitude_compensation(uint16_t altitude) { altitude_compensation_ = altitude; } - void set_ambient_pressure_compensation(float pressure_in_bar); + void set_ambient_pressure_compensation(float pressure_in_hpa); void set_ambient_pressure_source(sensor::Sensor *pressure) { ambient_pressure_source_ = pressure; } void set_temperature_offset(float offset) { temperature_offset_ = offset; };