From a952e7236b13f83c3946a75d94597675d22ce893 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 10 Sep 2026 16:04:57 -0500 Subject: [PATCH] [sensor] Only store raw state when filters are compiled in --- esphome/components/sensor/sensor.cpp | 7 ++----- esphome/components/sensor/sensor.h | 11 ++++++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/esphome/components/sensor/sensor.cpp b/esphome/components/sensor/sensor.cpp index c83cb483df..bee5d7c6d3 100644 --- a/esphome/components/sensor/sensor.cpp +++ b/esphome/components/sensor/sensor.cpp @@ -40,10 +40,7 @@ const LogString *state_class_to_string(StateClass state_class) { return StateClassStrings::get_log_str(static_cast(state_class), 0); } -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" -Sensor::Sensor() : state(NAN), raw_state_(NAN) {} -#pragma GCC diagnostic pop +Sensor::Sensor() : state(NAN) {} int8_t Sensor::get_accuracy_decimals() { if (this->sensor_flags_.has_accuracy_override) @@ -66,8 +63,8 @@ StateClass Sensor::get_state_class() { } void Sensor::publish_state(float state) { - this->raw_state_ = state; #ifdef USE_SENSOR_FILTER + this->raw_state_ = state; this->raw_callback_.call(state); #endif diff --git a/esphome/components/sensor/sensor.h b/esphome/components/sensor/sensor.h index 8c0c479463..594cd22e55 100644 --- a/esphome/components/sensor/sensor.h +++ b/esphome/components/sensor/sensor.h @@ -97,7 +97,13 @@ class Sensor : public EntityBase { /// Getter-syntax for .state. float get_state() const { return this->state; } /// Get the last state received by publish_state(), before any filters were applied. - float get_raw_state() const { return this->raw_state_; } + float get_raw_state() const { +#ifdef USE_SENSOR_FILTER + return this->raw_state_; +#else + return this->state; // No filters compiled in, raw == filtered +#endif + } /** Publish a new state to the front-end. * @@ -135,9 +141,8 @@ class Sensor : public EntityBase { void internal_send_state_to_frontend(float state); protected: - float raw_state_; ///< The last state passed to publish_state(), before filters. - #ifdef USE_SENSOR_FILTER + float raw_state_{NAN}; ///< The last state passed to publish_state(), before filters. LazyCallbackManager raw_callback_; ///< Storage for raw state callbacks. #endif LazyCallbackManager callback_; ///< Storage for filtered state callbacks.