Using Jinja to create multiple sensors & shorten config files

Hi all,

I’m currently looking for a way to clean some of my config files which are pretty cluttered.
I thought of using jinja to create multiple elements at once, using a syntax like this :

{% set containers = [0, 1, 2, 3, 4] %}
shell_command:
 {% for container in containers %}
   - restart_{{container}}: 'curl http://127.0.0.1:8126/container/home-assistant/restart'
 {% endfor %}

Obviously, since I’m writing this, it’s not working and I haven’t found a solution for now.

Would you know if what I’m trying to achieve is possible, or is it some alternative that could help me achieve something similar ?

Cheers !

You can enter that Template into the template into the template section of the developer tools, and then cut and paste the result into a YAML file. But template commands themselves don’t work like that in a config file.

For me, though, I find this sort of thing much easier in AppDaemon.

1 Like

Just as an FYI. Jinja/ansible can only be applied to a single field. It is not executed during load up of the yaml files. It’s only executed when home assistant knows ahead of time that the single field is a template field. So something like what you posted will unfortunately never work.

1 Like

Thank you for your answers ! This clarifies things for me. I’ll have a look at how to use appdaemon in order to achieve what I have in mind.