diff --git a/esphome/core/automation.h b/esphome/core/automation.h index fc2cad99be..3cb8caa026 100644 --- a/esphome/core/automation.h +++ b/esphome/core/automation.h @@ -419,12 +419,15 @@ template class Action { template class ActionList { public: void add_action(Action *action) { - if (this->actions_end_ == nullptr) { + if (this->actions_begin_ == nullptr) { this->actions_begin_ = action; } else { - this->actions_end_->next_ = action; + // 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; } - this->actions_end_ = action; } void add_actions(const std::initializer_list *> &actions) { for (auto *action : actions) { @@ -465,7 +468,6 @@ template class ActionList { } Action *actions_begin_{nullptr}; - Action *actions_end_{nullptr}; }; template class Automation {