Quotation marks and apostrophes

I use this script to display the title of a song. Mushroom and card-mod addons from HACS are also used here.

type: custom:mushroom-media-player-card
entity: media_player.kabinet_kompiuter
use_media_info: false
secondary_info: none

    .: |
      mushroom-state-info:after {
        content: '{{ state_attr(config.entity, 'media_title') }}';

But when the song title contains an apostrophe, the script breaks. A similar problem occurs when replacing an apostrophe with quotation marks.

Is there any way to avoid this?

Are you saying that this code does work when the song title does not contain an apostrophe?
I would be surprised when it does.
The content: line contains nested strings: the string media_title is nested inside the string
{{ state_attr(config.entity, 'media_title') }}.

Like the code is now, the compiler can interpret the content: line like this:
{{ state_attr(config.entity,
followed by some text:
followed by another string:
) }}

To have the compiler understand this correctly you have to use different types of quotation marks for the inner and the outer string.
Try this:

content: "{{ state_attr(config.entity, 'media_title') }}"

Or as an alternative:

content: '{{ state_attr(config.entity, "media_title") }}'

I tried to make 4 code configurations and 4 music track with different meta tags.

1. content: '{{ state_attr(config.entity, 'media_title') }}';
2. content: '{{ state_attr(config.entity, "media_title") }}';
3. content: "{{ state_attr(config.entity, "media_title") }}";
4. content: "{{ state_attr(config.entity, 'media_title') }}";

But this doesn’t solve the problem that when a quote or apostrophe doesn’t have a second closing element, it breaks the code when it doesn’t match a certain meta tag configuration.

A. Name of track: Track’ Title

Entity attribute:

media_title: Track' Title

1, 2 doesn’t work

B. Track" Title

media_title: Track" Title

3, 4 doesn’t work

C. Track" ’ Title

media_title: Track" ' Title

1, 2, 3, 4 doesn’t work

D. Track’ ’ Title

media_title: Track' ' Title

All work, but 1, 2 display Track Title , 3, 4 display Track’ ’ Title

You can use different ways to “escape” characters with a special meaning. Inside single quotes, you can put two single quotes or you could use unicode. A comprehensive sive guide can be found here:

I know about “escaping”, but I don’t understand, how I can use it with dynamic data from entity attribute which can contain different combinations of characters :sweat:

I think it may be that the way templating works is messing with it here. If it is expanding the media title in there and then parsing it, then this is in essence a code injection nightmare. I also think styles should ot be used in this way to specify content too… Is there another way to get the media title displayed?

Hm… I use - platform: universal in configuration.yaml for my players. Perhaps it is worth using this code to display the player status there? But won’t the same problem arise there?

I found a solution with universal player:

  - platform: universal
    name: TEST
      - media_player.desktoppc
    device_class: tv
    state_template: >
      {% if is_state('media_player.desktoppc', 'off') %}
      {% else %}

        {{ state_attr('media_player.desktoppc', 'media_title') }}

      {% endif %}

UPD: No, that’s not good solution, because that broke media player card, which can’t take correct standard state of entity :crying_cat_face:

1 Like

Work in similar places in my setup.

Oh, missed a part about “song name contains an apostrophe”….
Will check it then later.

In the docs you can find something about filters and automatic escaping. Might help.

1 Like