Force newlines or linebreaks in a template?

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…

I was looking for ideas to format the template email output from an automation.

This thread, combined with an old memory from my days of dabbling with JavaScript, prompted me to try something. There used to be an issue that required combining “carriage return” (/r) with “new line” (/n), to get a line break across all browsers so I tried it and it worked!

Sharing in the hope that it helps someone else:

{{"\r\n"}}

P.S. This StackOverflow thread gives background: string - What is the difference between \r\n, \r, and \n? - Stack Overflow

5 Likes

In home assistant, templates returning carriage returns depends on the destination. All carriage returns are removed when in attributes or sensor states.

Using payloads in messages or notifications or the markdown card, they are typically honored but you have to match what the OS requires.

1 Like
type: entities
entities:
  - type: section
    label: Offset (added to sensor reading)
  - type: section
    label: WARMER <--------------------> COOLER

Not templates, but I recently had a similar problem with card labels, and the only workaround I found was to use an entities card and add the label in a section, as shown. Other methods I tried - leaving a lbank line in the text, using \n, html markup, using a < after the colon, none of these worked. Labels within cards in the HA frontend are pretty important to get right, se there should be some means to avhieve this without using a complicated workaround.

Markdown card is the only item in the frontend that allows line breaks, everything else is clipped unless you add your own HTML

Old post I know, but you can force a word wrap via card-mod. The following works on a mushroom template card and will move text to a second line if there’s overflow.

card_mod:
  style:
    mushroom-state-info$: |
      span.primary {
      white-space: normal !important;
      }
2 Likes

I use br with a < in front and a > behind (when I tried it in the correct format it put a line break in this reply and didn’t show up!!) or \n to force line breaks in templates that I use for widgets on my Android phone. Works a treat.

1 Like

Try this:

As you can see from the example, br in lesser and more than brackets does the job nicely. Previously text would just wraparound but this hasn’t failed yet. Hope it helps.