New Sonos Favourites sensor - iterate through values

Hi - I am trying to iterate through the list of favourites in the new sensor.sonos_favorites, in order to populate an openHASP display field.

My old code looked like the block below, note I have presented it this way to make it readable - actually this is all on one line in my code:

{%for source in state_attr('media_player.sunroom','source_list')%}
  {{source+"\n"|e}}
  {%-if not loop.last%}{%-endif%}
{%-endfor%}

Under the old Sonos integration a couple of versions ago this produced a list of radios station favourites.

The new one looks like:

{%for item in state_attr('sensor.sonos_favorites','items')%}
  {{item+"\n"|e}}
  {%-if not loop.last%}{%-endif%}
{%-endfor%}

Rather that the station names - I get:

Result type: string
FV:2/4
FV:2/6
FV:2/5
This template listens for the following state changed events:

Entity: sensor.sonos_favorites

If you look at the source data - this is the first part in the attribute field:

Attributes

items: 
FV:2/4: Edge FM
FV:2/6: hit104.9 The Border
FV:2/5: Triple M The Border 105.7

unit_of_measurement: items
icon: mdi:star
friendly_name: Sonos Favorites

Is there a way to extract the actual name?

If it’s any help, this template:

{{state_attr('sensor.sonos_favorites','items')}}

returns:

{
  "FV:2/4": "Edge FM",
  "FV:2/6": "hit104.9 The Border",
  "FV:2/5": "Triple M The Border 105.7"
}

And solved - need to reference back to the array to extract the value:

{% for fav in state_attr('sensor.sonos_favorites','items') %}
  {{state_attr('sensor.sonos_favorites','items')[fav]+"\n"|e}}
  {%-if not loop.last%}{%-endif%}
{%-endfor%}

Unless someone else has a more elegant way?

{{ state_attr('sensor.sonos_favorites','items').values() | list | join('\n') }}
1 Like

I added some template examples to the PR, but those should probably be in the docs: Add Sonos favorites sensor by jjlawren · Pull Request #70235 · home-assistant/core · GitHub. I’ll do that soon.

1 Like

Much neater - thanks.

Added docs in Add Sonos favorites notes by jjlawren · Pull Request #22642 · home-assistant/home-assistant.io · GitHub. Please make any suggestions if you feel those could be improved.

Hi,

I try it but i have no sensor.sonos_favorites on my 2022.5.3?

Any idea why my HA do not show the new favirtes sensor?

Regards, Steffen

You need to find the sensor.sonos_favorites entity, open its advanced section, and enable it.

Screen_20220509-175907

I do not have or find a advanced section, at the integration there is Sonos but no Advanced?

Steffen

On the integrations screen, find the Sonos integration. Click on the entities link to open up just the Sonos entities. In the top right of that screen click on the menu button (upside down triangle) and ensure ‘Show disabled devices’ is checked.

sensor.sonos_favorites should now appear in the list. Click on the sensor name to open the dialogue for that sensor. Advanced is at the bottom. If you don’t see Advanced on this dialog box, it’s a UI bug, click on the title of the box and it should appear.

Thank you very much. After several restarts und reintegration now I see the disabled device and could enable the sonos_favorites.

Why first 10 times the sonos_favorites was not visible, I do not know?

Regards, Steffen

I’ve been hacking away not really knowing what I’m doing and got this to work:

      - obj: "p2b5" # sources list
        properties:
          "options": >
            {% if not (is_state('media_player.living_room','unavailable')) %}{{"(no source)\n"|e}}{%for name in state_attr("sensor.sonos_favorites", "items").values()%}{{name+"\n"|e}}{%-if not loop.last%}{%-endif%}{%-endfor%}{% endif %}
          "val": >
            {% if not (is_state('media_player.living_room','unavailable')) %}{%for media_id, name in state_attr("sensor.sonos_favorites", "items").values%}
            {{loop.index if source == state_attr("sensor.sonos_favorites",'name') }}
            {%-endfor%}{% endif %}
          "click": "{{ 'false' if (is_state('media_player.living_room','unavailable') or is_state('media_player.living_room','unknown')) else 'true' }}"
1 Like

What is the purpose of the e in those two templates?

I wonder too. They may be something to do with OpenHASP, I have similar too in my (copied) OpenHASP yaml. If someone knows I’ll be interested to know.

This took some Google-foo, It does not like searching for a pipe symbol.

From OpenHASP:

  1. As raw payload~

Raw payloads are directly passed to the LVGL text rendering engine without any conversion on the MCU. You need to make sure the string is properly encoded into UTF-8 by the application sending the payload! How this is accomplished depends on the Home Automation tool:

  • At the end of the template you must indicate that Home Assistant needs encoded the string before sending it by appending |e (pipe symbol + e) at the end.
1 Like

It’s curious because if you use it alone it’s handled as Euler’s number.