ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
total_daily_energy.cpp
Go to the documentation of this file.
3#include "esphome/core/log.h"
4
6
7static const char *const TAG = "total_daily_energy";
8static constexpr uint32_t TIMEOUT_ID_MIDNIGHT = 1;
9static constexpr uint8_t SECONDS_PER_MINUTE = 60;
10static constexpr uint8_t MINUTES_PER_HOUR = 60;
11static constexpr uint8_t HOURS_PER_DAY = 24;
12static constexpr uint32_t SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR;
13static constexpr uint16_t MILLIS_PER_SECOND = 1000;
14// Wake up 90 minutes before midnight to recalculate, ensuring DST transitions
15// (which shift wall clock by 1 hour but don't change millis()) don't cause
16// the midnight reset to fire late. DST transitions don't trigger the time sync
17// callback since they change local time interpretation, not the epoch.
18static constexpr uint32_t PRE_MIDNIGHT_SECONDS = 90 * SECONDS_PER_MINUTE;
19
21 float initial_value = 0;
22
23 if (this->restore_) {
25 this->pref_.load(&initial_value);
26 }
27 this->publish_state_and_save(initial_value);
28
30
31 this->parent_->add_on_state_callback([this](float state) { this->process_new_state_(state); });
32
33 // Schedule initial midnight reset if time is already valid, otherwise
34 // the time sync callback will handle it once time becomes available.
36 // Re-schedule on every NTP sync in case the clock jumped across midnight.
37 this->time_->add_on_time_sync_callback([this]() { this->schedule_midnight_reset_(); });
38}
39
40void TotalDailyEnergy::dump_config() { LOG_SENSOR("", "Total Daily Energy", this); }
41
43 auto t = this->time_->now();
44 if (!t.is_valid())
45 return;
46
47 // Check if the day changed (time sync moved us past midnight, or first call)
48 if (this->last_day_of_year_ != t.day_of_year) {
49 if (this->last_day_of_year_ != 0) {
50 // Day actually changed — reset energy
51 this->total_energy_ = 0;
53 }
54 this->last_day_of_year_ = t.day_of_year;
55 }
56
57 // Calculate seconds until next midnight.
58 // Uses the same TIMEOUT_ID_MIDNIGHT ID so re-scheduling (e.g. from time sync) cancels
59 // any previously pending timeout.
60 uint32_t seconds_until_midnight =
61 ((HOURS_PER_DAY - 1 - t.hour) * MINUTES_PER_HOUR + (MINUTES_PER_HOUR - 1 - t.minute)) * SECONDS_PER_MINUTE +
62 (SECONDS_PER_MINUTE - t.second);
63
64 // set_timeout counts real elapsed millis, but DST shifts wall clock by up to 1 hour
65 // without changing millis. To avoid firing up to 1 hour late/early, we use two stages:
66 // 1) Wake up 90 minutes before midnight to recalculate with current wall clock
67 // 2) From there, schedule the precise midnight reset
68 uint32_t timeout_seconds;
69 if (seconds_until_midnight > PRE_MIDNIGHT_SECONDS) {
70 timeout_seconds = seconds_until_midnight - PRE_MIDNIGHT_SECONDS;
71 } else {
72 timeout_seconds = seconds_until_midnight + 1;
73 }
74
75 ESP_LOGD(TAG, "Scheduling midnight check in %us", timeout_seconds);
76 this->set_timeout(TIMEOUT_ID_MIDNIGHT, timeout_seconds * MILLIS_PER_SECOND,
77 [this]() { this->schedule_midnight_reset_(); });
78}
79
81 this->total_energy_ = state;
82 this->publish_state(state);
83 if (this->restore_) {
84 this->pref_.save(&state);
85 }
86}
87
89 if (std::isnan(state))
90 return;
92 const float old_state = this->last_power_state_;
93 const float new_state = state;
94 float delta_hours = (now - this->last_update_) / static_cast<float>(MILLIS_PER_SECOND) / SECONDS_PER_HOUR;
95 float delta_energy = 0.0f;
96 switch (this->method_) {
98 delta_energy = delta_hours * (old_state + new_state) / 2.0f;
99 break;
101 delta_energy = delta_hours * old_state;
102 break;
104 delta_energy = delta_hours * new_state;
105 break;
106 }
107 this->last_power_state_ = new_state;
108 this->last_update_ = now;
109 this->publish_state_and_save(this->total_energy_ + delta_energy);
110}
111
112} // namespace esphome::total_daily_energy
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.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:510
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:68
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:138
ESPTime now()
Get the time in the currently defined timezone.
void add_on_time_sync_callback(F &&callback)
bool state
Definition fan.h:2
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t