Enhance Jinja with the import or include function

Now that I am doing more and more with TTS, I’d love to build a bunch of speech macros and then import them all and call them as needed.

Here is the Jinja part I am looking to use:
http://jinja.pocoo.org/docs/dev/templates/#import

and here is the code I want to try to reuse.
https://github.com/CCOSTAN/Home-AssistantConfig/blob/master/automation/Speech/announcements.yaml#L26-L38

The idea is I would be able to put all these voice commands into a voice.macro file and just import into all speech related automations and then build appropriate notifications with the automations. Allowing for reuse of morning, evening, holiday, and other nuance code.

http://jinja.pocoo.org/docs/dev/api/#loaders is what we need

I’ve been researching this concept of re-using templates for days (i.e. weeks) now . . . and there is such little to no information anywhere on it. Finally I found that this was a requested feature here. I also saw an old commit comment (from balloob) here that implied it may have been added a long time ago, but alas, none of my testing has been succesfull.

Per the docs it already includes a FileSystem loader implementation… for the life of me I can’t understand why the {% include %} and {% import %} instructions are not already supported in the HomeAssistant templateing… in my mind this is a necessity to not have to repeat your code and keep things DRY in you templating configurations.

I guess the current though process is that the Scripts workaround is sufficient (e.g. add another layer of Script, which is even more config you have to type to wrap each re-useable block of templating). But a script cannot be re-used to share among multiple Template Sensors (that I can think of). I think both have their value, but the Jinja macro implementations are very powerful HomeAssistant… and if they have access to the existing variables context (per Jinja docs (“import xxx with context”) then we can develop some awesome re-useable functionality and keep our configurations much much more modular and clean!

Still hoping that this will be implemented… and gave this my vote too, now that I FINALLY found it was already requested!!!

With the use of Python Scripts now, at least I can get alot more value out of enhanced logic and parameterization, that lets us truly re-use logic. Though we could do it before with command line based python scripts, the integrated python scrips is more elegant and I’ve found it to be a bit more stable.

While Jinja has support for this, and I’m not sure why HASS never supported the instructions . . . I wanted to follow up here to note that Python Scripts offer a good alternative for anyone else starting with HASS and looking for good ways to keep their automation code “DRY”!

Hi. Sorry to raise such an old thread, but I just submitted a PR to implement this. It’s something I need pretty badly, and I am sure others could use it, too.
Anyone want to take a look?

1 Like

Hi,

as I was looking for a solution as well for this issue I thought I might share my approach.

The idea: reuse a complex Jinja template in various locations.

I use lovelace_gen to include the file which includes a special formated Jinja template.

The File which includes the Jinja template:

example.txt

# lovelace_gen

{{ yaml_intro }}

  {{ pre_code }}
  
{% raw %}
  {# Complex Jinja template code goes here... #}
  {{ " World" }}
{% endraw %}
  {{ post_code }}

To use this code you can say for example:

cards:
  - !include
    - example.txt
    - yaml_intro: |
        type: markdown
        content: |
      pre_code: "{{- 'Hello' -}}"
      post_code: "{{- '!!!' -}}"

Which will show a Markdown card with ‘Hello World!!!’

PLEASE, make sure your lovelace_gen and yaml dashboard configuration is setup right, before you try this. To bootstrap your progress in case you have not set it up, yet. Here is my configuration:

lovelace_gen:

lovelace:
  mode: storage
  # Add yaml dashboards
  dashboards:
    lovelace-yaml:
      mode: yaml
      title: Home
      icon: mdi:home
      show_in_sidebar: true
      filename: lovelace/lovelace-home.yaml

Hope this helps some of you.
Ben