Random radios to play

I have this script that turns on radio every morning:

accendi_radio_mattina:
  alias: Accendi Radio Mattina
  sequence:
    - service: switch.turn_on
      entity_id: switch.ampli_on
    - delay: 00:00:01
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.salotto_audio
        volume_level: 0.5
    - service: input_number.set_value
      data_template:
        entity_id: input_number.radio_volume
        value: 0.5
    - service: input_boolean.turn_on
      entity_id: input_boolean.radio_play
    - service: media_player.play_media
      data_template:
        entity_id: media_player.salotto_audio
        media_content_id: "http://broadcast.infomaniak.net/tsfjazz-high.mp3"
        media_content_type: 'audio/mp4'

But i have several radio station configured in another script. How to change this script to turn on randomly radios configured in another script?

The list of configured radios is the following:

media_content_id: >
  {% if is_state("input_select.radio_station", "Q-Music") %} http://icecast-qmusic.cdp.triple-it.nl/Qmusic_nl_live_96.mp3
  {% elif is_state("input_select.radio_station", "3FM") %} http://icecast.omroep.nl/3fm-bb-mp3
  {% elif is_state("input_select.radio_station", "100% NL") %} http://stream.100p.nl/100pctnl.mp3
  {% elif is_state("input_select.radio_station", "Sky Radio") %} http://19993.live.streamtheworld.com:80/SKYRADIO_SC6
  {% elif is_state("input_select.radio_station", "Arrow Classic Rock") %} http://91.221.151.155/listen.mp3
  {% elif is_state("input_select.radio_station", "Classic FM") %} http://19143.live.streamtheworld.com/CLASSICFM_SC
  {% elif is_state("input_select.radio_station", "BNR Nieuwsradio") %} http://icecast-bnr.cdp.triple-it.nl/bnr_mp3_96_03
  {% elif is_state("input_select.radio_station", "Hot 108") %} http://jbmedia-edge1.cdnstream.com:80/hot108
  {% elif is_state("input_select.radio_station", "The Beat") %} http://live.leanstream.co/CKBTFM-MP3
  {% elif is_state("input_select.radio_station", "Alt Rock") %} http://149.56.23.7:20254/stream
  {% elif is_state("input_select.radio_station", "Hit Remixes") %} http://149.56.23.7:20252/stream
  {% elif is_state("input_select.radio_station", "Play Radio") %} http://176.9.31.78:8222
  {% elif is_state("input_select.radio_station", "Elium-Rock") %} http://listen.radionomy.com/Elium-Rock
  {% elif is_state("input_select.radio_station", "Classic Rock Florida") %} http://listen.shoutcast.com/Classic-RockFlorida
  {% elif is_state("input_select.radio_station", "181FM Power (Todays Hits)") %} http://listen.181fm.com/181-power_128k.mp3?
  {% elif is_state("input_select.radio_station", "181FM 90's Dance") %} http://listen.181fm.com/181-90sdance_128k.mp3
  {% elif is_state("input_select.radio_station", "181FM Star 90's") %} http://listen.181fm.com/181-star90s_128k.mp3
  {% elif is_state("input_select.radio_station", "181FM The Breeze") %} http://listen.181fm.com/181-breeze_128k.mp3
  {% elif is_state("input_select.radio_station", "Heat Radio (RnB)") %} http://174.37.159.206:8106/stream
  {% elif is_state("input_select.radio_station", "Fresh 92-7") %} http://live.fresh927.com.au/freshmp3
  {% elif is_state("input_select.radio_station", "DI Chill & Tropical House") %} http://pub1.diforfree.org:8000/di_chillntropicalhouse_hi
  {% elif is_state("input_select.radio_station", "DI Disco House") %} http://pub1.diforfree.org:8000/di_discohouse_hi
  {% elif is_state("input_select.radio_station", "DI Liquid D&B") %} http://pub1.diforfree.org:8000/di_liquiddnb_hi
  {% elif is_state("input_select.radio_station", "Jazz Blues") %} http://icepe5.infomaniak.ch/jazzblues-high.mp3
  {% elif is_state("input_select.radio_station", "TSF Jazz") %} http://broadcast.infomaniak.net/tsfjazz-high.mp3
  {% elif is_state("input_select.radio_station", "Jazz FM") %} http://tx.sharp-stream.com/icecast.php?i=jazzfmmobile.mp3
  {% elif is_state("input_select.radio_station", "ABC Classic 2") %} http://live-radio02.mediahubaustralia.com/FM2W/mp3/
  {% elif is_state("input_select.radio_station", "Boston Baroque Radio") %} http://peridot.streamguys.com:7010/bblive-sgplayer
  {% elif is_state("input_select.radio_station", "Radio1") %} http://icestreaming.rai.it/1.mp3
  {% elif is_state("input_select.radio_station", "Radio2") %} http://icestreaming.rai.it/2.mp3
  {% elif is_state("input_select.radio_station", "Radio3") %} http://icestreaming.rai.it/3.mp3
  {% elif is_state("input_select.radio_station", "GrParlamento") %} http://icestreaming.rai.it/7.mp3
  {% elif is_state("input_select.radio_station", "Radio24") %} http://shoutcast.radio24.it:8000/listen.pls
  {% elif is_state("input_select.radio_station", "Deejay") %} http://radiodeejay-lh.akamaihd.net/i/RadioDeejay_Live_1@189857/master.m3u8
  {% elif is_state("input_select.radio_station", "Virgin") %} http://icecast.unitedradio.it/Virgin.mp3
  {% elif is_state("input_select.radio_station", "Capital") %} http://radiocapital-lh.akamaihd.net/i/RadioCapital_Live_1@196312/master.m3u8
  {% elif is_state("input_select.radio_station", "Radio 105") %} http://icecast.105.net/105.mp3
  {% elif is_state("input_select.radio_station", "Radio Italia") %} http://sr9.inmystream.info:8006
  {% elif is_state("input_select.radio_station", "Radio Sportiva") %} http://nr5.newradio.it:8545/stream.mp3
  {% elif is_state("input_select.radio_station", "R101") %} http://icecast.unitedradio.it/r101
  {% elif is_state("input_select.radio_station", "RDS") %} http://stream1.rds.it:8000/rds64k
  {% elif is_state("input_select.radio_station", "Subasio") %} http://onair18.xdevel.com:8152
  {% elif is_state("input_select.radio_station", "Radio Montecarlo") %} http://edge.radiomontecarlo.net/RMC.mp3
  {% elif is_state("input_select.radio_station", "Radio Montecarlo 2") %} http://edge.radiomontecarlo.net/MC2.mp3
  {% endif %}
media_content_type: 'audio/mp4'

you need to get a random number in your stations range
You can either get that number at the beginning of your automation using range(1,45) | random

or you can have one that is based on the random platform that you can refer to at any time:

- platform: random
  name: random_station_number
  minimum: 1
  maximum: 44
  scan_interval: 300

Then you could go through your list of radio station checking the random number.
Say you got for the 2nd option, it would look like that:

media_content_id: >
  {% if is_state('sensor.random_station_number','1') %} http://icecast-qmusic.cdp.triple-it.nl/Qmusic_nl_live_96.mp3
  {% elif is_state('sensor.random_station_number','2') %} http://icecast.omroep.nl/3fm-bb-mp3
  {% elif is_state('sensor.random_station_number','3') %} http://stream.100p.nl/100pctnl.mp3
  {% elif is_state('sensor.random_station_number','4') %} http://19993.live.streamtheworld.com:80/SKYRADIO_SC6
  {% elif is_state('sensor.random_station_number','5') %} http://91.221.151.155/listen.mp3
  {% elif is_state('sensor.random_station_number','6') %} http://19143.live.streamtheworld.com/CLASSICFM_SC
  {% elif is_state('sensor.random_station_number','7') %} http://icecast-bnr.cdp.triple-it.nl/bnr_mp3_96_03
  {% elif is_state('sensor.random_station_number','8') %} http://jbmedia-edge1.cdnstream.com:80/hot108
  {% elif is_state('sensor.random_station_number','9') %} http://live.leanstream.co/CKBTFM-MP3
  {% elif is_state('sensor.random_station_number','10') %} http://149.56.23.7:20254/stream
  {% elif is_state('sensor.random_station_number','11') %} http://149.56.23.7:20252/stream
  {% elif is_state('sensor.random_station_number','12') %} http://176.9.31.78:8222
  {% elif is_state('sensor.random_station_number','13') %} http://listen.radionomy.com/Elium-Rock
  {% elif is_state('sensor.random_station_number','14') %} http://listen.shoutcast.com/Classic-RockFlorida
  {% elif is_state('sensor.random_station_number','15') %} http://listen.181fm.com/181-power_128k.mp3?
  {% elif is_state('sensor.random_station_number','16') %} http://listen.181fm.com/181-90sdance_128k.mp3
  {% elif is_state('sensor.random_station_number','17') %} http://listen.181fm.com/181-star90s_128k.mp3
  {% elif is_state('sensor.random_station_number','18') %} http://listen.181fm.com/181-breeze_128k.mp3
  {% elif is_state('sensor.random_station_number','19') %} http://174.37.159.206:8106/stream
  {% elif is_state('sensor.random_station_number','20') %} http://live.fresh927.com.au/freshmp3
  {% elif is_state('sensor.random_station_number','21') %} http://pub1.diforfree.org:8000/di_chillntropicalhouse_hi
  {% elif is_state('sensor.random_station_number','22') %} http://pub1.diforfree.org:8000/di_discohouse_hi
  {% elif is_state('sensor.random_station_number','23') %} http://pub1.diforfree.org:8000/di_liquiddnb_hi
  {% elif is_state('sensor.random_station_number','24') %} http://icepe5.infomaniak.ch/jazzblues-high.mp3
  {% elif is_state('sensor.random_station_number','25') %} http://broadcast.infomaniak.net/tsfjazz-high.mp3
  {% elif is_state('sensor.random_station_number','26') %} http://tx.sharp-stream.com/icecast.php?i=jazzfmmobile.mp3
  {% elif is_state('sensor.random_station_number','27') %} http://live-radio02.mediahubaustralia.com/FM2W/mp3/
  {% elif is_state('sensor.random_station_number','28') %} http://peridot.streamguys.com:7010/bblive-sgplayer
  {% elif is_state('sensor.random_station_number','29') %} http://icestreaming.rai.it/1.mp3
  {% elif is_state('sensor.random_station_number','30') %} http://icestreaming.rai.it/2.mp3
  {% elif is_state('sensor.random_station_number','31') %} http://icestreaming.rai.it/3.mp3
  {% elif is_state('sensor.random_station_number','32') %} http://icestreaming.rai.it/7.mp3
  {% elif is_state('sensor.random_station_number','33') %} http://shoutcast.radio24.it:8000/listen.pls
  {% elif is_state('sensor.random_station_number','34') %} http://radiodeejay-lh.akamaihd.net/i/RadioDeejay_Live_1@189857/master.m3u8
  {% elif is_state('sensor.random_station_number','35') %} http://icecast.unitedradio.it/Virgin.mp3
  {% elif is_state('sensor.random_station_number','36') %} http://radiocapital-lh.akamaihd.net/i/RadioCapital_Live_1@196312/master.m3u8
  {% elif is_state('sensor.random_station_number','37') %} http://icecast.105.net/105.mp3
  {% elif is_state('sensor.random_station_number','38') %} http://sr9.inmystream.info:8006
  {% elif is_state('sensor.random_station_number','39') %} http://nr5.newradio.it:8545/stream.mp3
  {% elif is_state('sensor.random_station_number','40') %} http://icecast.unitedradio.it/r101
  {% elif is_state('sensor.random_station_number','41') %} http://stream1.rds.it:8000/rds64k
  {% elif is_state('sensor.random_station_number','42') %} http://onair18.xdevel.com:8152
  {% elif is_state('sensor.random_station_number','43') %} http://edge.radiomontecarlo.net/RMC.mp3
  {% elif is_state('sensor.random_station_number','44') %} http://edge.radiomontecarlo.net/MC2.mp3
  {% endif %
media_content_type: 'audio/mp4'

I tried in this way:

- service: media_player.play_media
  data_template:
    entity_id: media_player.salotto_audio
    media_content_id: >
      {{ ["http://listen.181fm.com/181-power_128k.mp3?", "http://radio-th.com:8002/;", "http://icecast.unitedradio.it/Virgin.mp3", "http://91.221.151.155/listen.mp3", "http://listen.shoutcast.com/Classic-RockFlorida", "http://broadcast.infomaniak.net/tsfjazz-high.mp3", "http://tx.sharp-stream.com/icecast.php?i=jazzfmmobile.mp3", "http://peridot.streamguys.com:7010/bblive-sgplayer", "http://stream1.rds.it:8000/rds64k", "http://edge.radiomontecarlo.net/MC2.mp3"] | random }}        
    media_content_type: 'audio/mp4'

Is it working the same as yours?

Oh that’s way more elegant than my solution :+1:

Ok thanks… Is a better way to indent my solution, to avoid that so long line?

May i ask another thing?
Well i have the tts.google_say service that tells me the name of the radio station playing, but if i use a random choice, how to tell tts.google_say which is the name of the randomly chosen station?

since you used the > in media_content_id: > you can spread it over several lines without issues

Can you give me an example on how to divide and indent those lines?

ah if you want it this way you probably need an automation/script that will set your input_select to a random station, so kinda back to my first “inelegant” solution :smiley:

- service: media_player.play_media
  data_template:
    entity_id: media_player.salotto_audio
    media_content_id: >
      {{ ["http://listen.181fm.com/181-power_128k.mp3?",
      "http://radio-th.com:8002/;",
      "http://icecast.unitedradio.it/Virgin.mp3",
      "http://91.221.151.155/listen.mp3",
      "http://listen.shoutcast.com/Classic-RockFlorida",
      "http://broadcast.infomaniak.net/tsfjazz-high.mp3",
      "http://tx.sharp-stream.com/icecast.php?i=jazzfmmobile.mp3",
      "http://peridot.streamguys.com:7010/bblive-sgplayer",
      "http://stream1.rds.it:8000/rds64k",
      "http://edge.radiomontecarlo.net/MC2.mp3"] | random }}        
    media_content_type: 'audio/mp4'

That’s good… :ok_hand: