Extracting non typed data from json in Plex integration

So I want to put some data on a widget so that I can see what devices are playing what content from my Plex server.

I can see in the developer tools states, that the Plex Media Server integration has attributes against the main Plex sensor which lists a number of attributes which can be retrieved as json when I’ve been playing in the template editor.

However, I can only really reliably pull data if I know the name value pairs name.

See the above screenshot, which contains attributes “unit_of_measurement” with a value of “watching”, “friendly_name” with a value of “plexdocker” and then from hereon in, the content will list the device that’s playing content as the element name and the media as the element value, so with multiple devices playing there will be an extra line in the json. Since I don’t know all the media play names that will appear here (and in particular the friendly name for them), how can I iterate this data and show all the players that are playing? Note that I do not want to see the unit_of_measurement or the friendly_name?

Copy-paste the following template into the Template Editor.

{{ states.sensor.plexdocker.attributes
  | reject('match', '(unit_of_measurement|friendly_name)')
  | list }}

It should create a list of the sensor’s attributes excluding unit_of_measurement and friendly_name. What remains are the names of the players.

What is the next step? How/where do you want to use it?

1 Like

Thanks Taras, don’t know why I was hung up on it being json rather than using your approach, I’ve used the select and reject options elsewhere before!

So now that’s listing the players that are playing like so:

[‘GadgetBazza - Plex for Sonos’, ‘GadgetBazza - Plex for Apple TV’]

So what I’d like to do is add a template card or something to list the Plex server status and what media is playing on what device, something like the below image…

Try this version:

{% for x in states.sensor.plexdocker.attributes.items()
  | rejectattr(0, 'match', '(unit_of_measurement|friendly_name)') -%}
{{ x[0] }} {{ x[1] }}
{% endfor %}

If the result is satisfactory, you can use it as-is in a Markdown card or add a bit more to the template so that it produces what’s needed to create a Markdown Table.

| Player | Title |
| :---   | :---  | 
{% for x in states.sensor.plexdocker.attributes.items()
  | rejectattr(0, 'match', '(unit_of_measurement|friendly_name)') -%}
|{{ x[0] }}|{{ x[1] }}|
{% endfor %}
1 Like

That’s awesome, I new there must be a way to iterate the array!

Thanks Taras

1 Like

If all of your player names start with GadgetBazza - Plex for and you would like to eliminate it from being displayed, you can trim off the first 23 characters like this:

|{{ x[0][23:]}}|{{ x[1] }}|