Sonos favorites define list

Hi,

I am using the sonos integration and also integrated this with some KNX switches to start music without searching my mobile phone first.

It works very stable and I really like it however there’s one thing I am missing. I am pretty sure it’s possible to do this however I did not find a way yet.

I defined several playlists/radiostations in the Sonos app. To start it I use the media_player with the favorite_item_id.

My question is: Is there a possibility to setup a list of all favorites from sonos in a custom card. From there you can select one of the items as the “Prio 1 Favorite” which would play when pressing the switch.

This would be much easier than hard-coding the item id.

Thanks S

Check the Sonos custom card.

Many thanks. Did not see this before.

S

Hi,

I installed the custom card - guess you meant this one: GitHub - punxaphil/custom-sonos-card: Home Assistant custom lovelace sonos card

This is really helpful - thank you for the recommendation. However I did not find any setting that could solve my issue. I see that you can define/hardcode TopFavorites that appear than at the beginning of the list. You can also ignore some favorites.

What I want, is a setting to mark a specific item in the favorite list. This marked item is the TopFavorite than and will be played as a standard when pressing the KNX-Switches in the house.
This setting should help to change the TopFavorite for all users easily without hardcoding it.
Hoe this explains it a little better.

Thanks a lot

S

Yes you can, simply make an automation that gets triggered by the switch and have it play your favorite.

But a more complete approach that might help you achieve your goal :arrow_heading_down:
Based on this, you could just have your automation select your favorite from the list. That way your list also shows the favorite playing and you can easily change the fav.

The approach:

  1. Create a drop-down helper, say sonos_favorites
  2. Fill it with your favorites using a script or automation.
    Sonos favorites are stored in sensor.sonos_favorites. To put them in your helper you use service call input_select.set_options like below:
service: input_select.set_options
target:
  entity_id: input_select.sonos_favorites
data:
  options: "{{ state_attr('sensor.sonos_favorites', 'items').values() | list }}"
  1. Create an automation that triggers on a change of selection, I assume you know how, that picks and plays your fav. To pick the correct item of your Sonos favorites sensor this template might help.

    In this example I store the position of the favorite (that is selected in the helper of step 1) using the variable ‘FavIDX’ the position is the position in sensor.sonos_favorites.
    Next I use that position to return the ‘FV:x/yy’ value that is used to actually identify the favorite to play. This is stored in the varibable fav_id.
{% set FavIDX = (state_attr("sensor.sonos_favorites", "items").values() | list).index(states('input_select.sonos_favorites')) %}

{% set fav_id = (state_attr("sensor.sonos_favorites", "items")|list)[FavIDX] %}
  1. Now play the favorite`, this can be done using the following action
service: media_player.play_media
data:
  media_content_id: "{{ fav_id }}"
  media_content_type: favorite_item_id
target:
  entity_id: <your speaker>
continue_on_error: true

Of course one can put (and maybe should) steps 3 and 4 into one action that looks like this:

service: media_player.play_media
data:
  media_content_id: >-
    {{ (state_attr('sensor.sonos_favorites',
    'items')|list)[(state_attr('sensor.sonos_favorites', 'items').values() |
    list).index(states('input_select.sonos_favorites'))] }}
  media_content_type: favorite_item_id
target:
  entity_id: <your speaker>
continue_on_error: true

Disclaimer, I did not test it (yet) cause I had to build the first part (for this response) and the second part is a script that I use, but I am pretty sure it’s close if not spot on.
It works

alias: Sonos Favorite Select list
trigger:
  - platform: state
    entity_id:
      - input_select.sonos_favorites
condition: []
action:
  - variables:
      sonos_speaker: media_player.<your speaker>
  - service: media_player.play_media
    data:
      media_content_id: >-
        {{ (state_attr('sensor.sonos_favorites',
        'items')|list)[(state_attr('sensor.sonos_favorites', 'items').values() |
        list).index(states('input_select.sonos_favorites'))] }}
      media_content_type: favorite_item_id
    target:
      entity_id: "{{ sonos_speaker }}"
    continue_on_error: true
  - service: input_select.set_options
    target:
      entity_id: input_select.sonos_favorites
    data:
      options: "{{ state_attr('sensor.sonos_favorites', 'items').values()|list }}"
mode: single

At the end I update the favorites helper to make life easier.

1 Like

Wow ok :slight_smile: Thank you very much didn’t expect such a complete instruction.
I did not work through yet but wanted to say Thank you upfront.

I will let you know the result.

Have a great weekend

S

1 Like