diff --git a/esphome/core/application.h b/esphome/core/application.h index 185ee4163b1..86aa461fbb6 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -103,6 +103,12 @@ class Application { void set_current_component(Component *component) { this->current_component_ = component; } Component *get_current_component() { return this->current_component_; } + /// Update the cached loop component start time. Used by the scheduler before + /// dispatching a queued callback so callers reading + /// get_loop_component_start_time() inside the callback observe a fresh value + /// instead of one inherited from the prior loop iteration's last component. + void set_loop_component_start_time(uint32_t now) { this->loop_component_start_time_ = now; } + // Entity register methods (generated from entity_types.h). // Each entity type gets two overloads: // - register_(obj) — bare push_back diff --git a/esphome/core/scheduler.cpp b/esphome/core/scheduler.cpp index d83d67d6e42..fc02ba61f8d 100644 --- a/esphome/core/scheduler.cpp +++ b/esphome/core/scheduler.cpp @@ -772,6 +772,11 @@ Scheduler::SchedulerItem *HOT Scheduler::pop_raw_locked_() { // Helper to execute a scheduler item uint32_t HOT Scheduler::execute_item_(SchedulerItem *item, uint32_t now) { App.set_current_component(item->component); + // Freshen the cached loop component start time so callbacks reading + // App.get_loop_component_start_time() observe the dispatch time of this + // item rather than a stale value from the prior loop iteration's last + // component phase. + App.set_loop_component_start_time(now); WarnIfComponentBlockingGuard guard{item->component, now}; item->callback(); uint32_t end = guard.finish();