Output Sonos track data

Hi all.

I got this little smart display and followed the tutorial and got the code from Smart Home Junkie -

Its excellent. Great code and tutorial.

But I am a bit of a newbie -

Could anyone point me in the right direction to be able to code to display/output the ‘now playing’ artist and track name from a Sonos speaker onto the screen using esphome?

Many many thanks

Each media player is a little different in the metadata it can show. However, if you pay a track on you Sonos, then look in the states section of the developer tools. There will be a whole lot of attributes, like this sort of thing

	group_members: 
volume_level: 0.3
is_volume_muted: false
media_content_id: {"index": 8, "urls": [{"url": "file:///music/John_Mayall_&_The_Bluesbreakers-Blues_Breakers_With_Eric_Clapton/01.All_Your_Love.mp3"}, {"url": "file:///music/John_Mayall_&_The_Bluesbreakers-Blues_Breakers_With_Eric_Clapton/02.Hideaway.mp3"}, {"url": "file:///music/John_Mayall_&_The_Bluesbreakers-Blues_Breakers_With_Eric_Clapton/03.Little_Girl.mp3"}, {"url": "file:///music/John_Mayall_&_The_Bluesbreakers-Blues_Breakers_With_Eric_Clapton/04.Another_Man.mp3"}, {"url": "file:///music/John_Mayall_&_The_Bluesbreakers-Blues_Breakers_With_Eric_Clapton/05.Double_Crossing_Time.mp3"}, {"url": "file:///music/John_Mayall_&_The_Bluesbreakers-Blues_Breakers_With_Eric_Clapton/06.Whatd_I_Say.mp3"}, {"url": "file:///music/John_Mayall_&_The_Bluesbreakers-Blues_Breakers_With_Eric_Clapton/07.Key_To_Love.mp3"}, {"url": "file:///music/John_Mayall_&_The_Bluesbreakers-Blues_Breakers_With_Eric_Clapton/08.Parchman_Farm.mp3"}, {"url": "file:///music/John_Mayall_&_The_Bluesbreakers-Blues_Breakers_With_Eric_Clapton/09.Have_You_Heard.mp3"}, {"url": "file:///music/John_Mayall_&_The_Bluesbreakers-Blues_Breakers_With_Eric_Clapton/10.Ramblin_On_My_Mind.mp3"}, {"url": "file:///music/John_Mayall_&_The_Bluesbreakers-Blues_Breakers_With_Eric_Clapton/11.Steppin_Out.mp3"}, {"url": "file:///music/John_Mayall_&_The_Bluesbreakers-Blues_Breakers_With_Eric_Clapton/12.It_Aint_Right.mp3"}]}
media_content_type: playlist
media_duration: 357
media_position: 178
media_position_updated_at: 2024-07-25T05:21:14.576932+00:00
media_title: Have You Heard
media_artist: John Mayall & The Bluesbreakers
media_album_name: Blues Breakers With Eric Clapton
shuffle: false
repeat: off
query_result: 
{}

RIP John Mayall!!!

This is not for a Sonos, but you should be able to find what you want this way.

thanks! so i managed that… sorry to be simple, but how do i access the artist / track info so i can output it onto a screen?

group_members:
  - media_player.sonos_one_sl
  - media_player.sonos_one_sl_2
volume_level: 0.01
is_volume_muted: false
media_content_id: >-
  x-sonosapi-hls-static:ALkSOiFReKIM7hugV5R27QWo6kgF93001tW-tkbkIj52MYEg?sid=284&flags=8&sn=14
media_content_type: music
media_duration: 195
media_position: 21
media_position_updated_at: "2024-07-25T17:39:58.977883+00:00"
media_title: We Never Die (Acoustic)
media_artist: Juke Ross
media_album_name: We Never Die (Acoustic)
shuffle: false
repeat: "off"
queue_position: 1
queue_size: 100
device_class: speaker
entity_picture: >-
  /api/media_player_proxy/media_player.sonos_one_sl?token=35a996a5aae16ac19936b66717a57076e53784e28b067d26ceb5f0f81d232d61&cache=c03bef7fac013d17
friendly_name: Kitchen
supported_features: 4127295

sensor:
  - platform: template
    sensors:
      sonos_current_artist:
        value_template: "{{ states('media_player.sonos_one_sl_2').attributes.media_artist }}"
      sonos_current_album:
        value_template: "{{ states('media_player.sonos_one_sl_2').attributes.media_album_name }}"

i tried this but just get ‘Unavailable’ as an output

In esphome attributes can be accessed via the homeassistant sensor.