diff --git a/esphome/automation.py b/esphome/automation.py index 4aede00c5..0c4bda09d 100644 --- a/esphome/automation.py +++ b/esphome/automation.py @@ -254,7 +254,11 @@ async def repeat_action_to_code(config, action_id, template_arg, args): var = cg.new_Pvariable(action_id, template_arg) count_template = await cg.templatable(config[CONF_COUNT], args, cg.uint32) cg.add(var.set_count(count_template)) - actions = await build_action_list(config[CONF_THEN], template_arg, args) + actions = await build_action_list( + config[CONF_THEN], + cg.TemplateArguments(cg.uint32, *template_arg.args), + [(cg.uint32, "iteration"), *args], + ) cg.add(var.add_then(actions)) return var diff --git a/esphome/core/base_automation.h b/esphome/core/base_automation.h index b36a64b82..5a4fb6519 100644 --- a/esphome/core/base_automation.h +++ b/esphome/core/base_automation.h @@ -235,22 +235,21 @@ template class RepeatAction : public Action { public: TEMPLATABLE_VALUE(uint32_t, count) - void add_then(const std::vector *> &actions) { + void add_then(const std::vector *> &actions) { this->then_.add_actions(actions); - this->then_.add_action(new LambdaAction([this](Ts... x) { - this->iteration_++; - if (this->iteration_ == this->count_.value(x...)) + this->then_.add_action(new LambdaAction([this](uint32_t iteration, Ts... x) { + iteration++; + if (iteration >= this->count_.value(x...)) this->play_next_tuple_(this->var_); else - this->then_.play_tuple(this->var_); + this->then_.play(iteration, x...); })); } void play_complex(Ts... x) override { this->num_running_++; this->var_ = std::make_tuple(x...); - this->iteration_ = 0; - this->then_.play_tuple(this->var_); + this->then_.play(0, x...); } void play(Ts... x) override { /* ignore - see play_complex */ @@ -259,8 +258,7 @@ template class RepeatAction : public Action { void stop() override { this->then_.stop(); } protected: - uint32_t iteration_; - ActionList then_; + ActionList then_; std::tuple var_; };