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
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.
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
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.
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;
}
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.
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.
i’ve used
- type: custom:template-entity-row
entity: sensor.web_course_coupons
name: Coupons
state: |
{{ " \n ".join(state_attr(config.entity, 'active_coupons')) }}
card_mod:
style: |
:host {
div.state {
background-color: rgba(0, 0, 0, 0.1);
white-space: pre-line;
line-height: 1.1em;
padding: 0 10px 10px 10px;
}
}
This is my result:
Reviving an old thread, but none of these solutions are working for me I’m using Mushroom Template Card and want to do a line break between the “Dirty/Clean/Unknown” text and the "Time Remaining: " text. What do I need to do to make this work?
{% if is_state('binary_sensor.kitchen_dishwasher_clean', 'off') %}
Dirty
{% elif
is_state('binary_sensor.kitchen_dishwasher_clean', 'on') %}
Clean
{% else %}
Unknown
{% endif %}
{% if states('sensor.kitchen_dishwasher_time_remaining') != '4:00' %}
Time Remaining: {{ states('sensor.kitchen_dishwasher_time_remaining') }}
{% endif %}
you have to use multiline_secondary
and the template needs to be in secondary field.
Thanks. I am using multiline_secondary and the template is in the secondary field. But it still displays with a ton of white space in between the primary and secondary fields Any idea how to fix?