Support for jinja templates in homeassistant.yaml configuration files

I’ve been using the hass-lovelace_gen add-on to add jinja support to lovelace cards, and I’ve found it incredibly useful. I really like being able to define re-usable macros. I’m using this for my 3D Floorplan config, and I couldn’t imagine writing this config without it. (If this didn’t exist then I would have to write my own scripts to generate the YAML configuration.)

Now I really miss these features while I’m working on the rest of my Home Assistant configuration. I’ve been splitting up my configuration into different packages, and I’ve come across lots of cases where variables, loops, and macros would be super useful. Here’s one example - I’m working on the configuration for iOS app actions, which currently look like this:

ios:
  actions:
    - name: Turn Off Lights
      background_color: "#073642"
      label:
        text: Turn Off Lights
        color: "#ffffff"
      icon:
        icon: lightbulb_group_off
        color: "#ffffff"
    - name: Arm Away
      background_color: "#cb4b16"
      label:
        text: Arm Away
        color: "#ffffff"
      icon:
        icon: alarm_light
        color: "#ffffff"
    - name: Arm Home
      background_color: "#268bd2"
      label:
        text: Arm Home
        color: "#ffffff"
      icon:
        icon: alarm_light
        color: "#ffffff"
   ...

Here’s what it would look like if I could write a jinja template and set up a macro:

ios:
  actions:
    {%- macro ios_action(name, icon, bg_color, text_color='ffffff') -%}
    - name: "{{ name }}"
      background_color: "#{{ bg_color }}"
      label:
        text: "{{ name }}"
        color: "#{{ text_color }}"
      icon:
        icon: "{{ icon }}"
        color: "#{{ text_color }}"
    {%- endmacro -%}
    {{ ios_action("Turn Off Lights", icon="lightbulb_group_off", bg_color="073642") }}
    {{ ios_action("Arm Away", icon="alarm_light", bg_color="cb4b16") }}
    {{ ios_action("Arm Home", icon="alarm_light", bg_color="268bd2") }}
    {{ ios_action("Disarm", icon="alarm_light_off", bg_color="859900") }}
    {{ ios_action("Unlock Front Door", icon="lock_off", bg_color="859900") }}
    {{ ios_action("Turn On Coffee Machine", icon="coffee", bg_color="8b4513") }}
    {{ ios_action("Disable Bedroom Motion Sensors", icon="motion_sensor_off", bg_color="073642") }}
    {{ ios_action("Turn On Bedside Lights", icon="lightbulb_on", bg_color="fdf6e3", text_color="073642") }}

The only downside I can think of is that I would need to start using {% raw %} ... {% endraw %} in my actual templates for sensors, etc.

Would it be possible add this as an optional feature? I think a lot of people would find this useful once their configuration starts to become more complex. Thanks!

Have you tried using #lovelace_gen on a config file? I haven’t but I don’t see why it wouldn’t work.

EDIT: It might require some changes to the code though now that I think about it.

1 Like

Unfortunately that doesn’t work when HA restarts. It does work after HA is booted and I can validate the configuration, but maybe lovelace_gen isn’t loaded when HA is initially parsing the config files.

It would be awesome if this was a built-in feature in Home Assistant, so that it’s also part of the test suite and doesn’t break in the future. Maybe the comment on the first line could be renamed to # template.

In the meantime, I want to try installing jinja-cli on my server and write a script that monitors my configuration directory. I’ll rename my templates to .yaml.jinja, and the script will compile them to .yaml files whenever it detects a change.

Is there something in HACS that already does this?

I finished my script and set up a systemd service. (I run Home Assistant Supervisor on a Debian server.) It’s pretty cool! I can view the jinja template and the compiled YAML side-by-side, and I also print any errors to a log file.

Check out a demo video here: Demo: Jinja templates for Home Assistant configuration - YouTube

I put the script / service in a gist: Script to compile and format *.yaml.jinja files in Home Assistant config directory (Including systemd service) · GitHub

2 Likes

This is awesome. Would be cool to convert it into an addon. I run homeassistant os and if it was an addon would make it really easy to run. I may try to build it out if I get some time

I’ve wrapped up @ndbroadbent’s script as an addon here GitHub - tonyroberts/ha-jinja2config: Compile Home Assistant yaml config files from jinja templates.

With this addon running, any files named “*.yaml.jinja” in your HA config folder will automatically get compiled to yaml when you save them.

1 Like