Implement send_first_at for exponential_moving_average (#3240)

This commit is contained in:
EdJoPaTo 2022-02-21 01:13:06 +01:00 committed by GitHub
parent 771162bfb1
commit 69633826bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 7 deletions

View File

@ -408,18 +408,30 @@ async def sliding_window_moving_average_filter_to_code(config, filter_id):
) )
@FILTER_REGISTRY.register( EXPONENTIAL_AVERAGE_SCHEMA = cv.All(
"exponential_moving_average",
ExponentialMovingAverageFilter,
cv.Schema( cv.Schema(
{ {
cv.Optional(CONF_ALPHA, default=0.1): cv.positive_float, 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_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): 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( @FILTER_REGISTRY.register(

View File

@ -200,8 +200,8 @@ optional<float> SlidingWindowMovingAverageFilter::new_value(float value) {
} }
// ExponentialMovingAverageFilter // ExponentialMovingAverageFilter
ExponentialMovingAverageFilter::ExponentialMovingAverageFilter(float alpha, size_t send_every) ExponentialMovingAverageFilter::ExponentialMovingAverageFilter(float alpha, size_t send_every, size_t send_first_at)
: send_every_(send_every), send_at_(send_every - 1), alpha_(alpha) {} : send_every_(send_every), send_at_(send_every - send_first_at), alpha_(alpha) {}
optional<float> ExponentialMovingAverageFilter::new_value(float value) { optional<float> ExponentialMovingAverageFilter::new_value(float value) {
if (!std::isnan(value)) { if (!std::isnan(value)) {
if (this->first_value_) { if (this->first_value_) {

View File

@ -194,7 +194,7 @@ class SlidingWindowMovingAverageFilter : public Filter {
*/ */
class ExponentialMovingAverageFilter : public Filter { class ExponentialMovingAverageFilter : public Filter {
public: public:
ExponentialMovingAverageFilter(float alpha, size_t send_every); ExponentialMovingAverageFilter(float alpha, size_t send_every, size_t send_first_at);
optional<float> new_value(float value) override; optional<float> new_value(float value) override;

View File

@ -358,6 +358,7 @@ sensor:
- exponential_moving_average: - exponential_moving_average:
alpha: 0.1 alpha: 0.1
send_every: 15 send_every: 15
send_first_at: 15
- throttle_average: 60s - throttle_average: 60s
- throttle: 1s - throttle: 1s
- heartbeat: 5s - heartbeat: 5s