Force newlines or linebreaks in a template?

hi,

i have a template that looks like this. is there a newline character or something that forces linebreaks between day_0 and day_1, etc.? as it is now, the lines just wrap and break according to polymer rules or whatever.

thanks!

platform: template
sensors:
  five_day_forecast:
  friendly_name: 'five day'
  value_template: >
    {{states.sensor.yweather_day_0_condition.state}}, {{states.sensor.yweather_day_0_temperature_min.state}}-{{states.sensor.yweather_day_0_temperature_max.state}}
    {{states.sensor.yweather_day_1_condition.state}}, {{states.sensor.yweather_day_1_temperature_min.state}}-{{states.sensor.yweather_day_1_temperature_max.state}}
    {{states.sensor.yweather_day_2_condition.state}}, {{states.sensor.yweather_day_2_temperature_min.state}}-{{states.sensor.yweather_day_2_temperature_max.state}}
    {{states.sensor.yweather_day_3_condition.state}}, {{states.sensor.yweather_day_3_temperature_min.state}}-{{states.sensor.yweather_day_3_temperature_max.state}}
    {{states.sensor.yweather_day_4_condition.state}}, {{states.sensor.yweather_day_4_temperature_min.state}}-{{states.sensor.yweather_day_4_temperature_max.state}}
2 Likes

Bump, I would like to know too. I have a sonarr sensor for the day’s tv episodes and would like have a line break after each.

- platform: template
  sensors: 
    tv_schedule:
      value_template: >-
        {% for attr in states.sensor.sonarr_upcoming.attributes %}
        {% if not attr=="friendly_name" and not attr=="icon" and not attr=="unit_of_measurement" %}
        {{attr}} 
        {%- endif %}
        {%- endfor -%}

I’m looking for the same thing. Has anyone figured this out yet?

I’m also looking for this. Hopefully this message get the attention of someone who has already figured this out.

I found this page which I thought was the solution. Using {{ '\n' -}} where you want the line break does work in the dev tools template editor but it doesn’t seem to have an effect in an actual template sensor. Here is the sensor that I’m trying to use…

template_ha_version_display:
  friendly_name: 'HA Version'
  value_template: >-
    {%- if states.sensor.current_installed_ha_version.state == states.sensor.current_release_ha_version.state -%}
      - Up To Date -
    {{- '\n' -}}
      Version {{states.sensor.current_release_ha_version.state}}
    {%- else -%}
      Installed {{states.sensor.current_installed_ha_version.state}}
    {{- '\n' -}}
      Available {{states.sensor.current_release_ha_version.state}}
    {%- endif -%}

7 Likes

I have a similar problem and haven’t found any solution searching at stackoverflow and here.

Anyone else that has solved this?

1 Like

Hrmm, trying to do the same, and as others have said, it seems to work in the template editor but not in the actual sensor.

Anyone come across new ideas?

  - platform: template
    sensors:
      neweps:
        value_template: >
          {%- for attr in states.sensor.sonarr_upcoming.attributes -%}
          {%- if not attr=="friendly_name" and not attr=="icon" and not attr=="unit_of_measurement" and not attr=="hidden" %}
          {{attr}}:{{states.sensor.sonarr_upcoming.attributes[attr]}}
          {{- '\n' -}}
          {%- endif -%}
          {%- endfor -%}
3 Likes

Seems it might just be Chrome on PC.
The above code works nicely in Safari on iOS and Edge on PC.

Does anyone find a solution?
I log some sensors states on a txt file but cannot achieve the new line.
As discussed aboce I’ve tried {- '\n' -} but it works only in the template.

Appreciate your help
Thanks in advance

try {{"- \n -"}}

2 Likes

Now broken on Edge too - suspect since they changed rendering engine.

I realize this is a little old, but I was looking for the same solution. In case others are looking:
This answer worked for me. Note that the dashes and spaces were literally rendered so if you don’t want extra spaces and the dashes, just use {{\n}}. Btw, I used this in an email message.

1 Like

this one doesnt work for me, when i change it to {{\n}} it literally prints that

Carriage returns typically will not work in any template. Most templates strip all whitespace.

I’m using {{ … | join(’\n’) }} to get a newline in a template

carriage lines will only work in markdown cards templates. It might work in the message field for notifications. Outside that, it’ll get stripped.

And it’ll work in the template editor

1 Like

You are right, i’m using it in a telegram message for readability.

It might be better to list where it wont work: any template that represents a entity state

For notifications using the HA companion app one can use html line breaks {{'<br>'}} , {{'\n'}} gets stripped.

6 Likes

Possibly related: I created an automation which gets triggered by messages on a specific mqtt topic and sends “{{ trigger.payload }}” to the logbook. In the payload I can see formatting like “\n” and “\t” but these seem to get stripped out from the logbook messages.

I went to a lot of trouble to create nicely formatted status reports ! Is there no way to preserve these characters in logbook entries ?

Actually just now I have found a workaround, it is not perfect, but if you precede the first word of each line with something like “…” then the line gets broken before the dots and the word. It might work in the case of templates also. Just make sure not put enough dots to force a linebreak and not leave whitespace betweem the dots and the first word of the line.
So something like “line of text one …line of text two”

Edit: actually it is not very good. If you change the font size you lose the word wrap…