ESPHome 2026.11.0b6
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
23void FilterLifetime::update() {
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 PUBLISH_STATE(this->runtime_hours_sensor_, runtime_minutes / 60.0f);
51 PUBLISH_STATE(this->remaining_days_sensor_, remaining_minutes / MINUTES_PER_DAY);
52}
53
54void FilterLifetime::on_shutdown() { this->pref_.save(&this->runtime_seconds_); }
55
56void FilterLifetime::reset_filter() {
57 this->runtime_seconds_ = 0;
58 this->carry_ms_ = 0;
59 this->pref_.save(&this->runtime_seconds_);
60 this->publish_values_();
61 ESP_LOGI(TAG, "Filter lifetime reset");
62}
63
65#ifdef USE_BINARY_SENSOR
66 // Reads false until the sensor first publishes, so nothing accumulates before then.
67 if (this->is_on_binary_sensor_ != nullptr)
68 return this->is_on_binary_sensor_->state;
69#endif
70 if (this->is_on_lambda_ != nullptr)
71 return this->is_on_lambda_();
72 return true;
73}
74
76 if (this->current_speed_sensor_ != nullptr)
77 return this->current_speed_sensor_->state; // NaN before the first publish, treated as 0
78 if (this->current_speed_lambda_ != nullptr)
79 return this->current_speed_lambda_();
80 return 100.0f;
81}
82
83void FilterLifetime::dump_config() {
84 LOG_SENSOR("", "Filter Lifetime", this);
85 ESP_LOGCONFIG(TAG,
86 " Max lifetime: %" PRIu32 " min (%.1f days)\n"
87 " Runtime: %.1f h",
88 this->max_lifetime_minutes_, this->max_lifetime_minutes_ / MINUTES_PER_DAY,
89 this->runtime_seconds_ / 3600.0f);
90#ifdef USE_BINARY_SENSOR
91 if (this->is_on_binary_sensor_ != nullptr) {
92 LOG_BINARY_SENSOR(" ", "Is-on sensor", this->is_on_binary_sensor_);
93 } else
94#endif
95 {
96 ESP_LOGCONFIG(TAG, " Is-on: %s",
97 this->is_on_lambda_ != nullptr ? LOG_STR_LITERAL("lambda") : LOG_STR_LITERAL("always"));
98 }
99 if (this->current_speed_sensor_ != nullptr) {
100 LOG_SENSOR(" ", "Speed sensor", this->current_speed_sensor_);
101 } else {
102 ESP_LOGCONFIG(TAG, " Speed: %s",
103 this->current_speed_lambda_ != nullptr ? LOG_STR_LITERAL("lambda") : LOG_STR_LITERAL("100%"));
104 }
105 LOG_SENSOR(" ", "Runtime hours", this->runtime_hours_sensor_);
106 LOG_SENSOR(" ", "Remaining days", this->remaining_days_sensor_);
107 LOG_UPDATE_INTERVAL(this);
108}
109
110} // 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.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
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