Loop through Media Player source list

Hi home’s,

I want to loop through the source list of my media player. With a button-click i would like to change the source.

My thoughts:

{{ states.media_player.wohnzimmer.attributes.source_list }}
gives me:
['1LIVE - Das junge Radio des WDR.', 'JAM FM New Music Radio', 'WDR 2 Bergisches Land']

To get the last state of the source list i have the following:

set lastItem = states.media_player.wohnzimmer.attributes.source_list[(states.media_player.wohnzimmer.attributes.source_list | length) - 1]

if source == lastItem and button clicked --> source = states.media_player.wohnzimmer.attributes.source_list[0]

to get to the beginning and first item when we reach the last item.

To make my script work i would need the sourceitem as a number.

@petro I hope you read this cuz you are the master of templates :slight_smile:

This takes the current selection, then gets the next one. You’ll have to place it inside a script if you want a button.

{%- set entity_id = 'media_player.wohnzimmer' %}
{%- set current = states(entity_id) %}
{%- 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] }}
1 Like

You are awesome, Thank you

The current variable is getting the playing or paused state. Is this as it should be?

The output in the template editor is:

Error rendering template: ‘paused’ is not in list

So i think… it looks for the state (paused or playing) in the source list. And there are just the 3 radio stations and not the paused or playing… While writing this i think it has to print the current source

edit: like this

{%- set entity_id = 'media_player.wohnzimmer' %}
{%- 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 %}
{{ options[next] }}

I get this in template editor:

Error rendering template: UndefinedError: ‘options’ is undefined

wrong variable, I edited the comment. Also, this should have nothing to do with the state playing or paused. This purely looks at the source list.

@Petro, how would you place this code in a script?

  alias: Sonos Next Favourite
  sequence:
    {%- set entity_id = 'media_player.sonos' %}
    {%- set current = states(entity_id) %}
    {%- 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] }}

You literally just place that in the script section as is. Depending on how you have your script section broken out, you may need the - sign in front of alias.

I must be missing something obvious here…

image
image

'1553339734942':
  alias: sonos next favourite
  {%- set entity_id = 'media_player.sonos' %}
  {%- set current = states(entity_id) %}
  {%- 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] }}

Missing the word sequence

  alias: Sonos Next Favourite
  sequence:
    {%- set entity_id = 'media_player.sonos' %}
    {%- set current = states(entity_id) %}
    {%- 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] }}

Thanks for getting back to me. I’m still getting the same error when I check my config

“Error loading /config/configuration.yaml: while scanning for the next token
found character ‘%’ that cannot start any token
in “/config/scripts.yaml”, line 81, column 6”

any other suggestions?

I’m braindead this morning. You are completely missing your action and service. YOu need to add that and place this template in the source field.

Take a look at your previous script. Notice it has a service with attributes and a data section?

EDIT:

  alias: Sonos Next Favourite
  sequence:
  - service: media_player.select_source
    data_template:
      entity_id: media_player.sonos
      source: >
        {%- set entity_id = 'media_player.sonos' %}
        {%- 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] }}
1 Like

it almost works…but it keeps trying to put the state “playing” as the source

Edited it, try it again

Weird…it doesn’t work but nothing shows up in the log files.

I’m wondering if this would be easier with a input_select with a list of the sources. I’m just not sure how to make the source = an input_select.

EDIT - Found the proper format on a different thread.

'1553339734862':
  alias: Sonos Source
  sequence:
  - service: media_player.select_source
    data_template:
      entity_id: media_player.sonos
      source: "{{  states('input_select.sonos_source') }}"

are you using the correct entity_id?

I am. i tried a different entity_id and nothing happened as well. It’s okay though i got the input_select changing the sources for now. Is there a way to trigger the next option on a input_select?

Hello.

Trying to get this to work also. No luck, script does not work for me either.

Were you able to solve this? I also want to change media player source with one button.

Try this:

bathroom_cycle_player_source:
  alias: Bathroom Cycle Player Source
  sequence:
  - service: media_player.select_source
    entity_id: media_player.bad
    data_template:
      source: >
        {%- set entity_id = 'media_player.bad' %}
        {%- 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] }}
2 Likes

bah looking back at my template, it’s missing data_template instead of data. Edited it

1 Like

Sorry to dig up an old thread - but how would you do this in node-RED?