Force newlines or linebreaks in a template?

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.