Best way to have a Jinja2 Template spit out multi-line?

Hey there,

I have chores that I keep track of with input numbers. There’s a threshold (30) that once they reach that, I need to do them. Examples are changing the HVAC filters, etc. Each one increments by 1 if it needs to be done once a month or 0.5 for example if I need to do it every 2 months.

The following template spits out the chores I need to do.

I want to have it organized better where each chore is on a newline. I’m sure this is something simple, but when I do this, and this template basically writes to a text helper that I show on a Dashboard, it’s all wonky and there’s a lot of weird spacing. I’m sure I’m missing something.

Thanks!

>

  {%- macro get_chores() -%}
    {% set allChores = states.input_number | selectattr('entity_id', 'search', 'chore_') | list %}
    {% set data = namespace(pastDueChores = []) %}
    
    {% for chore in allChores %}
      {% set currentChoreValue = states(chore.entity_id) | float %}
      {% if currentChoreValue > states('input_number.chores_threshold') | float %}
        {% set data.pastDueChores = (data.pastDueChores + [chore]) %}
      {% endif %}
    {% endfor %}

    {% for chore in data.pastDueChores %}
      {{ (chore.name | capitalize) ~ '.' }}
    {% endfor %}
  {%- endmacro -%}

  {%- macro cleanup(data) -%}
    {{ data.split("\n") | join("\n") }}
  {%- endmacro -%}

  {%- macro mother_of_all_macros() -%}
    {% if is_state('binary_sensor.outstanding_chores', 'on') %}
      {{ get_chores() }}
    {% else %}
      No pending maintenance
    {% endif %}
  {%- endmacro -%}

  {{ cleanup(mother_of_all_macros()) }}

You could put the template into a Markdown card and get more control over the result’s appearance (including the ability to employ newlines and HTML codes).

The following example displays the results in a table:

1 Like

Newlines does not work well with htm, but
is good

I know it works, but it can be a bit erratic, like not allowing double newlines without extra characters in between and value or code delimiters can also make strange things happen, if they are not pre-/suffix with a -.

Instead of making easily disproved claims, post an example demonstrating “a bit erratic”.


EDIT

Or did you mean it disallows triple newlines (or more)? Because in that case, you’re correct; the Markdown card won’t render more than two consecutive \n.

I have actually never really tested how many it allowed.
I just ran into the issue with newlines and having {{ }} be counted in too, so tried to use {{- -}}, but in the end it got too confusing on the card I made.