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?
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…
{% 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 %}
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:
The GadgetBazza bit is the user playing the content, would be nice to keep that, but getting rid of the “Plex for” piece would be handy, is there a way to locate the ‘-’ character and do some string manipulation based on that?
I’d like to send this content out in a message to my phone (as my children have a habit of watching content when they are supposed to be going to sleep).
I’ve tried adding similar content into a notify action in an automation, but the yaml never seems to save (when I come back it still has the fixed string message that I started with). I assume this is because the construct of the yaml is incorrect, but there is no error to describe where I am going wrong. Is it possible to use the for next logic in a notify template?