Needing help with script (noob guy)

So I’m doing my first steps with HA and I want to create a way to choose a radio station to play on my soundbar using TuneIn. I’ve got a rest service configured like this (using the station parameter):

rest_command:
  change_station:
    url: http://xx.xx.xx.xx:55001/CPM?cmd=giantCommand...{{ station }}restOfGiantCommand
    method: GET

If I execute this service with developer tools it works great.
Using some information I got from these forum I added this to configuration

input_select:
  radio_station:
    name: 'Select Radio Station:'
    options:
      - Option1
      - Option2

And this script

radio_script:
    alias: Play Radio on Soundbar
    sequence:
      -  service: rest_command.change_station
         data:
           station: >
            {% if is_state("input_select.radio_station", "Option1") %} 3
            {% elif is_state("input_select.radio_station", "Option2") %} 4
            {% endif %}

And this lovelace entity

entities:
  - entity: input_select.radio_station
  - entity: script.radio_script
show_header_toggle: false
title: Teste
type: entities

This doesn’t work. If i change the script to have a fixed value like bellow it will work but I can’t control the station

radio_script:
    alias: Play Radio on Soundbar
    sequence:
      -  service: rest_command.change_station
         data:
           station: 3

I’ve searched online but can’t find out what i’m doing wrong. Thanks

In radio_script, change:

         data:

to:

         data_template:

Thank you so much, it worked. I’ve been going in circles for a long time

What’s the difference between the 2 of them? In which situations do you use data instead of data_template?

1 Like

I think you’ve already discovered the difference empirically. The first one, data, doesn’t support templates whereas the second one, data_template, does.

  • If you use data with templates, the templates are not evaluated but used literally (which usually means things fail to work as expected).
  • In contrast, if you use data_template without templates, nothing bad happens and it behaves like data.
1 Like

Thank you so much for the help with the problem and improving my knowledge :smiley:

Big cheers

1 Like

not meaning to impose anything, but why did you choose these station options/names? Option1 = 3, and Option2 = 4 :wink:

why not use station names, makes it easier for you if the list grows, and so much better on the eye?

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'
      - 'Select'
    initial: Select

if you need to stream the stations based on the input_select, have a look at this:

      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',
                'Select':''} %}
            {% set state = states('input_select.radio_station') %}
            {{mapper[state]}}

Hi there @Mariusthvdb, thank you for your input. I’m not actually using Option1 and Option2 it was just to simplify the post since they’re my country’s radio stations and they might not be understandable in english :slight_smile:

I have Option1 = 3 (well actually it’s the station’s name and not Option1) because I can’t find a way to tell my Samsung soundbar to play a specific stream, so I used the multiroom app to save the stations as favorites and they use 0,1 and 2 as presets and 3 onward as favorites.

My current solution is using this custom_component https://github.com/macbury/ha_samsung_multi_room to control basic functions (on/off/volume/source) and I created another entity based on this Chromecast Radio with station and player selection to send API commands on the network based on information I found on this thread Samsung Multiroom speakers :smiley:

I would like to merge both and possibly send a pull request to the custom component repository if I can make it happen

cool :wink:

btw, Ive posted in that thread: Chromecast Radio with station and player selection so if you need more info on the setup, do have a look.

If we can further help you, just let us know :+1:

@Mariusthvdb Currently I have it setup like this:
soundbar

Next step is to find a way to join them into one card, since I don’t even have a chromecast I don’t think that topic is the right one to look for help right now, but you never know :wink:

I’m still starting to get around HA so I still have a lot to learn.