14 return LOG_STR(
"previous");
16 return LOG_STR(
"linear");
18 return LOG_STR(
"new");
24 return {
static_cast<float>(ms), LOG_STR(
"ms"), 0};
26 return {ms / 1000.0f, LOG_STR(
"s"), 1};
27 if (ms < 60 * 60 * 1000)
28 return {ms / (60 * 1000.0f), LOG_STR(
"min"), 1};
29 return {ms / (60 * 60 * 1000.0f), LOG_STR(
"h"), 1};
36 if (this->
pref_.
load(&restored) && std::isfinite(restored)) {
51 LOG_SENSOR(
"",
"Exponential Moving Average Sensor",
this);
55 " Time Constant: %.*f %s\n"
56 " Time Weighting: %s",
60 ESP_LOGCONFIG(TAG,
" Alpha: %.3f", this->
alpha_);
62 ESP_LOGCONFIG(TAG,
" Restore: %s", YESNO(this->
restore_));
68 if (std::isnan(value))
72 this->last_update_ = now;
87 const double gain = -std::expm1(-
x);
94 result = average +
gain * (previous - average);
98 const double weight_new =
x > 0.0 ? (
x + std::expm1(-
x)) /
x : 0.0;
99 result = average + (
gain - weight_new) * (previous - average) + weight_new * (value - average);
103 result = average +
gain * (value - average);
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
void dump_config() override
ESPPreferenceObject pref_
void process_(float value, uint32_t now)
uint32_t time_constant_ms_
void reset()
Clear the average; the next sample starts it again.
TimeWeighting time_weighting_
void publish_and_save_(float value)
void add_on_state_callback(F &&callback)
Add a callback that will be called every time a filtered value arrives.
void publish_state(float state)
Publish a new state to the front-end.
float state
This member variable stores the last state that has passed through all filters.
ScaledDuration scale_duration(uint32_t ms)
ESPHOME_LOG_TAG(TAG, "exponential_moving_average")
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.
@ TIME_WEIGHTING_PREVIOUS
Application App
Global storage of Application pointer - only one Application can exist.
A duration in the largest of ms, s, min or h that keeps the value at 1 or more.