Help find all integration entities for sonos currently playing

I’m trying to identify media players that are actively playing through Sonos, but I’m having trouble getting the right results with my current approach. I’ve tried filtering the list of Sonos entities to only include media players and then checking their state, but it’s not returning the expected results.


{{ integration_entities("sonos") | selectattr("state", "equalto", "playing") | select("match", "media_player") | list }}

well here is was I wanted

{{ expand(integration_entities('plex')) | selectattr('state', 'eq', 'playing')| map(attribute='attributes') | selectattr('media_title', 'defined') | map(attribute='friendly_name') | list }}
1 Like

Thanks for posting your solution. did you end up using the plex intergration for this? or mis posted the code? i would very much like to use this, in this blueprint: Group Sonos based on presence (e.g. Motion)

i use it for plex and sonos, since i have a shared plex account and want to see what movie everyone is watching and what time and for how long.


'''
- platform: template
  sensors:
    movie_watching_sensor:
      friendly_name: "Plex Playing Now"
      value_template: >
        {% set devices = expand(integration_entities('plex')) %}
        {% set playing_devices = devices | selectattr('state', 'eq', 'playing') %}
        {% set movie_data = playing_devices | map(attribute='attributes') | selectattr('media_title', 'defined') | list %}
        {% set ns = namespace(movie_info=[]) %}
        {% if movie_data %}
          {% for device in movie_data %}
            {% set ns.movie_info = ns.movie_info+[device.media_title + " - " + device.friendly_name] %}
          {% endfor %}
          {{ ns.movie_info | join(', ') }}
        {% else %}
          No movie data available
        {% endif %}
'''

here is what i use to join sonos speakers

service: media_player.join
target:
  entity_id: >
    {{integration_entities("sonos") | select("match",
    "media_player")|list|first}}
data:
  group_members: >
    {% set sonos= integration_entities("sonos") | select("match",
    "media_player") | list%}

    {{sonos[1:]}}
alias: Join Sonos Speakers
1 Like

Thanks! but that code just gives me the first speaker, not the actively playing speaker. ive added selectattr('state', 'eq', 'playing') to the template but that doesnt seem to work

{% set ns = namespace(players=[]) %}
{% for player in expand(integration_entities('sonos') | select("match","media_player"))| selectattr('state', 'eq', 'playing')|list%}
  {% set ns.players = ns.players + [player.entity_id] %}
{% endfor %}
{{ ns.players }}
{{ integration_entities('sonos')
  | select('match', 'media_player')
  | expand
  | selectattr('state', 'eq', 'playing')
  | map(attribute='entity_id') | list }}

Yes! this works, great thanks!

1 Like