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.