diff --git a/esphome/components/sensor/filter.cpp b/esphome/components/sensor/filter.cpp index b40f5f6100..9a4275d57a 100644 --- a/esphome/components/sensor/filter.cpp +++ b/esphome/components/sensor/filter.cpp @@ -255,6 +255,8 @@ bool throttle_check_and_update(uint32_t &last_input, uint32_t min_time_between_i return false; } +void throttle_update_timestamp(uint32_t &last_input) { last_input = App.get_loop_component_start_time(); } + // ThrottleFilter ThrottleFilter::ThrottleFilter(uint32_t min_time_between_inputs) : min_time_between_inputs_(min_time_between_inputs) {} optional ThrottleFilter::new_value(float value) { diff --git a/esphome/components/sensor/filter.h b/esphome/components/sensor/filter.h index 24783af7da..50c3e2cf71 100644 --- a/esphome/components/sensor/filter.h +++ b/esphome/components/sensor/filter.h @@ -378,10 +378,13 @@ class ThrottleFilter : public Filter { uint32_t min_time_between_inputs_; }; -/// Returns true if throttle should allow the value through (time expired or first input). -/// Updates last_input in-place. Implementation in filter.cpp (accesses App without circular include). +/// Returns true if throttle time has expired or this is the first input. +/// Implementation in filter.cpp (accesses App without circular include). bool throttle_check_and_update(uint32_t &last_input, uint32_t min_time_between_inputs); +/// Update last_input timestamp to current loop time. Implementation in filter.cpp. +void throttle_update_timestamp(uint32_t &last_input); + /// Same as 'throttle' but will immediately publish values contained in `value_to_prioritize`. template class ThrottleWithPriorityFilter : public ValueListFilter { public: @@ -392,6 +395,7 @@ template class ThrottleWithPriorityFilter : public ValueListFilter optional new_value(float value) override { if (throttle_check_and_update(this->last_input_, this->min_time_between_inputs_) || this->value_matches_any_(value)) { + throttle_update_timestamp(this->last_input_); return value; } return {};