diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 9aafe53925c..2e112706fba 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -2063,6 +2063,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment): self.template_cache: weakref.WeakValueDictionary[ str | jinja2.nodes.Template, CodeType | str | None ] = weakref.WeakValueDictionary() + self.add_extension("jinja2.ext.loopcontrols") self.filters["round"] = forgiving_round self.filters["multiply"] = multiply self.filters["log"] = logarithm diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index ec6714bafe1..2434b2aed15 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -243,6 +243,26 @@ def test_iterating_domain_states(hass: HomeAssistant) -> None: ) +def test_loop_controls(hass: HomeAssistant) -> None: + """Test that loop controls are enabled.""" + assert ( + template.Template( + """ + {%- for v in range(10) %} + {%- if v == 1 -%} + {%- continue -%} + {%- elif v == 3 -%} + {%- break -%} + {%- endif -%} + {{ v }} + {%- endfor -%} + """, + hass, + ).async_render() + == "02" + ) + + def test_float_function(hass: HomeAssistant) -> None: """Test float function.""" hass.states.async_set("sensor.temperature", "12")