Line breaks in list

I want to create a list in a markdown card with line breaks between each value from a template sensor but can’t figure out how to do that. Currently the output is in a string with each value broken up by a comma (which I also don’t want).

Can someone please advise what I need to add to get the line breaks as well as removing the comma?

Template sensor:
{{ expand('binary_sensor.doors') | selectattr('state', 'eq', 'on') |map(attribute='name') |list }}

Markdown card:

  - type: markdown
    card_mod:
      style: |
        ha-card {
          font-size: 12px !important;
          font-weight: 400;
        }
    content: '{{ states(''sensor.list_windows_open'') }}'

Current result in markdown card:

- type: markdown
    card_mod:
      style: |
        ha-card {
          font-size: 12px !important;
          font-weight: 400;
        }
    content: '{{ states(''sensor.list_windows_open'')|replace(",","\n") }}'
1 Like

Thanks, but I’ve added that and it removes the comma but doesn’t create a line break.

What about:

content: '{{ states(''sensor.list_windows_open'')|replace(",","<br>") }}'

2 Likes

Thanks Tom. That’s providing a list for me which has resolved that bit but in my markdown card it is showing encased in square brackets and quotes. Is there a way off cleaning it up so as not to show those?

*EDIT - I’ve figured out how to remove the square brackets, by adding |replace("["," ")|replace("]"," ") but when I try the same with the single quote marks it won’t accept it.

That’s because the markdown card content declaration is already surrounded by single quotes.

Use |replace("\x27","") instead.

1 Like

Perfect, thank you.

If that markdown card is the only place you use that template sensor, just replace the |list at the end of the expression with |join("<br>") — then you can use the sensor state directly in the card without all of the |replaces.

2 Likes

Great, thanks for your help.

You can put a line break between each value in your list by using the ‘n’ character.

"list_values = “value1nvalue2nvalue3"” will make a list with three values on three different lines.

1 Like

You mean \n. I tried that above, but the markdown card uses HTML and doesn’t render it with the line break.

1 Like

Try this:

As you can see from my example the br in lesser and more than brackets displays everything properly.

Okay… So, How do you make the first result be prefixed the same way as the others…

code here:

{{ states.media_player | rejectattr('state', 'equalto', 'off') | rejectattr('state', 'equalto', 'idle') | map(attribute ='entity_id') |join("\n  - ")}}

result here:

media_player.echos
  - media_player.livingroom_alexas
  - media_player.dlna_sony_tv_kdl_40r510c
  - media_player.dlna_lg_tv_nano80upa
  - media_player.this_device
  - media_player.grand_room_fire_tv_stick
  - media_player.stephen_s_alexa_app_for_pc
  - media_player.kitchen_dot
  - media_player.livingroom_east_dot
  - media_player.livingroom_west_dot
  - media_player.stephen_s_lg_webos_2021_tv
  - media_player.bedroom_dot
  - media_player.everywhere
  - media_player.bedroom_fire_tv_stick
  - media_player.stephen_s_fire_tv
  - media_player.living_room
  - media_player.this_device_2
  - media_player.plex_plex_for_android_tv_bedroom_firestick
  - media_player.plex_plex_for_android_tv_amazonfirestick
  - media_player.plex_plex_for_lg_lg_65nano80upa
  - media_player.plex_plex_for_android_tv_at_t_tv
  - media_player.plex_plex_for_android_mobile_galaxy_s23_ultra
  - media_player.homekit_lg_tv_9903

One way is to end with an indent filter:

{{ [...] | join('\n') | indent(width='- ',first=true) }}

That will still give you "- " if the list is empty, though. Another alternative:

{{ [...] | join('\n') | replace('media_player.','- media_player.') }}