Makejinja: Automatically generate complex Home Assistant configurations using Jinja templates

makejinja can be used to automatically generate files from Jinja templates. This allows you to load variables from external files or create repeating patterns via loops. A very interesting use case for this tool is generating config files for Home Assistant: Using the same language that the built-in templates use, you can greatly simplify your configuration. When creating for example dashboards, it allows you to create a view for each room based on a single common template, dramatically reducing the maintenance overhead of complex dashboards. I originally developed this for my smarthome setup, but thought it may be useful for others as well!

Home Assistant Example

A concrete example for Home Assistant can be found in the tests directory.

Features

  • Recursively convert nested directories containing template files. One can even specify a pattern to specify relevant files in a folder.
  • Load data files containing variables to use in your Jinja templates from YAML, TOML, and Python files.
  • Use custom functions in your Jinja templates by loading custom filters and/or globals.
  • Easily load bundled as well as custom Jinja extensions.
  • Tailor the whitespace behavior to your needs.
  • Use custom delimiters for Jinja blocks/comments/variables.
  • Modify all init options for the Jinja environment.
  • Write custom Python loaders that implement a subset of our fully typed abstract loader class (more details in the GitHub repo).

Installation

makejinja is available via pip and can be installed via

pip install makejinja

Beware that depending on other packages installed on your system via pip, there may be incompatibilities. Thus, we advise leveraging pipx instead:

pipx install makejinja

Alternatively, the application can also be used via Docker. We automatically publish an image at the GitHub Container Registry. More information on its usage can be found in the project’s README.

Usage

In its default configuration, makejinja searches the input folder recursively for files ending in .jinja. Also, we copy all contents (except raw template files) of the input folder to the output folder and remove the .jinja ending during the render process. To get an overview of the remaining options, we advise you to run makejinja --help. The output of this command can also be viewed in the GitHub repo.

Development

makejinja is tested automatically with pytest and releases are automated using GitHub actions. I am happy for any kind of contributions, be it an issue or a pull requests draft. Surely there will be scenarios that I did not envision during development of this tool, so feel free to open a discussion for feature requests as well.

Please :star: this project if you find it useful :smiley:

The GitHub link can be found right at the beginning of this post.

9 Likes