Sonos should play a radio station

Folks,
on motion I would love to hear my favorite radio station. From time to time my sonos seams to forget the radio station and I have to go to my sonos app and choose the station. Now I would love to enhance my trigger From:

- alias: "Switch on BAD Media Player if Motion is detected during day"
  trigger:
     platform: state
     entity_id: sensor.vision_zp3111_multisensor_4in1_burglar_10_10
     from: '0'
     to: '8'
  condition:
     condition: time
     # At least one of the following is required.
     after: '8:00:00'
     before: '23:00:00'
  action:
    service: media_player.turn_on
    entity_id: media_player.bad

To:

- alias: "Switch on BAD Media Player if Motion is detected during day"
  trigger:
     platform: state
     entity_id: sensor.vision_zp3111_multisensor_4in1_burglar_10_10
     from: '0'
     to: '8'
  condition:
     condition: time
     # At least one of the following is required.
     after: '8:00:00'
     before: '23:00:00'
  action:
    service: media_player.turn_on
    entity_id: media_player.bad
    ##the radio station #

when I look to my logs after i switched on my Sonon in my bathroom:

Dec 28 19:45:40 home-assistant hass[11883]: 16-12-28 19:45:40 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: new_state=<state media_player.bad=playing; media_duration=0, supported_media_commands=10766, volume_level=0.18, media_content_type=music, friendly_name=Bad, media_album_name=, media_artist=, is_volume_muted=False, media_title=HITRADIO RT1 SÜDSCHWABEN, media_content_id=livestream.mp3, source=HITRADIO RT1 SÜDSCHWABEN, entity_picture=/api/media_player_proxy/media_player.bad?token=1848527760&cache=2936b, source_list=[‘ANTENNE BAYERN’, ‘B5 plus’, ‘Bayern 3’, ‘DASDING’, ‘HITRADIO RT1 SÜDSCHWABEN’, ‘LORA München’, ‘Meine Shazam-Titel’, ‘Top10’] @ 2016-12-28T20:45:33.793157+01:00>, old_state=<state media_player.bad=playing; media_duration=0, supported_media_commands=10766, volume_level=0.18, media_content_type=music, friendly_name=Bad, media_artist=, is_volume_muted=False, media_title=HITRADIO RT1 SÜDSCHWABEN, media_content_id=x-sonosapi-stream:s57038?sid=254&flags=8224&sn=0, source=HITRADIO RT1 SÜDSCHWABEN, entity_picture=/api/media_player_proxy/media_player.bad?token=1848527760&cache=2936b, source_list=[‘ANTENNE BAYERN’, ‘B5 plus’, ‘Bayern 3’, ‘DASDING’, ‘HITRADIO RT1 SÜDSCHWABEN’, ‘LORA München’, ‘Meine Shazam-Titel’, ‘Top10’] @ 2016-12-28T20:45:33.793157+01:00>, entity_id=media_player.bad>

The state of my media Player shows:
media_duration: 0
supported_media_commands: 10766
volume_level: 0.18
media_content_type: music
friendly_name: Bad
media_album_name:
media_artist:
is_volume_muted: false
media_title: HITRADIO RT1 SÜDSCHWABEN
media_content_id: livestream.mp3
source: HITRADIO RT1 SÜDSCHWABEN
entity_picture: /api/media_player_proxy/media_player.bad?token=1848527760&cache=2936b
source_list: ANTENNE BAYERN,B5 plus,Bayern 3,DASDING,HITRADIO RT1 SÜDSCHWABEN,LORA München,Meine Shazam-Titel,Top10

The “HITRADIO RT1 SÜDSCHWABEN’” is the channel I wanna use. How can I tell HA always play that station? Can you help?

Look at my script which is activated by an alarm trigger. You can see how I tell the sonos player to play a specific radio station livestream - in this case Hitradio Ö3 or Antenne Vorarlberg.

sonos_radiosender_spielen:
  alias: Radiosender abspielen
  sequence:
    - service: media_player.volume_set
      data_template:
        entity_id: >
          {% if is_state("input_select.sonos_player", "KĂźche") %}
            media_player.kuche
          {%-elif is_state("input_select.sonos_player", "Wohnzimmer") %}
            media_player.wohnzimmer
          {% else %}
            none
          {% endif %}
        volume_level: '{{ states.input_slider.sonos_lautstaerke.state | multiply(0.01) }}'
    - service: media_player.play_media
      data_template:
        entity_id: >
          {% if is_state("input_select.sonos_player", "KĂźche") %}
            media_player.kuche
          {%-elif is_state("input_select.sonos_player", "Wohnzimmer") %}
            media_player.wohnzimmer
          {% else %}
            none
          {% endif %}
        media_content_id: >
          {% if is_state("input_select.sonos_radiosender", "Ö3 Hitradio") %}
            x-rincon-mp3radio://mp3stream7.apasf.apa.at:8000
          {%-elif is_state("input_select.sonos_radiosender", "Antenne Vorarlberg") %}
            x-rincon-mp3radio://webradio.antennevorarlberg.at:80/live
          {% else %}
            none
          {% endif %}
        media_content_type: 'radio'
    - service: notify.gidea
      data:
        message: "{{ states('input_select.sonos_radiosender') }} wird in {{ states('input_select.sonos_player') }} abgespielt!"

To get the media_content_id check the ip of your sonso and open http://[ip_of_your_sonos_player]:1400/support/review. Under Radio Station Log you can usually see the media_conten_id - after the word ‘stream’. If not, first select and play the radio station with your sonos controller.

Hope this help.

3 Likes

@fgabriel
Can you show me the “input_select.sonos_player” anf the “input_slider.sonos_lautstaerke” yaml code?

@Juergen: Sure. Please consider that I splited the controls into separate yaml-files.

Frist the two input_selects to choose the radio station and the sonos player:

sonos_radiosender:
  name: Radiosender
  options:
    - Ö3 Hitradio
    - Antenne Vorarlberg
    - None
  initial: Ö3 Hitradio
  icon: mdi:radio

sonos_player:
  name: Sonosplayer
  options:
    - KĂźche
    - Wohnzimmer
    - None
  initial: KĂźche
  icon: mdi:airplay

Then the input_slider to control the volume of the sonos player:

sonos_lautstaerke:
  name: Lautstärke
  initial: 6
  min: 1
  max: 20
  step: 1

The input_sliders to set the time when to play

alarmhour:
  name: Stunde
  icon: mdi:timer
  initial: 6
  min: 0
  max: 23
  step: 1
alarmminutes:
  name: Minute
  icon: mdi:timer
  initial: 30
  min: 0
  max: 59
  step: 1

A template sensor to display the selected time as HH:MM and to use in the trigger section of the automation:

- platform: template
  sensors:
    alarm_time:
      friendly_name: 'Zeit'
      value_template: '{% if states.input_slider.alarmhour.state | length == 3 %}0{% endif %}{{ states.input_slider.alarmhour.state | int }}:{% if states.input_slider.alarmminutes.state | length == 3 %}0{% endif %}{{ states.input_slider.alarmminutes.state | int }}'

And a group to put them all together:

alarmclock:
  name: Aufwachen
  entities:
    - sensor.alarm_time
    - input_slider.alarmhour
    - input_slider.alarmminutes
    - input_boolean.alarmstatus
    - input_boolean.alarmweekday
    - input_select.sonos_player
    - input_select.sonos_radiosender
    - input_slider.sonos_lautstaerke

And finally the automation to trigger the script I already cited:

- alias: 'Aufwachen'
  trigger:
    platform: template
    value_template: '{{ states.sensor.time.state == states.sensor.alarm_time.state }}'
  condition:
    condition: or
    conditions:
      - condition: and
        conditions:
          - condition: state
            entity_id: input_boolean.alarmstatus
            state: 'on'
          - condition: state
            entity_id: input_boolean.alarmweekday
            state: 'on'
          - condition: time
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
          - condition: state
            entity_id: sensor.holiday_sensor
            state: 'off'
      - condition: and
        conditions:
          - condition: state
            entity_id: input_boolean.alarmstatus
            state: 'on'
          - condition: state
            entity_id: input_boolean.alarmweekday
            state: 'off'
  action:
    service: homeassistant.turn_on
    entity_id: script.sonos_radiosender_spielen

The sensor.holiday_sensor is a sensor I wrote myself. I had no time to make a pull request. So it is stored in my custom_components directory.

That’s it. :slight_smile:

1 Like

@fgabriel
what do you mean with “The sensor.holiday_sensor is a sensor I wrote myself. I had no time to make a pull request”. I mean with the pull request???