Hi’
I’m currently changing quite a few of my Sonos templates, since the deprecation of the source_list
attribute and the birth of the sensor.sonos_favorites
instead
One set of templates are beyond my template knowledge though. I simply can’t get my head wrapped around it…
I’ve two templates that selects next or previous favourites from the previously indexed attribute list. Template include a circular loop - meaning skip to first source when at last source and vice versa. And with an extra twist that I define a start index no. to search from (excluding all my playlists from the ‘real’ source list (radio channels), as these are handled differently.
- id: hasp_sonos_prev_source
alias: hasp_sonos_prev_source
mode: single
trigger:
platform: mqtt
topic: hasp/+/state/p2b16
payload: '{"event":"down"}'
condition:
- condition: template
value_template: "{{ states('sensor.media_source') != 'no source' }}"
action:
service: media_player.select_source
data_template:
entity_id: "{{ state_attr('group.sonos_all', 'entity_id')[0] }}"
source: >-
{% set source_start = states('sensor.sonos_source_index_start') | int(0) %}
{% set entity_id = 'media_player.kokken' %}
{% set current = state_attr(entity_id, 'source') %}
{% set source_list = state_attr(state_attr('group.sonos_all', 'entity_id')[0],'source_list') %}
{% set index = source_list.index(current) %}
{% set prev = -1 if current == source_list[source_start] else index - 1 %}
{{ source_list[prev] }}
I have already done the template changes in my other for/endfor loops without issues. Got help looking at this issue
These are my test template/results in HA’s template engine:
{{ states('sensor.sonos_source_index_start') }}
[{{ state_attr('sensor.sonos_favorites','items').values() | join(", ") }}]
{% set source_start = 0 | int(0) %}
{% set entity_id = 'media_player.kokken' %}
{% set current = state_attr(entity_id, 'media_title') %}
{% set source_list = states.sensor.sonos_favorites.attributes.values() | list | join(', ') %}
{% set index = source_list.index(current) %}
{% set next = source_start if current == source_list[-1] else index + 1 %}
{{ source_list[next] }}
4
[*HT, *HT new, *New Music Friday Denmark, *The Ultimate Hit Mix, Classic FM, Classic Rock, DR Nyheder, DR P1, DR P3, DR P4 Fyn, DR P5, DR P6 Beat, Nova FM|, Radio 100|, Radio Soft|, Radio VLR, Retro Radio, Retro-Radio Millennium, Skala.fm, The Voice|]
R
So even it it’s appears to be an index, it’s not returning the expected source but an ‘R’ ?
Would really appreciate any help solving this, thanks.
//Henning