Generate yaml blocks

Hello,

I try to create sensors dynamically throught a loop :

{% for item in group.all_zigate.entity_id %}
- platform: template
  sensors:
    {{ item }}_battery_level:
      friendly_name: "Batterie {{ item.friendly_name}}"
      unit_of_measurement: 'percent'
      value_template: "{{ state_attr('{{ item }}', 'battery_level') }}"
{% endfor %}

But I get this error :

ERROR (SyncWorker_0) [homeassistant.util.yaml.loader] while scanning for the next token
found character ‘%’ that cannot start any token

am I doing wrong or not possible to do that ?

Thanks

You can only use templates in values that accept templates. You can’t template a whole block of config like that.

In your config above the only place a template can go is in value_template:

As @tom_l says this is not possible, but a solution to your situation of having battery attributes is available on hacs in the form of the attribute sensor.

I was just discovering and installing hacs :slight_smile:
Attributes integration seems perfect, thx !