ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
filter_lifetime.cpp
Go to the documentation of this file.
1#include "filter_lifetime.h"
3#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6#include <algorithm>
7#include <cmath>
8
10
11ESPHOME_LOG_TAG(TAG, "filter_lifetime");
12
13static constexpr uint32_t SECONDS_PER_HOUR = 3600;
14static constexpr float MINUTES_PER_DAY = 24.0f * 60.0f;
15
18 this->pref_.load(&this->runtime_seconds_);
19 this->last_update_ = millis();
20 this->publish_values_();
21}
22
25 const uint32_t elapsed_ms = now - this->last_update_;
26 this->last_update_ = now;
27
28 if (this->get_is_on_()) {
29 float speed = this->get_current_speed_();
30 if (std::isnan(speed))
31 speed = 0.0f;
32 speed = std::clamp(speed, 0.0f, 100.0f);
33 const uint32_t prev_hours = this->runtime_seconds_ / SECONDS_PER_HOUR;
34 this->carry_ms_ += static_cast<uint32_t>(elapsed_ms * (speed / 100.0f));
35 this->runtime_seconds_ += this->carry_ms_ / 1000;
36 this->carry_ms_ %= 1000;
37 // Save once per hour of runtime (and in on_shutdown): every save erases a flash sector on ESP8266.
38 if (this->runtime_seconds_ / SECONDS_PER_HOUR != prev_hours)
39 this->pref_.save(&this->runtime_seconds_);
40 ESP_LOGV(TAG, "Elapsed %" PRIu32 " ms at %.0f%%, runtime %" PRIu32 " s", elapsed_ms, speed, this->runtime_seconds_);
41 }
42 this->publish_values_();
43}
44
46 const float runtime_minutes = this->runtime_seconds_ / 60.0f;
47 const float max_minutes = this->max_lifetime_minutes_;
48 const float remaining_minutes = std::max(0.0f, max_minutes - runtime_minutes);
49 this->publish_state(100.0f * remaining_minutes / max_minutes);
50 if (this->runtime_hours_sensor_ != nullptr)
51 this->runtime_hours_sensor_->publish_state(runtime_minutes / 60.0f);
52 if (this->remaining_days_sensor_ != nullptr)
53 this->remaining_days_sensor_->publish_state(remaining_minutes / MINUTES_PER_DAY);
54}
55
57
59 this->runtime_seconds_ = 0;
60 this->carry_ms_ = 0;
61 this->pref_.save(&this->runtime_seconds_);
62 this->publish_values_();
63 ESP_LOGI(TAG, "Filter lifetime reset");
64}
65
67#ifdef USE_BINARY_SENSOR
68 // Reads false until the sensor first publishes, so nothing accumulates before then.
69 if (this->is_on_sensor_ != nullptr)
70 return this->is_on_sensor_->state;
71#endif
72 if (this->is_on_lambda_ != nullptr)
73 return this->is_on_lambda_();
74 return true;
75}
76
78 if (this->current_speed_sensor_ != nullptr)
79 return this->current_speed_sensor_->state; // NaN before the first publish, treated as 0
80 if (this->current_speed_lambda_ != nullptr)
81 return this->current_speed_lambda_();
82 return 100.0f;
83}
84
86 LOG_SENSOR("", "Filter Lifetime", this);
87 ESP_LOGCONFIG(TAG,
88 " Max lifetime: %" PRIu32 " min (%.1f days)\n"
89 " Runtime: %.1f h",
90 this->max_lifetime_minutes_, this->max_lifetime_minutes_ / MINUTES_PER_DAY,
91 this->runtime_seconds_ / 3600.0f);
92#ifdef USE_BINARY_SENSOR
93 if (this->is_on_sensor_ != nullptr) {
94 LOG_BINARY_SENSOR(" ", "Is-on sensor", this->is_on_sensor_);
95 } else
96#endif
97 {
98 ESP_LOGCONFIG(TAG, " Is-on: %s",
99 this->is_on_lambda_ != nullptr ? LOG_STR_LITERAL("lambda") : LOG_STR_LITERAL("always"));
100 }
101 if (this->current_speed_sensor_ != nullptr) {
102 LOG_SENSOR(" ", "Speed sensor", this->current_speed_sensor_);
103 } else {
104 ESP_LOGCONFIG(TAG, " Speed: %s",
105 this->current_speed_lambda_ != nullptr ? LOG_STR_LITERAL("lambda") : LOG_STR_LITERAL("100%"));
106 }
107 LOG_SENSOR(" ", "Runtime hours", this->runtime_hours_sensor_);
108 LOG_SENSOR(" ", "Remaining days", this->remaining_days_sensor_);
109 LOG_UPDATE_INTERVAL(this);
110}
111
112} // namespace esphome::filter_lifetime
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.
bool state
The current state of this binary sensor. Also used as the backing storage for StatefulEntityBase.
binary_sensor::BinarySensor * is_on_sensor_
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:140
int speed
Definition fan.h:3
ESPHOME_LOG_TAG(TAG, "filter_lifetime")
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t