Simple notifications when Plex is playing

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.

1 Like

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

So I’ll put this note here because I discovered my solution from playing around after your post.

I was having a problem where mine wasn’t showing any picture at all on HASS.Agent.
I tried the quotations & it didn’t change anything.
Then I tried https since I prefer it to use the SSL I had set up & with the

        image: https://homeassistant.local:8123{{ picture }}

it didn’t work
But when I put in my SSLed DNS it finally showed the picture properly

        image: https://www.MYuniqueSUBDOMAIN.duckdns.org:8123{{ picture }}

So anyone else having that issue there’s a solution

Question: is there any way to get it to format the Season & Episode with 2 digits?
I like this & am using it, but it just looks weird to me saying Show Name S1E4 - Episode Title instead of Show Name S01E04 - Episode Title

That may be just my preference, but I feel others might feel the same, I just have no idea how to make it return the Season & Episode values with at least 2 digits

I haven’t found a way to do so unfortunately. I agree it looks a bit weird like this.

No worries, For now I have just changed it to

      {{ series }} S0{{ season }}E0{{ episode }} - {{ title }}

It’s pretty solid for season since most have less than 10, Episodes however look a bit odd with S02E011 but personally I think that looks less off-putting. I was just curious, if it’s a limitation of the scripting then it is what it is. What language is the scripting done in?

Anyway. It’s a great automation either way.