Skip to content

Allow list.append in Jinja2 #33678

Description

@i00

The problem

Using list.append causes an error:
SecurityError: access to attribute 'append' of 'list' object is unsafe.

Environment

  • Home Assistant Core release with the issue: 0.107.7
  • Last working Home Assistant Core release (if known): N/A
  • Operating environment (Home Assistant/Supervised/Docker/venv): Docker
  • Integration causing this issue: Templating?
  • Link to integration documentation on our website: https://www.home-assistant.io/docs/configuration/templating/

Problem-relevant configuration.yaml

Jinja template:

{% set entities = [] %}
{% for state in states.cover %}
 {{ entities.append(state.entity_id) }}
{% endfor %}
{{ entities|join(',') }}

Traceback/Error logs

SecurityError: access to attribute 'append' of 'list' object is unsafe.

Additional information

I can understand why this could be a security issue if you could append to states.cover for example ... but for local variables??

I know there are workarounds like:

{%- set entities = namespace() %}
{%- set entities.entities = '' %}
{%- for state in states.cover -%}
  {%- if entities.entities != '' %}{% set entities.entities = entities.entities + ',' %}{%- endif %}
  {%- set entities.entities = entities.entities + state.entity_id %}
{%- endfor %}
{{- entities.entities }}

... but this is not as nice and somewhat tedious.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions