I’m no HA expert. Maybe upper-beginner or lower-mid. I’ve been working with Gemini for a couple of evenings trying to come up with an automation that I want. But I’ve come to the determination that the root of my question/problem is this… Is it possible to identify what Favorite is currently playing? Seems like a no-brainer, but it doesn’t seem to exist. Anyone know how?
Sonos favourites have ID numbers in the format FV:2/31
There’s a section about it in the docs.
Thanks. Yes, I’m aware HA can tell Sonos to play a source given the Favorite ID and that the Favorites ID is in the format FV:2/31. The Favorite ID is what I am trying to ascertain from whatever is currently playing. Sorry if I didn’t explain that clearly. Say I start playing Favorite “Yacht Rock” from the Sonos app on my phone. It is FV:2/44. I see no way for HA to give me the data that FV:2/44 was selected or is playing. This is what I need for my project.
The media player entity has an attribute showing what file is currently playing:
media_content_id: >-
x-file-cifs://192.168.1.141/music/General/Alan%20Price/O_Lucky_Man!_(Reissue)/01_-_O_Lucky_Man!_(LP_Version).mp3
Is that any help?
Unfortunately, no. But thanks for trying to help.
When I play a selection from Favorites, the chosen media_player acquires a media_channel attribute. This template reports if the attribute’s value exists in the items attribute of sensor.sonos_favorites.
{% set channel = state_attr('media_player.bedroom', 'media_channel') | default(none) %}
{{ iif(channel in state_attr('sensor.sonos_favorites', 'items').values(), channel, 'No favorite') }} is playing
NOTE
If you need the both the Favorite key and value then here’s a way to do it.
{% set ns = namespace(k='No favorite', v='is playing') %}
{% for k, v in state_attr('sensor.sonos_favorites', 'items').items()
if v == state_attr('media_player.bedroom', 'media_channel') | default(none) %}
{% set ns.k, ns.v = (k, v) %}
{% endfor %}
{{ ns.k }} {{ ns.v }}
That looks promising. I’ll give it try. Thanks.
Be advised that, depending on your requirements, the template may need additional enhancements.
For example, if you stop/pause playback the media_channel attribute will continue to exist (and report the channel’s name). So if you want the template to report which favorite channel (if any) is actually playing, then you will need to check the media_player’s state (i.e. is it playing).
The following example reports Nothing is playing if the media_player’s state is not playing. In all other cases, it behaves like the first example posted above.
{% set player = 'media_player.bedroom' %}
{% set is_playing = is_state(player, 'playing') %}
{% set msg = iif(is_playing, 'No favorite', 'Nothing') %}
{% set channel = state_attr(player, 'media_channel') | default(none) %}
{% set favorites = state_attr('sensor.sonos_favorites', 'items').values() %}
{{ iif(is_playing and channel in favorites, channel, msg) }} is playing
Any progress to report after testing the suggested template?
Correct me if I'm wrong, but it looks like this will only work if there is an exact match? If I rename a favorite in the Sonos app (which I do), then this is not going to work?
Yes.
It attempts to find an exact match for the value of media_channel in the Favorites list.
I don't know (because I have never renamed a Favorite channel).
My assumption is that the values reported by media_channel reflect the values entered in Favorites. I suggest you test it and report the results.




