diff --git a/esphome/core/automation.h b/esphome/core/automation.h index 90711cfaee7..72bcc3d7079 100644 --- a/esphome/core/automation.h +++ b/esphome/core/automation.h @@ -419,11 +419,15 @@ 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() + 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