Simple notifications when Plex is playing

@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.

I came across a problem that I would like to know if it’s a known issue or not. It may well be that there is no way to fix it, I would just like to bring it up in case it hadn’t been looked into.

When I get notifications that something is playing I get USERNAME started playback on DEVICE but if I get another notification from a different device, or from 3 devices at the same time, it just has a single Username on top of all the messages, I believe the last to play something, but could be the 1st, not sure. I’d much prefer to have the device & user connected to the notification. I suppose the reason for this is because the Title changes with each notification, & it just updates the previous Title to the new Title. Ideally I’d prefer to have devices grouped together, if possible.

I’m going to attempt to make different automations for each different device, hopefully that will give me what I’m looking for, but wondered if anyone else had this issue?

So having separate automations still gets them grouped into a single clump it seems.

I have moved the username to the message instead of the title, but still hoped that the separate devices would be grouped together.
Here’s what I’m using

    data:
      title: Playback started on {{ device }}
      message: |
        {% if series == None %}
          '{{ username}} started {{ title }}'
        {% else %}
          {{ username}} started {{ series }} - S0{{ season }}E0{{ episode }}
          {{ title }}
        {% endif %}

Edit: I gave up trying to get the Title to be separate instead of just the last device to start. Instead I just put all the information in the Message & just changed the Title to “Plex Playback Started” since it is lost when another message comes in

This is what I have now

    data:
      title: Plex Playback Started
      message: |
        {% if series == None %}
          {{ device }}
          {{ username}} started {{ title }}
        {% else %}
          {{ device }}
          {{ username}} started {{ series }}
          S0{{ season }}E0{{ episode }} - {{ title }}
        {% endif %}

Not sure how doable this is but i recently started using prerolls on my plex server (which work great) but this notification gets sent when the preroll plays as well. Does anyone know if there’s a way to not have the notification triggered when the video is only short?