Using sonos favourites sensor

I am trying to use the new sonos sensor about favourites and created this script to play randomly favourites, but i get no success…

sonos_random:
  alias: Play Sonos Favourites
  sequence:
    - service: media_player.play_media
      data:
        media_content_id: '{{ state_attr("sensor.sonos_favorites", "items") | list | random }}'       
        media_content_type: audio/mp4
      target:        
        entity_id: media_player.sonos
  mode: single
  icon: mdi:playlist-music

Can anybody help me to trace where is the error?

The items attribute contains a dictionary (not a list) so if you want all the dictionary’s values, do this:

        media_content_id: '{{ state_attr("sensor.sonos_favorites", "items").values() | list | random }}'       

Thanks, but after changing the template it still doesn’t work…

It works for me.

Did you enable sensor.sonos_favorites? It’s disabled by default.

yes, of course… are you using the same script? Or, did you tried using mine?

Checking the script inside the developers tool/models i get this:

alias: Play Sonos Favourites
  sequence:
    - service: media_player.play_media
      data:
        media_content_id: 'Three Friends (2011 Remaster)'
        media_content_type: audio/mp4
      target:        
        entity_id: media_player.studio

That seems correct, but running the script i get no music…

The template works for me.

I assumed your service call was correct but after testing it, it failed with errors. Check your log and you’ll see what I mean.

I see no error in my log about this service call…
What it says in your try?

BTW i solved creating a list of my favorites and put inside the script the names in place of the template and now it’s working…

I use the media_player.select_source service call to play Sonos favorites (it’s part of a script I have that handles Alexa commands like “Alexa, play Sonos Jazz Canada”).

Here’s how that service call would be used to play a random Sonos favorite.

    - service: media_player.select_source
      target:
        entity_id: media_player.sonos
      data:
        source: "{{ state_attr('sensor.sonos_favorites', 'items').values() | list | random }}"
1 Like

Hi Taras,

is it possible to distinguish between playlist and radio stations in the favorites?
I would like to play the favorites randomly but only my playlist, the radio stations I want to exclude.

Greetings
Moss

I don’t know the answer; I don’t have any playlists in my Favorites.

Ok anyway thanks for your quick reply

Have a nice weekend