Help for a universal media player

I’m trying to configure an FM radio device to be controlled by IR as a universal media player.
I defined the list of radio channels as an input_select.
I have defined an automation that, when the selected element changes, executes scripts, each of them sends the relevant IR code via a Broadlink switch.

When I change the selection through the input_select displayed on the home page everything works perfectly.
The channel change done through the channel list displayed inside the universal media player does not work.
I have done dozens of different attempts but I can not make it work in any way.

Here are my configurations:

Input_select

  studio_radio_elenco_canali:
    name: Elenco canali radio studio
    options:
      - '1 Radio Deejay'
      - '2 Radio Capital'
      - '3 Radio Montecarlo'
      - '4 Virgin Radio'
      - '5 RDS'
      - '6 Radio Nostalgia'
      - '7 RAI Radio 1'
      - '8 RAI Radio 2'
      - '9 RAI Radio 3'
    initial: 1 Radio Deejay
    icon: mdi:radio

automation

  - alias: 'cambio canale radio'
    hide_entity: true
    trigger:
      - platform: state
        entity_id: input_select.studio_radio_elenco_canali
    action:
      - service: script.studio_radio_cambio_canale

script

  studio_radio_cambio_canale:
    alias: Studio Radio cambio canale
    sequence:
      - service: script.turn_on
        data_template:
          entity_id: >
            {% if is_state('input_select.studio_radio_elenco_canali', '1 Radio Deejay') %}
              script.studio_radio_canale_1
            {% elif is_state('input_select.studio_radio_elenco_canali', '2 Radio Capital') %}
              script.studio_radio_canale_2
            {% elif is_state('input_select.studio_radio_elenco_canali', '3 Radio Montecarlo') %}
              script.studio_radio_canale_3
            {% elif is_state('input_select.studio_radio_elenco_canali', '4 Virgin Radio') %}
              script.studio_radio_canale_4
            {% elif is_state('input_select.studio_radio_elenco_canali', '5 RDS') %}
              script.studio_radio_canale_5
            {% elif is_state('input_select.studio_radio_elenco_canali', '6 Radio Nostalgia') %}
              script.studio_radio_canale_6
            {% elif is_state('input_select.studio_radio_elenco_canali', '7 RAI Radio 1') %}
              script.studio_radio_canale_7
            {% elif is_state('input_select.studio_radio_elenco_canali', '8 RAI Radio 2') %}
              script.studio_radio_canale_8
            {% elif is_state('input_select.studio_radio_elenco_canali', '9 RAI Radio 3') %}
              script.studio_radio_canale_9
            {% else %}
                none
            {% endif %}

  studio_radio_canale_1:
    alias: 1 Radio Deejay
    sequence:
      - service: switch.broadlink_send_packet_192_168_20_41
        data:
          packet: "JgDMAHM5Dg8OKw4PDQ8ODw4ODg8ODw0PDg8ODg4PDg8NLA4ODg8ODw0PDg8ODg4PDisODw4rDg4ODw4rDg8ODg4PDg8NDw4PDg4ODw4PDSwODg4PDg8NDw4PDisODw0sDisODw0sDgAJi3I6Dg4OLA4ODg8ODg4PDg8NDw4PDg4ODw4PDQ8OKw4PDg8NDw4PDg4ODw4PDSwODg4sDQ8ODw4rDg8NDw4PDg4ODw4PDQ8ODw4ODisODw4PDg4ODw4PDSwODg4rDiwODg4rDgANBQAAAAAAAAAAAAAAAA=="

media_player

- platform: universal
  name: Radio Studio
  commands:
    turn_on:
      service: script.studio_radio_on
    turn_off:
      service: script.studio_radio_off
    select_source:
      service: script.studio_radio_cambio_canale
  attributes:
    state: input_boolean.studio_radio_state
    source: input_select.studio_radio_elenco_canali
    source_list: input_select.studio_radio_elenco_canali|options

Can someone help me?
Thank you very much.

This should work for that

- platform: universal
  name: Radio Studio
  commands:
    turn_on:
      service: script.studio_radio_on
    turn_off:
      service: script.studio_radio_off
    select_source:
      service: script.turn_on
      data_template:
        entity_id: >
          {% if source ==  '1 Radio Deejay' %}
            script.studio_radio_canale_1
          {% elif source ==  '2 Radio Capital' %}
            script.studio_radio_canale_2
          {% elif source == '3 Radio Montecarlo' %}
           script.studio_radio_canale_3
          {% elif source == '4 Virgin Radio' %}
            script.studio_radio_canale_4
          {% elif source == '5 RDS' %}
            script.studio_radio_canale_5
          {% elif source == '6 Radio Nostalgia' %}
            script.studio_radio_canale_6
          {% elif source == '7 RAI Radio 1' %}
            script.studio_radio_canale_7
          {% elif source == '8 RAI Radio 2' %}
            script.studio_radio_canale_8
          {% elif source == '9 RAI Radio 3' %}
            script.studio_radio_canale_9
          {% else %} 
            script.studio_radio_off
          {% endif %}
  attributes:
    state: input_boolean.studio_radio_state
    source: input_select.studio_radio_elenco_canali
    source_list: input_select.studio_radio_elenco_canali|options
1 Like

One more thing, if you want to add stations in the future, you’ll have to do a lot of editing. To alleviate that issue, you could always deal with a different method than what you are currently doing.

If you are dead set on numbering your channels you could split your channel name based on space (' ') and pull the first item, which should be the number.

- platform: universal
  name: Radio Studio
  commands:
    turn_on:
      service: script.studio_radio_on
    turn_off:
      service: script.studio_radio_off
    select_source:
      service: script.turn_on
      data_template:
        entity_id: >
          {% if source in state_attr('input_select.studio_radio_elenco_canali', 'options') %}
            script.studio_radio_canale_{{ source.split(' ')[0] }}
          {% else %}
            script.studio_radio_off
          {% endif %}
  attributes:
    state: input_boolean.studio_radio_state
    source: input_select.studio_radio_elenco_canali
    source_list: input_select.studio_radio_elenco_canali|options

You’d just need to maintain a channel number and script number. To add a new channel, you’d just make a script and add the input option. As opposed to adding a script, adding the input option, and adding if statements where needed.

Thank you so much @petro
You saved me.

The first method you’ve suggested works great. In your code was missing only the item entity_id after data_template.
In the source of the media player remains selected the previous item, but I solved it by adding a call to input_select.select_option within each script of changing channel.

The second method, which is certainly more elegant and which I like a lot, unfortunately does not work.

Well i’m missing entity_id in both solutions. Second one only works if you have a number there. I’ll edit the other posts

Hi,

I am trying to do the same, but I am not very succesfull at it unfortunately.
What am I doing wrong in below code?
I have the sources, and I can select them, but then the scripts don’t get executed.

platform: universal
name: Living Room Media Center
children:
  - media_player.living_room_tv
  - media_player.living_room_kodi
commands:
  turn_on:
    service: script.living_tv_turn_on
  turn_off:
    service: script.living_tv_turn_off
  select_source:
    service: script.turn_on
    data_template:
      entity_id: media_player.select_source
        {% if source ==  'BBC One' %}
          script.tv_bbcone
        {% elif source ==  'BBC Two' %}
          script.tv_bbctwo
        {% endif %}
attributes:
  source: input_select.living_room_media_input|state
  source_list: input_select.living_room_media_input|options

This whole section is alittle off. Your problem here is that you’ve added an entity_id and then you try to give it a second entity. Also, the first entity_id you provide is not in the same domain as the service call. I.E. You are running the script.turn_on service with the entity_id of media_player.select_source. I think all you need to do is replace media_player.select_source with a carrot >.

  select_source:
    service: script.turn_on
    data_template:
      entity_id: >
        {% if source ==  'BBC One' %}
          script.tv_bbcone
        {% elif source ==  'BBC Two' %}
          script.tv_bbctwo
        {% endif %}

Shamless PR: https://github.com/elupus/hass_broadlink

how come this actually works? even with the automatic script templating, these individual scripts need to point to a source in the end…?

I’ve done it like shown below, and wonder if that could be improved upon using your technique?

  play_radio:
    alias: Play radio
    sequence:
      - service: media_player.volume_set
        data_template:
          entity_id: >
            {{states('sensor.media_player')}}
          volume_level: >
            {{ states('input_number.radio_volume')|float }}
      - service: media_player.play_media
        data_template:
          entity_id: >
            {{states('sensor.media_player')}}
          media_content_id: >
            {{states('sensor.radio_station')}}
          media_content_type: 'audio/mp4'
sensor:
  - platform: template
    sensors:
      media_player:
        friendly_name: Media player
        value_template: >
          {% set state = states('input_select.media_player') %}
          {% if state in ['Woonkamer','Hobbykamer','Hall', 'Master bedroom'] %} media_player.googlehome_{{(state)|lower|replace(' ','_')}}
          {% elif state == 'Gym' %} media_player.chromecastaudio_gym
          {% else %} group.broadcast
          {% endif %}

      radio_station:
        friendly_name: Radio station
        value_template: >
            {% set mapper = 
              { 'Radio 1':'http://icecast.omroep.nl/radio1-bb-mp3',
                'Radio 2':'http://icecast.omroep.nl/radio2-bb-mp3',
                '3FM':'http://icecast.omroep.nl/3fm-bb-mp3',
                'Radio 4':'http://icecast.omroep.nl/radio4-bb-mp3',
                'SLAM!':'http://stream.slam.nl/slam',
                'Radio 538':'http://playerservices.streamtheworld.com/api/livestream-redirect/RADIO538.mp3',
                'Q-Music':'http://icecast-qmusic.cdp.triple-it.nl/Qmusic_nl_live_96.mp3',
                'Veronica':'http://19993.live.streamtheworld.com/VERONICA_SC',
                'Sky Radio':'http://19983.live.streamtheworld.com/SKYRADIO.mp3',
                'Arrow Classic Rock':'https://stream.gal.io/arrow',
                'BNR Nieuwsradio':'http://icecast-bnr.cdp.triple-it.nl/bnr_mp3_96_03',
                '538 Dance Department':'http://playerservices.streamtheworld.com/api/livestream-redirect/TLPSTR01.mp3',
                'Ambi Nature Radio':'http://94.23.252.14:8067/stream',
                'Calm Radio - Sleep':'http://streams.calmradio.com/api/39/128/stream'} %}
            {% set state = states('input_select.radio_station') %}
            {{mapper[state]}}
input_select:
  radio_station:
    icon: mdi:radio
    name: 'Select Radio Station:'
    options:
      - 'Radio 1'
      - 'Radio 2'
      - '3FM'
      - 'Radio 4'
      - 'SLAM!'
      - 'Radio 538'
      - 'Q-Music'
      - 'Veronica'
      - 'Sky Radio'
      - 'Arrow Classic Rock'
      - 'BNR Nieuwsradio'
      - '538 Dance Department'
      - 'Ambi Nature Radio'
      - 'Calm Radio - Sleep'

moreover, what does the Universal player do, that I can’t do with these templates? Would the main difference be a play-button/switch?