Media title splitting error

I have internet radio (Frontier Silicon based) and i get “media_title” attribute where station name - artist - title is stated. However, sometimes one of my stations stop sending this data and sends “www.radio80.si” instead. I have a number of template sensors, which splits above info into separate sensors, so when this happens i get tons of warnings in my HA log

Template variable warning: list object has no element 2 when rendering...

It’s clear WHY this warning occurs: there IS no second (and third) object in “www.radio80.si” when delimiter is “-”. But, how to write template sensor to eliminate these warnings?
my current sensors:

(Some explanation about multiple “if”'s: station “radio 1 80-ta” differs from others with media_title format…)

# media_title attribute contains this info: station_name - artist - song
# so delimiter is "-"
#
# this is sensor for display current station  - this one works ok:
- name: trenutna postaja Pavle
  state: >
    {%if states('media_player.pavle_internetni_radio')=='off'%}
      izključen...
    {%else%}
      {{ states.media_player.pavle_internetni_radio.attributes.media_title.split(" -")[0] }}
    {%endif%}
#------------------------------------
# this is sensor for display current artist - here i get warnings about not existing element:
- name: trenutni izvajalec Pavle
  state: >
    {%if states('media_player.pavle_internetni_radio')=='off'%}
      izključen...
    {%elif is_state('sensor.trenutna_postaja_pavle','Ekspres Slo')%}
        {{ states.media_player.pavle_internetni_radio.attributes.media_title.split(':')[1].split('-')[0]}}
    {%elif is_state('sensor.trenutna_postaja_pavle','Radio 1 80-a')%}  
      {{ states.media_player.pavle_internetni_radio.attributes.media_title.split('-')[2]}}
    {%else%}
        {{ states.media_player.pavle_internetni_radio.attributes.media_title.split("-")[1] }}
    {%endif%}

# this is sensor for display current song
- name: trenutna pesem Pavle
  state: >
    {%if states('media_player.pavle_internetni_radio')=='off'%}
      izključen...
    {%elif is_state('sensor.trenutna_postaja_pavle','Radio 1 80-a')%}
      {{ states.media_player.pavle_internetni_radio.attributes.media_title.split("-")[3] }}
    {%else%}
      {{ states.media_player.pavle_internetni_radio.attributes.media_title.split("-")[2] }}
    {%endif%}

#dolžina imena pesmi za avtomatsko izbiro velikosti pisave
- name: dolžina imena pesmi Pavle
  state: >
    {{ states('sensor.trenutna_pesem_pavle') |length}}

I do it this way - seems to stop all the errors (also a Frontier Silicon device). Mine is so I create Spotify playlists while I listen to the radio - all done natively with HA, no other integrations :slight_smile:

Here is how I make sure I know what will be in the sensor.

      media_artist:
        friendly_name: "Artist"
        unique_id: lr_artist
        value_template: >-
          {% if states("media_player.ituner_320bt")|default('') != 'off' %}
              {% if states("media_player.ituner_320bt")|default('not playing') == 'playing' %}
                {% if state_attr('media_player.ituner_320bt', 'media_artist')|default('not playing') == none %}
                    {% if states.media_player.ituner_320bt.attributes.media_title.split(' - ')[0]|default('not playing') != 'Radio Caprice' and states.media_player.ituner_320bt.attributes.media_title.split(' - ')[0]|default('not playing') != 'RADIO BOB!' %}
                      {{ states.media_player.ituner_320bt.attributes.media_title.split(' - ')[1]|default('not playing') }}
                    {%else%}
                      {{ states.media_player.ituner_320bt.attributes.media_title.split(' - ')[2]|default('not playing') }}
                    {%endif%}
                {%else%}
                   {{ states.media_player.ituner_320bt.attributes.media_artist|default('not playing') }}
                {%endif%}
              {%else%}
                not playing
              {%endif%}
          {%endif%}
1 Like

Hm… so, you say that only option is “to complicate” my template even more…? I mean to create additionl “if” ‘s and check if elements 2 and 3 really exist…

There’s no magic bullet, but this has worked for me for 3 years. I used to get annoying error messages on restart because the media_title wouldn’t be there - this got rid of all that. Also note the default options which will trick HA.

Yes, lack of default options is, i guess, one of reasons for my errors… will add them pronto…
Thanks!

1 Like

I have another question: is it possible to split “media_title” to left and right part, delimited by first ‘-’ ?
Example: i have this media title content:

Radio 1 - Nik Kershaw - The Riddle

Now i’d like to split that to

“Radio 1”

and all the rest from minus sign:

“Nik Kershaw - The Riddle”

Basically i’d like to cut off station name. Possible or not?

THX

Yes, here’s an example where I need to cut out the station name as it’s always at the start :

{% if states.media_player.ituner_320bt.attributes.media_title.split(' - ')[0]|default('not playing') != 'Radio Caprice' and states.media_player.ituner_320bt.attributes.media_title.split(' - ')[0]|default('not playing') != 'RADIO BOB!' %}
                     {{ states.media_player.ituner_320bt.attributes.media_title.split(' - ')[0]|default('not playing') }}
                  {% else %}
                     {{ states.media_player.ituner_320bt.attributes.media_title.split(' - ')[1]|default('not playing') }}
                  {% endif %}

As you can see after the .split there is a number in the square brackets - [0] is the first “string” before the ‘-’ and [1] is the second and so on. So to do what you want you would either concatenate [1] and [2] or there maybe another way which I haven’t encountered.

1 Like

Ahhh… magic thing that i didn’t notice: separator between station name and all other info is “space-minus-space” while separator between artist and title is only “minus”… that’s too easy… i hope that this goes for all stations and it won’t change…
thanks!

1 Like