Status of a Plex client, useful to trigger automation

I’m using @JesseWebDotCom component: PlexDevices and it’s a really impressive work, well done!

I’ve had some problem with the status is not updating as it should, this holds for when you exit a player or a movie is finished. The status of the player continues to be “playing”. I don’t know if this is a know bug.

Personally, I only need the status of a certain Plex client to automate my living room when looking at a movie, thus I don’t need all the functions in the impressive PlexDevices. This script is very limited compared to PlexDevices, but it’s useful for automation.

Python

I haven’t wrote a component, it’s raw python but it should be simple enough to use. Paste this code into a file called plexstatus.py. Modify the user-defined settings in the top of the code.

import json
import requests


################################
#		Start user-defined settings
################################
plexToken =  <X-PLEX-TOKEN> 
# Same as in PlexDevices 
plexURL = <IP to plex>
plexPORT = <port to plex>
trackedPlayer = <Name of device you want to track> 
# Name of devce can be found in Status-page at plex web. Case-Sensitive!

# exemple use:
# plexToken = 'gd887gd9fgfd7cf8'
# plexURL = 'http://192.168.0.2'
# plexPORT = '32400'
# trackedPlayer = 'RasPlex'

################################
#		End user-defined settings
################################

url = plexURL + ':' + plexPORT + '/status/sessions/'
headers = {'Accept': 'application/json', 'X-Plex-Token': plexToken}
r = requests.get(url,headers = headers)
myjSon = json.loads(r.text)

nbrOfSessions = myjSon['MediaContainer']['size']
trackedPlayerStatus = "idle"

if nbrOfSessions == 0:
	print(trackedPlayerStatus)

if nbrOfSessions > 0:
	videoStates = myjSon['MediaContainer']['Video']
	trackedPlayerStatus = "idle"
	for i in range(0, len(videoStates)):
		player = videoStates[i]['Player']['title']
		if player == trackedPlayer:
			trackedPlayerStatus = videoStates[i]['Player']['state']
			break
	print(trackedPlayerStatus)	

##Home-assistant
Create a sensor in Home-assistant:
I placed plexstatus.py in /home/homeassistant/.homeassistant/python/, you may place it elsewhere. Change the path in the command below according to where you placed your python-file.

- platform: command_line
  name: rasplex
  command: 'python3 /<path-where-you-placed-python-file>/plexstatus.py'
  scan_interval: 10

The sensor will be having three states:

  • playing
  • paused
  • idle

This is my first script ever in python, I hope it’s useful. I’m planing to making it into an AppDaemon and then it can be a little more complex.

5 Likes

Nice work, i might eventually use this one if the other one doesn’t work (i do like the pictures/control) but i assume that debugging that one was a bit out of your reach? (i know it was for me)

I’m thinking about doing something similar using the webhooks. I’m hoping it will be a bit better than polling. Also I really hope thats not your real plex token in there! If it is burn it asap.

I never looked into it actually since I only need the state of the client, no pictures or other info.

Plex only reports if a movie is playing or paused, the rest needs to be solved by logic as you see in the code. I suspect that PlexDevices doesn’t do this properly?

No, it’s a faked one :slight_smile:

If your looking into using plex webhooks would you mind doing it as a PR to the main plex media player? The original author wasn’t interested in doing that.

Cheers!
/R

My understanding is the web server hosting hass can’t process the webhooks sent from plex. I think we will need an external component that calls into hass.

I might have a chat with some devs and see what the best way to architect this might be

I don’t have plexpass and I think it’s needed to test it out fully.

Let me know if it works out, maybe we could implement webhook and make a component out of this script.

Do plex webhooks require the HA to be accessible via internet?
My HA is only accessible via VPN, thus it would be a dealbreaker for me to fiddle with webhooks.

No, it’s the server that sends the webhook.

Great, I read your post on github about this issue. What are you posting into the plex webhook field?

Maybe we could discuss this on gitter to find a solution, when you have the time. Or just hit me a message.

Sounds good. I’ll be around around 8pm GMT if that works for you?

Yes, it works for be. Great!

I’m in a bit early :slight_smile:

@RitteT @rossdargan Have either of you made any updates? I’m really in the same situation as you. I just want basic Plex server status and to act on a Plex event. IE: modify the lights in the theater when play/pause.

did you get either working?

my setup…


plex_status.yaml containing…
platform: command_line
name: rasplex
command: ‘python3 /config/scripts/plexstatus.py’
scan_interval: 10

and plexstatus.py in my scripts directory.

any help is appreciated

The code in the first post should give you proper play/pause-status of a plex client if you just follow the steps.

Have you done that and it’s still not working?

yes. However I don’t know where to put the “platform: command_line” stuff. I have separate directories for sensors, switches and lights.

Right now I have created plex_status.yaml and this file is in the same directory as my configuration.yaml file. NOTE: I changed the command: to ‘python3 /config/plexstatus.py’. This setup give me this error.

The following components and platforms could not be set up: * script Please check your config

I assume the script should give me a sensor named “sensor.rasplex”

thanks for the help

FYI… I’m using the latest image of hass.io on RPI3

@hass.io

ok, so I just tried to run the script manually and got an error “-ash: python3: not found”

ok, making progress.

  • created directory like the documentation says “python_scripts”. so I put the python script there.
  • movedplex_status.yaml to my sensors directory.
  • Command: is… NOTE: double quote instead of single quote.
    • command: “python3 /config/python_scripts/plexstatus.py”

With setup I get this error…

2017-08-05 11:28:00 INFO (SyncWorker_11) [homeassistant.components.sensor.command_line] Running command: python3 /config/python_scripts/plexstatus.py
Traceback (most recent call last):
File “/config/python_scripts/plexstatus.py”, line 36, in
videoStates = myjSon[‘MediaContainer’][‘Video’]
KeyError: ‘Video’
2017-08-05 11:28:01 ERROR (SyncWorker_11) [homeassistant.components.sensor.command_line] Command failed: python3 /config/python_scripts/plexstatus.py