Print all the attributes and values of Plex sensor

I am using the Plex Activity Monitor and it creates a sensor.plexspy that has the following attributes:

{
  "friendly_name": "PlexSpy",
  "unit_of_measurement": "Watching",
  "user1": "Brave (2012)",
  "user2": "The Look of Silence (2015)"
}

I am using the following automation to notify me when someone is playing:

- alias: "Plex Spy notification"
  trigger:
    platform: state
    entity_id: sensor.plexspy
  action:
    service: notify.pushbullet
    data_template:
      message: >
        {{ trigger.to_state.state }} people streaming
      title: Plex Streaming

The automation works, but I only get a message saying 2 people streaming. Is there any way to iterate over all the attributes of sensor.plexspy and display all the attributes/values other than friendly_name and unit_of_measurement?

The ideal message would be user1 is watching Brave (2012). user2 is watching The Look of Silence (2015). I am sure some version of for loop can be used, but am not able to come up with a working solution.

Try something like

{% for attr in states.sensor.plexspy.attributes %}
{%- if  "user" in attr  -%}
 {{attr}} is watching {{states.sensor.stue.attributes[attr]}}
{%- endif %}
{%- endfor -%}
1 Like

user and user2 are not the actual names, so {%- if "user" in attr -%} will not work. Is it possible to just list all the attributes/values other than “friendly_name” and “unit_of_measurement”?

Try with
{%- if not attr==“friendly_name” and not attr==“unit_of_measurement” -%}

@Danielhiversen That totally works…you are my hero :slight_smile:

Here’s the final working template for anyone who is struggling with the same problem:

{% for attr in states.sensor.plexspy.attributes %}
{%- if not attr=="friendly_name" and not attr=="unit_of_measurement" -%}
 {{attr}} is watching {{states.sensor.plexspy.attributes[attr]}}
{%- endif %}
{%- endfor -%}
2 Likes

is this the only thing used as the sensor?

@Charles_Brown The snippet is the template that you can use in your automation or elsewhere in HA.

Sorry for the bump, but I wanted to see if I can get some help to extend this cool feature.
The example @arsaboo posted with the initial bits plus the final part works great, but it sends notifications for all users all the time.
I can add if conditions to the action to try to filter for a single user, but since trigger is met, the message fires all the time. They work, but it too late. The automation has fired and notifies regardless of what the if in the message. Which makes sense.

I just want to get an alert when my son is watching something, not everyone else in the house.

I was hoping for something like this added into a condition would work.
It doesn’t trigger an HA error on load, nor does it work:

- alias: "The Kid is Watching Plex Notice"
  trigger:
    platform: state
    entity_id: sensor.plex
  condition:
    condition: template
    value_template: '{{states.sensor.plex.attributes.johnnyboy}}'
  action:
    service: notify.pushbullet
    data_template:
      message: >
       {% for attr in states.sensor.plex.attributes %}
        {%- if not attr=="friendly_name"  and not attr=="unit_of_measurement" and not attr=="icon" -%}
         {{attr}} is watching: {{states.sensor.plex.attributes[attr]}}
        {%- endif %}
       {%- endfor -%}
      title: Check Johnnyboy's Show

So in this case, johnnyboy in the value_template’s condition would be the username that the Plex sensor reports.
I would love the trigger to only go off when the attribute johnnyboy exists.
If it exists, do the action’s notification. If not, do nothing.

I based the {{states.sensor.plex.attributes.johnnyboy}} in the condition’s template off of examples I’ve seen regarding attributes like a lights brightness existing or not, but this just isn’t cutting it. I’ve tried so many things at this point based on a bunch of other examples I’ve read, but in the majority of those the testing is done against a known value equaling a number value or string.

I’ve tried a few combos of

{%- if states.sensor.plex.attributes[0] == “johnnyboy” -%}

or [1],etc. to try to cover array possibilities
and…

{%- if states.sensor.plex.attributes == “johnnyboy” -%}

… quoted, non-quoted, etc…
and…

{%- if states.sensor.plex.attributes.johnnyboy = true -%}
… (or True or TRUE or not null or NOT NULL or is string ,etc. ) … quoted, non-quoted etc…

… but I get all sorts of errors in any and all attempts when the automations load using these types of tests.

Perhaps I am just missing the syntax on some of the possibilities that would work. I am just starting to figure out the jinja templating stuff for HA, so I fear that may be the case.

So, a long way to get here…
Is there a way to test for the existence of an attribute’s key name in a condition or trigger directly?

Please tell me I am just missing something simple or nor understanding the test conditions properly.

Thanks in advance!!!
:smiley:
cheers!
J

I haven’t tried this, but I am thinking that you can create a template sensor like:

            sensor:
              - platform: template
                sensors:
                  johnnyboyplex:
                    value_template: >-
                      {%- if states.sensor.plex.attributes.johnnyboy -%}
                        '{{states.sensor.plex.attributes.johnnyboy}}'
                      {%- else -%}
                        NA
                      {%- endif -%}
                    friendly_name: 'Johny Plex' 

Now, sensor.johnnyboyplex should be NA when nothing is being played, else will take the value of the content that is being played. You can then use it your automation.

1 Like

@arsaboo
You are the man!

That sensor definition for NA is the critical point. This opens up so much!!!
Thank you!!! :smile:

OK… my final working solution to see if the johnnyb user is watching something on Plex…

the automation is:

#PLEX_TARGER_USER#1_IS_WATCHING
- alias: "Plex Watching Notification #1"
  trigger:
    platform: state
    entity_id: sensor.plex
  condition:
    condition: template
    value_template: '{% if not is_state("sensor.johnnyb_watching_plex", "NA") %}true{% endif %}'
  action:
    service: notify.pushbullet
    data_template:
      message: >
        {% for attr in states.sensor.plex.attributes %}
         {%- if not attr=="friendly_name" and not attr=="unit_of_measurement" and not attr=="icon" -%}
          {{attr}} is watching: {{states.sensor.plex.attributes[attr]}}
         {%- endif %}
        {%- endfor -%}
      title: JB is Watching Plex! 

and for the sensor:

sensor:
  - platform: template
    sensors:
      johnnyb_watching_plex:
        value_template: >-
          {%- if states.sensor.plex.attributes.johnnyb -%}
            '{{states.sensor.plex.attributes.johnnby}}'
          {%- else -%}
            NA
          {%- endif -%}

Again, thanks!!!

This works well in 3 users testing real quick!
NICE!

cheers!
J

2 Likes

OK, side note…
This works, but upon each restart I get an automation error, but all is well. I just need to dismiss the state card.

17-02-23 22:38:40 WARNING (MainThread) [homeassistant.components.sensor.template] Could not render template JB is Watching Plex…, the state is unknown.

Any thoughts to get rid of that error/warning?
Its not that big of a deal once this goes into ‘production’, but it is a tad annoying.

Thanks either way!!!
J

Try the following automation (or some version of this):

#PLEX_TARGER_USER#1_IS_WATCHING
- alias: "Plex Watching Notification #1"
  trigger:
    platform: template
    value_template: '{{ states.sensor.johnnyb_watching_plex.state != 'NA' }}'
  action:
    service: notify.pushbullet
    data_template:
      message: >
        JB is watching {{ states.sensor.johnnyb_watching_plex.state }}
      title: JB is Watching Plex!

sorry to bump this, but i just discovered the thread and would love to ask you this:

I’ve created the sensor, but since i use Custom-ui the sensor has a somewhat unexpected result:

02

and

43

- platform: template
  sensors:
    watching_plex:
      value_template: >-
           {% for attr in states.sensor.plex.attributes %}
           {%- if not attr=="friendly_name" and not attr=="unit_of_measurement" -%}
           {{attr}} is watching {{states.sensor.plex.attributes[attr]}}
           {%- endif %}
           {%- endfor -%}

is there a way to prevent this?
Cheers,
Marius

Hello,

I’ve used these templates to great effect - thank you. They are great when you have unknown number of attributes with unknown attirbute names.

However, the template sensor fails if it gets to a length of more than 255 characters. This is a known and deliberate limitation of home assisstant (breaking change in 0.57). Does anyone know how to get this template value printed into the UI (specifically Lovelace) without having it as being returned in a sensor (which doesn’t work because of the character limit).

James

Here’s the sensor I ended up with: It either returns “Idle” or the music/episode/movie being played and the username playing it.
I like it for the historical record.

Adding sensor because bits may help others in this thread.

    shield_plex_playing:
      friendly_name: "Media playing on Shield (Plex)"
      value_template: >
        {% if (is_state('media_player.shield', 'playing') or is_state('media_player.shield', 'paused')) %}
          {% if is_state_attr('media_player.shield', 'media_content_type', 'music') %}
            {{ state_attr('media_player.shield', 'session_username') }} {{ states('media_player.shield') }} {{ state_attr('media_player.shield', 'media_artist') }} - {{ state_attr('media_player.shield', 'media_title') }} (From {{ state_attr('media_player.shield', 'media_album_name') }})
          {% elif is_state_attr('media_player.shield', 'media_content_type', 'tvshow') %}
            {{ state_attr('media_player.shield', 'session_username') }} {{ states('media_player.shield') }} {{ state_attr('media_player.shield', 'media_series_title') }} S{{ state_attr('media_player.shield', 'media_season') }}E{{ state_attr('media_player.shield', 'media_episode') }} - {{ state_attr('media_player.shield', 'media_title') }} (From {{ state_attr('media_player.shield', 'media_library_name') }})
          {% elif is_state_attr('media_player.shield', 'media_content_type', 'movie') %}
            {{ state_attr('media_player.shield', 'session_username') }} {{ states('media_player.shield') }} {{ state_attr('media_player.shield', 'media_title') }} (From {{ state_attr('media_player.shield', 'media_library_name') }})
          {% else %}
            {{ state_attr('media_player.shield', 'session_username') }} {{ states('media_player.shield') }} unknown: {{ state_attr('media_player.shield', 'media_title') }}
          {% endif %}
        {% else %}
          Idle
        {% endif %}

Example value: josh playing The Golden Girls S3E1 - Old Friends

1 Like

Is this still working? because it just shows idle for me.

It is still working yes.

Of note: you do have to change the media player to match your media player (shield is just the example)

P.s. If anyone knows how to set a variable so that media_player.shield only needs to be specified once (and duplicating is easier) please share.

so that explains it. If im understanding your code shows what plex is playing on an individual machine. So how would I have it show whatever is playing from the plex server if I dont care which computer or monitor its on?

Based on the way Hass is designed, I’m not sure that’s possible.
If you have Plexpass you could use the webhook functionality and run that through a template.
Because Plex sends the webhook on all player events, that would get you what you’re looking for.