Loop through Media Player source list

Hope it’s ok to revive this…

I’m trying to accomplish the exact same thing. I grabbed the code above, and I think I did it right, but I get an error… I want to be able to cycle through my Sonos sources (favorites) with a button. An automation is calling this.

bathroom_source:
  alias: Sonos Next Favorite
  sequence:
    - service: media_player.select_source
      data:
        entity_id: media_player.bathroom
        source: >
          {%- set entity_id = 'media_player.bathroom' %}
          {%- set current = state_attr(entity_id, 'source') %}
          {%- set source_list = state_attr(entity_id, 'source_list') %}
          {%- set index = source_list.index(current) %}
          {%- set next = 0 if current == source_list[-1] else index + 1 %}
          {{ source_list[next] }}

The error:

2021-01-08 02:22:04 ERROR (MainThread) [homeassistant.components.script.bathroom_source] Sonos Next Favourite: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: ValueError: None is not in list

Thanks!

That should be working. I don’t see anything wrong in the code. Can you take a screenshot of media_player.bathroom inside the developer tools -> states page?

Sorry for the delayed response. I didn’t get a notification. Thanks for your reply!

The attribute “source” only has a value in some cases - e.g. if the source is a Sonos Radio station. If the Sonos favorite is e.g. a (Spotify) playlist, than “source” is not set, hence the “None” error. In case of a playlist, the attribute “media_playlist” is set to the name of the playlist and is identical to that of the source list entry. There are also other types of source list entries which are neither playlists nor radio stations - e.g. the “Artists” entry of the Sonos Library. This entry would not be be playable in the first place. The combinations of these scenarios will make tricky to cycle through the source list.

There’s been some changes recently to the Sonos HA integration. So some attributes have changed or been revised.

Anyway, source attribute is only present when playing radio stations (eg. TuneIn or similar)
When on playlists, source attrbute is omitted and you have to test for that scenario, otherwise the template will fail. Preferrably before doing the sequence at all, as I expect you don’t want to skip to a radio station when a playlist is currently running ? But you could do a test in the template that eg. chose first source from source_list if source attribute is not present.

{%- set entity_id = 'media_player.bad' %}
{%- set current = state_attr(entity_id, 'source') if state_attr('media_player.bad', 'source') != none else state_attr('media_player.bad', 'source_list')[0] %}
{%- set source_list = state_attr(entity_id, 'source_list') %}
{%- set index = source_list.index(current) %}
{%- set next = 0 if current == source_list[-1] else index + 1 %}
{{ source_list[next] }}

Ciao !