Notify file auto inserts new line

been fighting with this all day, and I can not prevent the notification to file to Not insert a new line in the output. And that makes the sensor showing the last line (what it is designed to do) somewhat useless…

where this is the whole notification filed:

25 Jan: 15:05:07 - mobile_app_calltheboss: 2 batteries are below 15 %:  Master bedroom dimmer switch Battery: (2%),
 Telefoon Battery Level: (6%),

it only shows me:

in my entities card:

entities:
  - type: custom:hui-element
    card_type: markdown
    content: >
      {{states('sensor.filed_notifications')}}
  - sensor.filed_notifications

Ive tried to trim the message:

      service: notify.filed_notifications
      data:
        message: >
          {% set message = trigger.event.data.service_data.message|trim %}
          {% set service = trigger.event.data.service|trim %}
            {{now().timestamp()|timestamp_custom('%d %b: %X')}} - {{service}}: {{message}}

but that doesnt help.

# https://www.home-assistant.io/integrations/file/#notifications
  - name: filed_notifications
    platform: file
    filename: /config/logging/filed_notifications.txt

and the sensor:

sensor:

  - platform: file
    file_path: /config/logging/filed_notifications.txt
    name: Filed notifications
    value_template: >
      {% if value is not none %}
        {% if value|length < 255 %} {{value}}
        {% else %} Truncated: {{value|truncate(240,True, '')}}
        {% endif %}
      {% endif %}

what could I be improving to not have the message get a new line?

or is it core/notify.py at 1093a2b80892afc4039a72f686b2399a021bb8c4 · home-assistant/core · GitHub causing my issue

Replace message: > by message: >- and all (or some) {% and %} by {%- and -%}.

See https://yaml-multiline.info/

thanks, and yes, I have been testing that extensively in dev template but there’s no new line added in the first place

BUT: I found the culprit:

            {%- set alert_level = states('input_number.battery_alert_level')|int(default=0) %}
            {%- set count = expand('group.battery_sensors')
                  |rejectattr('state','in',['unknown','unavailable'])
                  |map(attribute='state')
                  |map('int',default=0)
                  |select('<',alert_level)
                  |list|count %}
            {% set phrase = 'battery is' if count == 1 else 'batteries are' %}
            {%- if count > 0 %}
            {%- for s in expand('group.battery_sensors')
                          if s.state not in ['unknown','unavailable','None'] and
                             s.state|int(default=0) < alert_level %}
            {%- if loop.first %}{{loop.length}} {{phrase}} below {{alert_level}} %: {% endif %}
            {{s.name + ': ('+ s.state + '%),\n'}}
            {%- endfor %}
            {%- else %} All batteries above {{alert_level}} %
            {%- endif %}

fear it’s another huge USER error… now I need to fix that into a nice message without a \n and have it create a correct list (and also kill the closing , )

need something like this:

            {%- set alert_level = states('input_number.battery_alert_level')|int(default=0) %}
            {%- set count = expand('group.battery_sensors')
                  |rejectattr('state','in',['unknown','unavailable'])
                  |map(attribute='state')
                  |map('int',default=0)
                  |select('<',alert_level)
                  |list|count %}
            {% set phrase = 'battery is' if count == 1 else 'batteries are' %}
            {%- if count > 0 %}
            {{count}} {{phrase}} below {{alert_level}} %: 
            {%- set ns = namespace(batteries=[]) %}
            {%- for s in expand('group.battery_sensors')
              if s.state not in ['unknown','unavailable','None'] and
               s.state|int < alert_level %}

            {%- set ns.batteries = ns.batteries + [s.name+ ': (' + s.state + '%)'] %}
            {{ns.batteries|join(',')}}
            {%- endfor %}
            {%- else %} All batteries above {{alert_level}} %
            {%- endif %}

but now its returning the first sensor twice…

oops. put the {{ns.batteries|join(',')}} inside the for loop… duh

            {%- set alert_level = states('input_number.battery_alert_level')|int(default=0) %}
            {%- set count = expand('group.battery_sensors')
                  |rejectattr('state','in',['unknown','unavailable'])
                  |map(attribute='state')
                  |map('int',default=0)
                  |select('<',alert_level)
                  |list|count %}
    
            {% set phrase = 'battery is' if count == 1 else 'batteries are' %}
   
             {%- if count > 0 %}
            {{count}} {{phrase}} below {{alert_level}} %: 
            {%- set ns = namespace(batteries=[]) %}
            {%- for s in expand('group.battery_sensors')
              if s.state not in ['unknown','unavailable','None'] and
               s.state|int < alert_level %}

            {%- set ns.batteries = ns.batteries + [' ' + s.name+ ': (' + s.state + '%)'] %}
            {%- endfor %}
            {{- ns.batteries|join(',')}}
            {%- else %} All batteries above {{alert_level}} %
            {%- endif %}