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;
18static constexpr uint32_t PRE_MIDNIGHT_SECONDS = 90 * SECONDS_PER_MINUTE;
21 float initial_value = 0;
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);
69 if (seconds_until_midnight > PRE_MIDNIGHT_SECONDS) {
70 timeout_seconds = seconds_until_midnight - PRE_MIDNIGHT_SECONDS;
72 timeout_seconds = seconds_until_midnight + 1;
75 ESP_LOGD(TAG,
"Scheduling midnight check in %us", timeout_seconds);
76 this->
set_timeout(TIMEOUT_ID_MIDNIGHT, timeout_seconds * MILLIS_PER_SECOND,
89 if (std::isnan(
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;
98 delta_energy = delta_hours * (old_state + new_state) / 2.0f;
101 delta_energy = delta_hours * old_state;
104 delta_energy = delta_hours * new_state;
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.
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.
float state
This member variable stores the last state that has passed through all filters.
ESPTime now()
Get the time in the currently defined timezone.
void add_on_time_sync_callback(F &&callback)
time::RealTimeClock * time_
uint16_t last_day_of_year_
void publish_state_and_save(float state)
void process_new_state_(float state)
void dump_config() override
TotalDailyEnergyMethod method_
ESPPreferenceObject pref_
void schedule_midnight_reset_()
@ TOTAL_DAILY_ENERGY_METHOD_TRAPEZOID
@ TOTAL_DAILY_ENERGY_METHOD_LEFT
@ TOTAL_DAILY_ENERGY_METHOD_RIGHT
Application App
Global storage of Application pointer - only one Application can exist.