mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 05:24:53 +01:00
Add throttle_average sensor filter (#2485)
This commit is contained in:
parent
4406a08fa7
commit
3dee057826
4 changed files with 56 additions and 0 deletions
|
@ -160,6 +160,7 @@ SlidingWindowMovingAverageFilter = sensor_ns.class_(
|
|||
ExponentialMovingAverageFilter = sensor_ns.class_(
|
||||
"ExponentialMovingAverageFilter", Filter
|
||||
)
|
||||
ThrottleAverageFilter = sensor_ns.class_("ThrottleAverageFilter", Filter, cg.Component)
|
||||
LambdaFilter = sensor_ns.class_("LambdaFilter", Filter)
|
||||
OffsetFilter = sensor_ns.class_("OffsetFilter", Filter)
|
||||
MultiplyFilter = sensor_ns.class_("MultiplyFilter", Filter)
|
||||
|
@ -381,6 +382,15 @@ async def exponential_moving_average_filter_to_code(config, filter_id):
|
|||
return cg.new_Pvariable(filter_id, config[CONF_ALPHA], config[CONF_SEND_EVERY])
|
||||
|
||||
|
||||
@FILTER_REGISTRY.register(
|
||||
"throttle_average", ThrottleAverageFilter, cv.positive_time_period_milliseconds
|
||||
)
|
||||
async def throttle_average_filter_to_code(config, filter_id):
|
||||
var = cg.new_Pvariable(filter_id, config)
|
||||
await cg.register_component(var, {})
|
||||
return var
|
||||
|
||||
|
||||
@FILTER_REGISTRY.register("lambda", LambdaFilter, cv.returning_lambda)
|
||||
async def lambda_filter_to_code(config, filter_id):
|
||||
lambda_ = await cg.process_lambda(
|
||||
|
|
|
@ -187,6 +187,31 @@ optional<float> ExponentialMovingAverageFilter::new_value(float value) {
|
|||
void ExponentialMovingAverageFilter::set_send_every(size_t send_every) { this->send_every_ = send_every; }
|
||||
void ExponentialMovingAverageFilter::set_alpha(float alpha) { this->alpha_ = alpha; }
|
||||
|
||||
// ThrottleAverageFilter
|
||||
ThrottleAverageFilter::ThrottleAverageFilter(uint32_t time_period) : time_period_(time_period) {}
|
||||
|
||||
optional<float> ThrottleAverageFilter::new_value(float value) {
|
||||
ESP_LOGVV(TAG, "ThrottleAverageFilter(%p)::new_value(value=%f)", this, value);
|
||||
if (!std::isnan(value)) {
|
||||
this->sum_ += value;
|
||||
this->n_++;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
void ThrottleAverageFilter::setup() {
|
||||
this->set_interval("throttle_average", this->time_period_, [this]() {
|
||||
ESP_LOGVV(TAG, "ThrottleAverageFilter(%p)::interval(sum=%f, n=%i)", this, this->sum_, this->n_);
|
||||
if (this->n_ == 0) {
|
||||
this->output(NAN);
|
||||
} else {
|
||||
this->output(this->sum_ / this->n_);
|
||||
this->sum_ = 0.0f;
|
||||
this->n_ = 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
float ThrottleAverageFilter::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
|
||||
// LambdaFilter
|
||||
LambdaFilter::LambdaFilter(lambda_filter_t lambda_filter) : lambda_filter_(std::move(lambda_filter)) {}
|
||||
const lambda_filter_t &LambdaFilter::get_lambda_filter() const { return this->lambda_filter_; }
|
||||
|
|
|
@ -178,6 +178,26 @@ class ExponentialMovingAverageFilter : public Filter {
|
|||
float alpha_;
|
||||
};
|
||||
|
||||
/** Simple throttle average filter.
|
||||
*
|
||||
* It takes the average of all the values received in a period of time.
|
||||
*/
|
||||
class ThrottleAverageFilter : public Filter, public Component {
|
||||
public:
|
||||
explicit ThrottleAverageFilter(uint32_t time_period);
|
||||
|
||||
void setup() override;
|
||||
|
||||
optional<float> new_value(float value) override;
|
||||
|
||||
float get_setup_priority() const override;
|
||||
|
||||
protected:
|
||||
uint32_t time_period_;
|
||||
float sum_{0.0f};
|
||||
unsigned int n_{0};
|
||||
};
|
||||
|
||||
using lambda_filter_t = std::function<optional<float>(float)>;
|
||||
|
||||
/** This class allows for creation of simple template filters.
|
||||
|
|
|
@ -343,6 +343,7 @@ sensor:
|
|||
- exponential_moving_average:
|
||||
alpha: 0.1
|
||||
send_every: 15
|
||||
- throttle_average: 60s
|
||||
- throttle: 1s
|
||||
- heartbeat: 5s
|
||||
- debounce: 0.1s
|
||||
|
|
Loading…
Reference in a new issue