diff --git a/esphome/core/automation.h b/esphome/core/automation.h index 90711cfaee7..b41a3555e44 100644 --- a/esphome/core/automation.h +++ b/esphome/core/automation.h @@ -419,11 +419,16 @@ template class Action { template class ActionList { public: void add_action(Action *action) { - // Walk to end of chain - action lists are short and only built during setup() - Action **tail = &this->actions_begin_; - while (*tail != nullptr) - tail = &(*tail)->next_; - *tail = action; + if (this->actions_begin_ == nullptr) { + this->actions_begin_ = action; + } else { + // Walk to end of chain - action lists are short and only built during setup(). + // Note: intentionally not using pointer-to-pointer idiom here as it generates larger code. + auto *it = this->actions_begin_; + while (it->next_ != nullptr) + it = it->next_; + it->next_ = action; + } } void add_actions(const std::initializer_list *> &actions) { // Find tail once, then append all actions in a single pass