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
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] }}
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
@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.
'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] }}
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”
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] }}
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?
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] }}