Jinja is loosing format in "set variable" service

Hi,

For the key “value” in the service “var.set” I want to use a template using Jinja.
Unfortunately it is formatting the code as a string inserting \n etc. which makes it unreadable.
It seems that this behaviour is dependent on the service.
is there a workaround?

      - service: var.set
        alias: Arbeitspreis Wärme
        metadata: {}
        data:
          value: "{# Arbeitspreis ermitteln (einmal im Monat) #}\n{% set preis = ({ \n                  \"2024.7.1\": 112.95,\n                  \"2024.10.1\": 112.95\n                  }) %}\n\n{% set ns = namespace(break=false) %}  {% set ns = namespace(fee=preis[\"2024.7.1\"]) %} {% set now = now()\t%}  {% set day =  now.year |string  + '.' + now.month |string + '.1'%}         {% for key in preis %}\n  {% if not ns.break  %}\n    {% if day <= key  %}\n      {% set ns.fee = preis[key] %}\n      {% set ns.break = true %}\n    {% endif %}\n  {% endif %}\n{% endfor %} \n{{ ns.fee }}"
        target:
          entity_id: var.arbeitspreis_warme

I also tried with a different syntax

      - service: var.set
        metadata: {}
        data:
          value_template: "{% set tibber_fee = ({ \n      \"2024.7.1\": 3.35*1.19,\n      \"2024.10.1\": 3.35*1.19\n      }) %}\n{% set net_metering_fee = ({ \n                   \"2024.7.1\":   (0.0459 + 0.1152)*1.19,\n                   \"2024.10.1\":  (0.0459 + 0.1152)*1.19\n                   }) %}\n{# Calculate tibber fea per minute #}\n                      \n{% set ns = namespace(break=false) %}   {% set ns = namespace(fee=tibber_fee[\"2024.7.1\"]) %}  {% set now = now()\t%}   {% set day =  now.year |string  + '.' + now.month |string + '.1'\t%}          {% for key in tibber_fee %}\n  {% if not ns.break  %}\n    {% if day <= key  %}\n      {% set ns.fee = tibber_fee[key] %}\n      {% set ns.break = true %}\n    {% endif %}\n  {% endif %}\n{% endfor %} \n{% set no_days_in_month = ((now.replace(day=1) + timedelta(days = 32)).replace(day=1) -timedelta(days=1)).day %} {% set tibber = ns.fee/(no_days_in_month *24*60) %}\n{# Calculate network and measurement service fea per minute #}  {% set ns.break = false %}  {% for key in net_metering_fee %}\n  {% if not ns.break  %}\n    {% if day <= key  %}\n      {% set ns.fee = net_metering_fee[key] %}\n      {% set ns.break = true %}\n    {% endif %}\n  {% endif %}\n{% endfor %} {% set network =  ns.fee / (24*60)  %}   {{network + tibber }}"
          entity_id: var.minutliche_grundgebuhr_bezug
        alias: Minütliche Grundgebühr Tibber und Netz- und Messstellen
type or paste code here

but no luck

Why don’t you remove the /n from the yaml?

I did not insert them in the first place.
I insertes a nicely formated jinja file containing new lines etc.

value_template: >
    {% set tibber_fee = ({ 
                      "2024.7.1": 3.35*1.19,
                      "2024.10.1": 3.35*1.19
                      }) %}
    {% set net_metering_fee = ({ 
                       "2024.7.1":   (0.0459 + 0.1152)*1.19,
                       "2024.10.1":  (0.0459 + 0.1152)*1.19
                       }) %}
    {# Calculate tibber fea per minute #}
                          
    {% set ns = namespace(break=false) %}  
    {% set ns = namespace(fee=tibber_fee["2024.7.1"]) %} 
    {% set now = now()	%}  
    {% set day =  now.year |string  + '.' + now.month |string + '.1'	%}         
    {% for key in tibber_fee %}
      {% if not ns.break  %}
        {% if day <= key  %}
          {% set ns.fee = tibber_fee[key] %}
          {% set ns.break = true %}
        {% endif %}
      {% endif %}

etc ...


but when I save the automation and open the automation yaml again, I have this string contain \n and all in one line.
Presently I am using the bloody workaround to save the Jinja templates in separated text file I am storing on a different computer.

Are you using the Automation Editor?

That’s how the automation editor works. That’s also why I seldom use it and never use it for things I custom edit.

I bet it still functions. It’s not jinja doing that, it’s the UI editor in HA.

You might want to look at splitting your config if you do a lot of manual editing.

Splitting up the configuration - Home Assistant.

yes, it is working but it is hard to change and maintain.

yes, I am using the automation editor. I
s there another way aside editing the automation.yaml which I would like to avoid?

Looking at what you have, you could consider making that a macro and use that where you use the variable.

See the section on reusing templates.

1 Like

The Automation Editor formats YAML according to its own guidelines.

The only way to avoid it is to not use the Automation Editor and create your automations directly in YAML using a text editor (such as Visual Studio Code). In that case, you must split the configuration (see link above) in order to separate the automations created with the Automation Editor from the ones created with a text editor.

If you don’t split the configuration and keep all automations in one place, if you create an automation with a text editor and then later use the Automation Editor, it will reformat it.

If you don’t want to use a text editor and split the configuration, you will have to accept how the Automation Editor formats YAML.

@ parautenbach
Thanks for your advice. I am using Jinja macros now.

1 Like