Configuration template for multiple similar instances

In my HA I’m using Siemens LOGO!8 via modbus.
I control 16 similar modbus switches, and that’s how my configuration looks like:

switches:
  - name: v1_1
    unique_id: v1_1
    write_type: coil
    address: 9
  - name: v1_2
    unique_id: v1_2
    write_type: coil
    address: 10

  - name: v2_1
    unique_id: v2_1
    write_type: coil
    address: 17
  - name: v2_2
    unique_id: v2_2
    write_type: coil
    address: 18

...

  - name: v16_1
    unique_id: v16_1
    write_type: coil
    address: 129
  - name: v16_2
    unique_id: v16_2
    write_type: coil
    address: 130

I’m wondering, if it’s possible to shorten such a long list of entities. E.g, loop some ${num} variable that changes from 1 to 16, so that this all would be templated as (pseudocode):

for num in range(1, 16)
 - name: v${num}_1
    unique_id: v${num}_1
    write_type: coil
    address: ${num * 8 + 1}
  - name: v${num}_2
    unique_id: v${num}_2
    write_type: coil
    address: ${num * 8 + 2}
}

I searched in the google and this community and found some hints regarding loops in automation scripts, but not in configuration.yaml…

Home Assistant process YAML first then Jinja2 second therefore you cannot use Jinja templating to generate YAML within configuration.yaml. More information here.

What you can do is use Jinja2 templating in the Template Editor to generate YAML, then copy the generated YAML into configuration.yaml (see example). It’s up to you to decide if that’s faster than creating a single entity’s configuration and then copy-pasting it to create other entity configurations.

Thanks for the answer.

I just did something similar using bash script, e.g.:

#!/bin/bash

logo_host="192.168.100.108"
logo_port="502"
logo_q_num="16"

#==================================

config="modbus.yaml"
truncate --size 0 ${config}

cat >> ${config} << EOF
name: hub1
type: tcp
host: 192.168.100.108
port: 502

switches:
EOF

for num in $(seq 1 $logo_q_num); do
cat >> ${config} << EOF
  - name: v${num}_0
    unique_id: v${num}_0
    write_type: coil
    address: $(($num * 8 + 0))
  - name: v${num}_1
    unique_id: v${num}_1
    write_type: coil
    address: $(($num * 8 + 1))
  - name: v${num}_2
    unique_id: v${num}_2
    write_type: coil
    address: $(($num * 8 + 2))

EOF
done

When I compared the result with the previous file written manually, I even found some small errors/typos I had.

So, templating (even such one done with bash) itself is a great thing that helps to avoid errors. And it’s a pity that it can’t be done with configuration files…

Unless of course the bash script that generates YAML contains an error … :wink:

Regardless of the chosen programming language (bash script, Jinja2, python, etc), there are pros and cons to creating YAML- generating code. The time needed to compose and test the code may be longer than simply copy-pasting a few blocks of YAML.

For future projects, you may wish to familiarize yourself with the concept of YAML Anchors and Aliases. I posted an example here: