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:


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


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
  - 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.

    mushroom-state-info$: |
      span.primary {
      white-space: normal !important;
1 Like