Jinja2: multi-line code as single-line output?

Hi everybody,

I tried researching this, but I was not quite sure what to look for. When using data_template, there are these symbols that, if I understand correctly, define how to structure the output. I came across |, >, and >- so far and tried all three, but it didn’t work. My automation looks like this

automation:
  - alias: "[Notify] Wasserstand"
    trigger:
      - platform: numeric_state
        entity_id: sensor.tonne_links
        below: 200
      - platform: numeric_state
        entity_id: sensor.tonne_rechts
        below: 200
    action:
      - service: notify.ben
        data_template:
          message: |
            Achtung, die Pumpe ist in der
            {% if is_state("input_select.pumpe_in_tonne", "Links") %}
              linken
              {% elif is_state("input_select.pumpe_in_tonne", "Rechts") %}
              rechten
              {% endif %} Tonne, und dort sind nur noch
            {% if is_state("input_select.pumpe_in_tonne", "Links") %}
              {{ states("sensor.tonne_links") }}
              {% elif is_state("input_select.pumpe_in_tonne", "Rechts") %}
              {{ states("sensor.tonne_links") }}
              {% endif %} Liter drin.

This will result in a message like this

grafik

What do I have to do in order to get the output as one single line? I have had cases where I’d have preferred multiple lines and just got a single line (way back, so I cannot find them any more to check), and now it is the opposite.

(I assume I could just write the entire code in one line, but that would look terrible and be harder to modify later on imho)

Thank you for your help :slight_smile:

Add a “-” to the start and end of each “if” etc.

{%- if… - %}

{%- elif… -%}

{%- endif -%}

Etc etc.
Just note that this strips all whitespace, including " ", from the beginning and end of each line, so you might have to reformat the output a bit to add spaces between words again.

2 Likes

Thank you. I tried reformating the output by adding \<space> at the beginning and end of each line. This will literally output the \, though. If I do not use it ( only), nothing will happen. What is the correct way to do this in jinja2?

I am not sure you can do it like that.
What I usally do is that I assign all output to a variable, and then simply output that at the end of the script.

So something like

          message: >-
            {% set output = "Achtung, die Pumpe ist in der " %}
            {%- if is_state("input_select.pumpe_in_tonne", "Links") -%}
              {% set output = output ~ "linken " %}
            {%- elif is_state("input_select.pumpe_in_tonne", "Rechts") -%}
              {% set output = output ~ "rechten " %}
            {%- endif -%} 
            {% set output = output ~ "Tonne, und dort sind nur noch " %}
            {%- if is_state("input_select.pumpe_in_tonne", "Links") -%}
              {% set output = output ~ states("sensor.tonne_links") %}
            {%- elif is_state("input_select.pumpe_in_tonne", "Rechts") -%}
              {% set output = output ~  states("sensor.tonne_rechts") }}
            {%- endif -%} 
            {% set output = output ~ " Liter drin." %}
            {{ output }}

But I might have missed something.

1 Like

Another alternative is of course to just do

          message: >-
            {%- if is_state("input_select.pumpe_in_tonne", "Links") -%}
              {% set tonne = "linken" %}
              {% set liter = states("sensor.tonne_links")  %}
            {%- else -%}
              {% set tonne = "rechten" %}
              {% set liter = states("sensor.tonne_rechts")  %}
            {%- endif -%]
            Achtung, die Pumpe ist in der {{ tonne }} Tonne, und dort sind nur noch {{ liter }} Liter drin.
1 Like

use variables:

 {%- if is_state("input_select.pumpe_in_tonne", "Links") %}
   {%- set pumpe = 'linken' %}
   {%- set tonne = states("sensor.tonne_links") %}
 {%- elif is_state("input_select.pumpe_in_tonne", "Rechts") %}
   {%- set pumpe = 'rechten' %}
   {%- set tonne = states("sensor.tonne_links") %}
 {%- endif %}
 Achtung, die Pumpe ist in der {{ pumpe }} Tonne, und dort sind nur noch {{ tonne }} Liter drin.
1 Like

you’ll need the -'s before each set because you’ll still get 2 carriage returns with the double sets in a row. Easier to just place the - in the beginning of each line instead of the beginning and end.

1 Like

Thank you! Works nicely, and I learned something new. I will try implementing variables in all my templates from now in.