Print all the attributes and values of Plex sensor

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.

you can try something like this:

{% set player = states('media_player.shield') %} 
{% set user = state_attr('media_player.shield', 'session_username') %}
{% set media = state_attr('media_player.shield', 'media_title') %}
{% if player in [ 'playing','paused'] %}
  {% if is_state_attr('media_player.shield', 'media_content_type', 'music') %}
    {{ user}} {{ player }} {{ state_attr('media_player.shield', 'media_artist') }} - {{media }} (From {{ state_attr('media_player.shield', 'media_album_name') }})
  {% elif is_state_attr('media_player.shield', 'media_content_type', 'tvshow') %}
    {{ user }} {{ player }} {{ state_attr('media_player.shield', 'media_series_title') }} S{{ state_attr('media_player.shield', 'media_season') }}E{{ state_attr('media_player.shield', 'media_episode') }} - {{media }} (From {{ state_attr('media_player.shield', 'media_library_name') }})
  {% elif is_state_attr('media_player.shield', 'media_content_type', 'movie') %}
    {{ user }} {{ player }} {{ media }} (From {{ state_attr('media_player.shield', 'media_library_name') }})
  {% else %}
    {{ user}} {{ player }} unknown: {{ media }}
  {% endif %}
{% else %}
  Idle
{% endif %}

which is somewhat smaller. Im sure the rest can be shortened also, but it would probably require some more work…

Let me take a step back. By default I get a sensor that displays how many viewers are watching. If I click on that sensor I can than see what they are each watching. Why is it difficult to have that information displayed as well without the need to click on it?

If you go to “Developer Tools” > “States”, anything you see there can be pulled in to a template sensor.
The trick is that homeassistant does extra magic to show what’s in the UI so you don’t always have access to the magic.

silly of me to forget, to post this:

{% if states('sensor.plex') != '0' %}
  {%- for attr in states.sensor.plex.attributes %}
    {%- if not attr in ['friendly_name', 'unit_of_measurement','custom_ui_state_card','icon','supported_features','entity_picture'] %}
                  {{attr}} is watching {{states.sensor.plex.attributes[attr]}}
     {%- endif %}
   {%- endfor %}
 {% else %} Watching Plex: {{states('sensor.plex')}}
{% endif%}

which works just fine, why don’t you have a look if this is something you can use

5 Likes

Tested that here with a single user playing and it works. @teitelbot this looks like your solution.

1 Like

Thank You. Works great. Anyway I can get it to show a pic instead of text, the way it shows up in plex?

not that easily i am afraid.
You can get the images of the individual media_players like this:

        entity_picture_template: >
         {% if states('media_player.plex_telefoon') == 'playing' %}
            {{state_attr('media_player.plex_telefoon','entity_picture')}}
         {% else %} /local/various/plex.png
         {% endif %}

but these players are created dynamically, so you would need to give it some thought how to incorporate that in to the sensor I shared above, considering that is not targeting a single media_player.

the individual player looks like this:

50