ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
exponential_moving_average_sensor.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <cstdint>
5
7#include "esphome/core/log.h"
10
12
19
20const LogString *time_weighting_to_string(TimeWeighting weighting);
21
24 float value;
25 const LogString *unit;
26 uint8_t decimals;
27};
28
30
32 public:
34
35 void setup() override;
36 void dump_config() override;
37
38 void set_alpha(float alpha) { this->alpha_ = alpha; }
40 void set_time_constant(uint32_t time_constant_ms) { this->time_constant_ms_ = time_constant_ms; }
41 void set_time_weighting(TimeWeighting weighting) { this->time_weighting_ = weighting; }
42 void set_restore(bool restore) { this->restore_ = restore; }
44 void reset();
45
46 protected:
47 void process_(float value, uint32_t now);
48 void publish_and_save_(float value);
49
52 float alpha_{0.1f};
53 float accumulator_{NAN};
54 float previous_value_{NAN};
58 bool restore_{true};
59};
60
61} // namespace esphome::exponential_moving_average
void set_time_constant(uint32_t time_constant_ms)
When non-zero, each sample is weighted by the time since the previous one instead of by a fixed alpha...
Base-class for all sensors.
Definition sensor.h:47
const LogString * time_weighting_to_string(TimeWeighting weighting)
TimeWeighting
Which value is assumed to apply during the time between two readings, when a time constant is used.
static void uint32_t
A duration in the largest of ms, s, min or h that keeps the value at 1 or more.