Macro: Use phrasing (different sentences or localisation) in jinja templates via macro

this is my first post here, wasnt too sure where to put it, forgive me if wrong choice …

Not sure anyone else finds this useful, it helped me have my automations respond via voice/notifications a bit more entertaining :wink:

This jinja macro allows you to define multiple sentences and reference them with keys.
e.g. Respond with “yes”, “cool”, “alright” varyingly. Does support variable substitution (“Yes, %name%” → “Yes, Tamay”).

Keys & phrases get defined in /custom_templates/phrases/topic.jinja
The macro needs to be placed at /custom_templates/phraser.jinja

Code for phraser.jinja:

{# build a sentence #}
{%- macro phraseit(context, topic, key, replacementarray = []) -%}
    {%- set ns = namespace(data=[], macro_output = 'empty') -%} 
    {%-for ptopic in context.glossary if ptopic.0 == topic -%}
        {%- set ns.data = ptopic.1[key] -%} 
    {%-endfor-%}
    {%- set ns.macro_output = ns.data | random -%}
    {%- if replacementarray is defined and replacementarray is iterable and replacementarray|count > 0 -%}
        {%- for rakey,ravalue in replacementarray.items() -%}
            {%- set ns.macro_output = ns.macro_output | replace('%'+rakey+'%', ravalue) -%}
        {%- endfor -%}
    {%- endif -%}
    {{ ns.macro_output }}
{%- endmacro -%}

{# combine glossary files #}
{%- macro loadglossary(topic) -%}
    {%- set gfile = 'phrases/'+topic+'.jinja' -%}
    {%- import gfile|string as glossary -%}
    {%-if glossary is defined and glossary.phrases is defined -%}
        {{ glossary.phrases|tojson }}
    {%-endif-%}
{%- endmacro -%}

{# load initital context #}
{%- macro init_context(phraseids) -%}
    {%- if phraseids is defined and (phraseids is string or phraseids is iterable) -%}
        {%- if phraseids is string and phraseids|length > 0 -%}
            {%- set phraseids = [phraseids] -%}
        {%- endif -%}
        {%- if phraseids is iterable and phraseids|count > 0 -%}
            {%- set nsg = namespace(gdata=[]) -%}
            {%-for pid in phraseids -%}
                {%-if pid is defined -%}
                    {%- set nsg.gdata = nsg.gdata + [(pid,(loadglossary(pid)|from_json))] -%}
                {%-endif-%}
            {%-endfor-%}
            {%- set pcontext = { 
                'topics': phraseids,
                'glossary': nsg.gdata
                } 
            -%}
            {{ pcontext|tojson }}
        {%- endif -%}
    {%- endif -%}
{%- endmacro -%}

Some example phrases:

/custom_templates/phrases/generic.jinja

  {%-
    set phrases = { 
      "yes": [
        "Yes",
        "Cool",
        "Alright"
      ],
      
      "no": [
        "No",
        "NEIN",
        "Nope",
        "nah"
      ],

      "confirm": [
        "Confirm",
        "daccord",
        "absolutely"
      ],

      "cancel": [
        "Cancel",
        "Stop"
      ]
    }
  -%}

/custom_templates/phrases/windwarning.jinja

{%-
    set phrases = {
      "ask_to_close": [
        "The weather forecast indicates strong winds and some of the blinds are open. Do you want me to close them?",
        "Wooooosh! Winds are picking up and your blinds are open, shall I close them?",
        "Its getting windy today, shall I close those open blinds for you?",
        "Careeeful! You got some blinds open and the wind is picking up! Shall I close em?"
      ],  

      "muted_confirmation": [
        "Uuggh! Ill shut up then. For today that is.",
        "Aight, ill shut up for today ...",
        "You have it. Wont bother you again today boss."
      ],

      "warn_closing": [
        "Gotcha! Ill close the blinds in 15 seconds. Please make sure they are unblocked!",
        "Make sure the blinds can retract freely, Ill close them in just a moment ..",
        "Dont forget to check that the blinds are unobstructed. Ill close em shortly..",
        "Closing blinds in 15 seconds. Make sure they can move freely!"
      ]
    }
  -%}

Initiate context supports either one topic specified as string or multiple topics specified as array of strings. Topics are key files that get placed in /custom_templates/phrases/

To be used in templates as following:

{% import 'phraser.jinja' as px %}          
{% set phrases = px.init_context(['generic','windwarning']) | from_json %} 
{{ px.phraseit(phrases, 'windwarning', 'ask_to_close', {'name':'Tamay'}) }}  
1 Like