Dynamic Source Selection for Sonos

Hi All,

I’ve been trying to set up an automation to dynamically select_source for my Sonos system. I’m trying to use an input_select to do this. It seems that I can’t get the source field to work when there are “if” conditions. The automation seems to work if I input a static source.

Here’s a sample of my input_select code in configuration.yaml:

input_select:
  sonos_source:
    name: Sonos Source
    options:
      - alternative
      - club
      - Japanese
      - 98.0
      - 98.7
      - 91.3
      - kiss 92
      - Line-in
      - TV
    initial: TV
    icon: mdi:radio

and here is a sample of my automation code from my automations.yaml

- alias: sonos_music
  trigger:
    platform: state
    entity_id: input_select.sonos_source
  action:
    - service: media_player.select_source
      data_template: 
        entity_id: media_player.sonos_bedroom
        source: >
          {% if is_state("input_select.sonos_source", "alternative") -%}
            "Alternative.m3u"
          {%- elif is_state("input_select.sonos_source", "club") -%}
            "Club.m3u"
          {%- elif is_state("input_select.sonos_source", "Japanese") -%}
            "Japanese.m3u"
          {%- elif is_state("input_select.sonos_source", "98.0") -%}
            "Power 98 FM 98.0 (Top 40/Pop)"
          {%- elif is_state("input_select.sonos_source", "98.7") -%}
            "987 98.7 (Top 40/Pop)"
          {%- elif is_state("input_select.sonos_source", "kiss 92") -%}
            "Kiss92 92.0 (Adult Contemporary)"
          {%- elif is_state("input_select.sonos_source", "91.3") -%}
            "One FM 91.3 (Rock)"
          {%- else -%}
            "Line-in"
          {%- endif %}

Does anyone know if this is a bug or how to get it working as show at the bottom of the link below?

https://home-assistant.io/cookbook/perform_actions_based_on_input_select/

Thanks!

- id: select_and_show_living_room_sonos
  trigger:
  - entity_id: input_select.dynamic_living_room_sonos
    platform: state
  action:
  - data_template:
      source: "{{ trigger.to_state.state }}"
    entity_id: media_player.living_room
    service: media_player.select_source
  - data:
      visible: true
    entity_id: group.living_room_sonos_media_card
    service: group.set_visibility
  alias: Select & Show Living Room Sonos
```
1 Like

Thanks for the suggestion. I don’t think that would work in my case as my input_select state is slightly different from the actual source name in my sonos system.

I’m actually using IFTTT to change the state for my input_select, so it’s unlikely that I can get the input_select state to match the actual sonos source. I think something might be wrong with my syntax for all the “if” rules…?

Thanks for sharing this! Is there a way of specify that the Sonos speaker should shuffle a Spotify playlist? The shuffle service is only available for Spotify media player.