From 69633826bb54355680d24f47b03ed4d985c4ab2f Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Mon, 21 Feb 2022 01:13:06 +0100 Subject: [PATCH] Implement send_first_at for exponential_moving_average (#3240) --- esphome/components/sensor/__init__.py | 20 ++++++++++++++++---- esphome/components/sensor/filter.cpp | 4 ++-- esphome/components/sensor/filter.h | 2 +- tests/test1.yaml | 1 + 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/esphome/components/sensor/__init__.py b/esphome/components/sensor/__init__.py index 577596f6ce..65ae7b2168 100644 --- a/esphome/components/sensor/__init__.py +++ b/esphome/components/sensor/__init__.py @@ -408,18 +408,30 @@ async def sliding_window_moving_average_filter_to_code(config, filter_id): ) -@FILTER_REGISTRY.register( - "exponential_moving_average", - ExponentialMovingAverageFilter, +EXPONENTIAL_AVERAGE_SCHEMA = cv.All( cv.Schema( { cv.Optional(CONF_ALPHA, default=0.1): cv.positive_float, cv.Optional(CONF_SEND_EVERY, default=15): cv.positive_not_null_int, + cv.Optional(CONF_SEND_FIRST_AT, default=1): cv.positive_not_null_int, } ), + validate_send_first_at, +) + + +@FILTER_REGISTRY.register( + "exponential_moving_average", + ExponentialMovingAverageFilter, + EXPONENTIAL_AVERAGE_SCHEMA, ) async def exponential_moving_average_filter_to_code(config, filter_id): - return cg.new_Pvariable(filter_id, config[CONF_ALPHA], config[CONF_SEND_EVERY]) + return cg.new_Pvariable( + filter_id, + config[CONF_ALPHA], + config[CONF_SEND_EVERY], + config[CONF_SEND_FIRST_AT], + ) @FILTER_REGISTRY.register( diff --git a/esphome/components/sensor/filter.cpp b/esphome/components/sensor/filter.cpp index 49d2c648b0..d4a7a52fa1 100644 --- a/esphome/components/sensor/filter.cpp +++ b/esphome/components/sensor/filter.cpp @@ -200,8 +200,8 @@ optional SlidingWindowMovingAverageFilter::new_value(float value) { } // ExponentialMovingAverageFilter -ExponentialMovingAverageFilter::ExponentialMovingAverageFilter(float alpha, size_t send_every) - : send_every_(send_every), send_at_(send_every - 1), alpha_(alpha) {} +ExponentialMovingAverageFilter::ExponentialMovingAverageFilter(float alpha, size_t send_every, size_t send_first_at) + : send_every_(send_every), send_at_(send_every - send_first_at), alpha_(alpha) {} optional ExponentialMovingAverageFilter::new_value(float value) { if (!std::isnan(value)) { if (this->first_value_) { diff --git a/esphome/components/sensor/filter.h b/esphome/components/sensor/filter.h index 0ed7ce4801..a39c1ba25a 100644 --- a/esphome/components/sensor/filter.h +++ b/esphome/components/sensor/filter.h @@ -194,7 +194,7 @@ class SlidingWindowMovingAverageFilter : public Filter { */ class ExponentialMovingAverageFilter : public Filter { public: - ExponentialMovingAverageFilter(float alpha, size_t send_every); + ExponentialMovingAverageFilter(float alpha, size_t send_every, size_t send_first_at); optional new_value(float value) override; diff --git a/tests/test1.yaml b/tests/test1.yaml index 0d8ba9dfe8..496226565a 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -358,6 +358,7 @@ sensor: - exponential_moving_average: alpha: 0.1 send_every: 15 + send_first_at: 15 - throttle_average: 60s - throttle: 1s - heartbeat: 5s