Simple notifications when Plex is playing

I have a lot of users on my Plex, and I like to keep updated on what are the Plex clients watching.

Here is the Automation :

- id: '1431594919196'
  alias: Notifications Plex
  description: ''
  mode: single
  trigger:
  - platform: state
    from: 
      - "idle"
      - "unavailable"
    to: "playing"
    entity_id:
      - media_player.plex_television
      - media_player.plex_mom_television
  variables:
    device: "{{ state_attr(trigger.entity_id, 'friendly_name') }}"
    series: "{{ state_attr(trigger.entity_id, 'media_series_title') }}"
    title: "{{ state_attr(trigger.entity_id, 'media_title') }}"
    picture: "{{ state_attr(trigger.entity_id, 'entity_picture') }}"
  action:
    - service: notify.mobile_app_<phone name>
      data:
        title: "Playing on {{ device }}"
        message: >
          {% if series == None %}
            {{ title }}
          {% else %}
            {{ series }} - {{ title }}
          {% endif %}
        data:
          icon_url: "{{ picture }}"

If you have ideas to improve it, please let me know !

EDIT 11:
image

If you are using Hass.Agent to have notifications on windows like on your phone, you can add this action under the first one:

    - service: notify.hass_agent_nameOfYourDevice
      data:
        title: "Playing on {{ device }}"
        message: >
          {% if series == None %}
            {{ title }}
          {% else %}
            {{ series }} - {{ title }}
          {% endif %}
        data:
          image: "http://homeassistant.local:8123{{ picture }}"

Cool idea. I’m guessing this works only for guests in your home?

I love this, great job! I just set it up and it works great. Triggered when Plex played something on my phone, my Roku TV, and Roku streaming stick.

As for recommended changes, I think you should set it to run only if the state of that entity wasn’t set to paused. Right now I get notifications every time I pause/resume.

It can’t work for any since it’s triggered when a device from your home plays something.

Hmm,
I think it would work provided you set up the plex integration properly:

The trigger should be

sensor.plex_name 
unit_of_measurement: Watching

But it would require to set up the automation a bit different…

@chezpaul2
Just change the media_player entities with yours !
Don’t forget to also change the notify.mobile_app_<phone name>with your phone that has the home assistant app.

You also asked if it was working only for the guests. No, it works for every plex client you have that can connect to your plex server. So even your friend that are far away that use your plex will give a notification !

@djbrooks022
Good idea, I’ll work on it when I got time. I’ll tag you when I update this script.

@aceindy
If you were talking to me, I don’t understand what do you mean with

sensor.plex_name 
unit_of_measurement: Watching

I’m not sure that a media player has a unit of mesurment ?

@renaiku
What I meant is you can trigger on state change of the plex sensor, attribute ‘watching’:
image

@aceindy
aah, you meant that for not having the play/pause/play notification !

How would I check the changes with Watching going up, not down. like from 3 viewers to 4, but not from 4 to 3.

Yep…
I also had a look at it, but not so simple as it sounds :thinking:

I’ve added the compatibility with Hass.Agent to have desktopp notifications:
image

1 Like

@aceindy updated to only pop up once when starting playing from an idle or unavalaible state. no more spamming when pausing / unpausing !

EDIT:
Pictures seams to not work with apple products (tested on iphone and apple watch). If you are an apple user, please read the notification documentation and try to help us to make it work !

This is a great little script that works really well. I changed it slightly so it shows me the user name and the Season and Episode number of a show as well.
The only issue I’m running into (but that doesn’t seem to be related to this script) is that when I start something on my Apple TV, the user name shows up as `none’. All other devices show the user name correctly so it’s likely related to the Apple TV integration.

Can you post the automation that shows the username, show number, and episode number?

I have no idea if this is the best way of achieving this, but it works for me:

add this to the variables:

   username: "{{ state_attr(trigger.entity_id, 'username') }}"
   season: "{{ state_attr(trigger.entity_id, 'media_season') }}"
   episode: "{{ state_attr(trigger.entity_id, 'media_episode') }}"

Modify the title like this:

title: "{{ username}} started playback on {{ device }}"

and modify the message like this:

{{ series }} - S{{ season }}E{{ episode }} - {{ title }}

Thanks! That worked perfectly.

I would appreciate knowing how to extend this to when the show is stopped. Currently I seem to be running into a problem which I believe is because various state attributes become unavailable when the
state of the media player goes to idle.

I tried to sort this with trigger.from_state.attributes.media_season or state_attr(trigger.from_state, media_season) to pick up what was being played but I still couldn’t get it to function, I think because the other variables contained in the script were still expecting to have values but couldn’t take any. Templating is not my strong point!

I believe you are right, the variables are blank because the state changed already. The closest I could get is the device name in the automation, like this. Just doesn’t show any TV show info.

alias: Notifications Plex - Stopped (Test)
description: ''
trigger:
  - platform: state
    from:
      - playing
    to: idle
    entity_id:
      - media_player.plex_plex_web_chrome_2
condition: []
action:
  - service: notify.mobile_app_pixel_5_danny
    data:
      title: 'Playback on {{ device }} ended'
      message: |
        Show was stopped.
mode: single
variables:
  device: '{{ state_attr(trigger.entity_id, ''friendly_name'') }}'

I managed to get this sorted using this - my heaviest templating yet!

- id: '1431594919196'
  alias: Notifications Plex new
  mode: restart
  trigger:
    - platform: state
      from:
        - "playing"
        - "paused"
      entity_id:
        - media_player.plex_bedroom_shield
        - media_player.plex_bedroom_shield2
        - media_player.plex_lounge_shield
    - platform: state
      to:
        - "playing"
        - "paused"
      entity_id:
        - media_player.plex_bedroom_shield
        - media_player.plex_bedroom_shield2
        - media_player.plex_lounge_shield
  variables:
    device:    "{{ state_attr(trigger.entity_id, 'friendly_name') }}"
    series:    "{{ state_attr(trigger.entity_id, 'media_series_title') }}"
    title:     "{{ state_attr(trigger.entity_id, 'media_title') }}"
    picture:   "{{ state_attr(trigger.entity_id, 'entity_picture') }}"
    username:  "{{ state_attr(trigger.entity_id, 'username') }}"
    season:    "{{ state_attr(trigger.entity_id, 'media_season') }}"
    episode:   "{{ state_attr(trigger.entity_id, 'media_episode') }}"
    devstate:  "{{ trigger.to_state.state }}"
    prevseries:     "{{trigger.from_state.attributes.media_series_title}}"
    prevtitle:      "{{trigger.from_state.attributes.media_title}}"
    prevpicture:    "{{trigger.from_state.attributes.entity_picture}}"
    prevusername:   "{{trigger.from_state.attributes.username}}"
    prevseason:     "{{trigger.from_state.attributes.media_season}}"
    prevepisode:    "{{trigger.from_state.attributes.media_episode}}"

  action:
    - service: notify.jamesgroup
      data:
        title: >
          {% if username == None %}
            {{prevusername}} {{devstate | replace('idle', 'stopped')|replace('unavailable','stopped')}} on {{device}}
          {% else %}
            {{username}} {{devstate }} on {{device}}
          {% endif %}
        message: >
          {% if username == None %}
            {% if prevseries == "" %}
              {{ prevtitle }} {{devstate | replace('idle', 'stopped')|replace('unavailable','stopped')}} on {{device}}
            {% else %}
              {{ prevseries }} - S{{'%02d' % prevseason}}E{{'%02d' % prevepisode}} - {{ prevtitle }} {{devstate | replace('idle', 'stopped')|replace('unavailable','stopped')}} on {{device}}
            {% endif %}
          {% else %}
            {% if series == None %}
              {{ title }} {{devstate }} on {{device}}
            {% else %}
              {{ series }} - S{{'%02d' % season}}E{{'%02d' % episode}} - {{ title }} {{devstate}} on {{device}}
            {% endif %}
          {% endif %}
        data:
          icon_url: >
            {% if username == None %}
              {{prevpicture}}
            {% else %}
              {{picture}}
            {% endif %}
          notification_icon: "mdi:plex"
          tag: >
            {% if username == None %}
              {{prevusername}} {{device}}
            {% else %}
              {{username}} {{device}}
            {% endif %}

I think the reason it wasn’t working before is because I was filtering variables that didn’t exist (in the variables section) or I was using a replace filter on device in the variables section (which did exist, but for some reason it didn’t like it). Anyway, this works, I just need to create a wildcard somehow for media_player.plex* so that I don’t have to list out everything.

PS - these if statements all work fine, except for the icon_url one, which always returns false apparently. This means when a show is playing or paused, it can find the show/movie picture. However, if it’s idle/unavailable, then it does not find the picture and no picture is displayed. Can’t figure out why this is.

EDIT - I tried putting a static icon for the prevpicture part of the template but it still didn’t work.

Works very well with picture on iphone and applewatch if you use:

    data:
      image: "http://homeassistant.local:8123{{ picture }}"

Its funny