Plex Status - Video, Audio, Photo, Play Progress

Python and python framework

Moderator: leecollings

racquemis
Posts: 73
Joined: Monday 02 November 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Plex Status - Video, Audio, Photo, Play Progress

Post by racquemis »

On September 9th galadril posted a script a return the Plex playback status to Domoticz.
This script didn’t have everything I needed so I decided to make some improvements.
Since the resulting script works so well for me I thought I share it with the rest of you.

Features:
  • Differentiates between photo, audio and video playback
  • Differentiates between multiple plex clients
  • Reports the playback of multiple clients simultaneously. No need to run multiple script instances. Just fill in the names of the clients it should listen for.
  • Ability to show Play Progress
    • Automatically clears the device log to prevent cluttering the database (can be turned off)
  • Automatically creates and updates an status user variable for each client specified in the settings.
    • Status Codes:
      • -1: Plex Server Offline
      • 0: Client Idle
      • 1: Playing Video
      • 2: Paused Video
      • 3: Stopped Video
      • 11: Playing Audio
      • 12: Paused Audio
      • 13: Stopped Audio
      • 21: Showing Photo
      • 22: Paused Slideshow
  • Quick detection of stopped state on Video & Audio (10 seconds)
Preparation
  • In Domoticz create an text object or each for of the plex clients you wish monitor, write down their corresponding IDX
  • Open the script in notepad and change the domoticz and plex IP/port to match your own setup.
  • Enter the names of the plex clients under dom_PlexPlayers and their corresponding text object idx under dom_PlexPlayInfo_ID
  • Save the script
Installation
Assign 0775 permission to the plex.py file.
Create a cronjob to attempt to run the script every 10 minutes.

Code: Select all

 */10 * * * *  /home/pi/domoticz/scripts/python/plex.py
This will ensure the script is restarted when it encounters an unhandled exception.
If everything went right you could soon be seeing something similar to this when you play something
If you still run into problems (permission issues) strip the PIDFile lines from the file and run the script at reboot (thanks to bob123bob)

Code: Select all

@reboot python /home/pi/domoticz/scripts/python/plex.py &
Keep in mind that if the script encounters an fatal error it will only start again after you have rebooted.
Image
Use the user variables this script creates to trigger events.
Image
Authentication
The script is made for Plex Server sthat are setup NOT to use authentication. If your server does use authentication try the following: under Plex Options>Server>Network you can specify which networks can access the server without authentication. You can enter specific IP addresses or whole networks (e.g 192.168.0.1) don’t forget to add the subnet as shows in the settings description.

When using Plex Home: Fill in the plexToken parameter in the script. Follow these instructions to retrieve the token.

Script:
Get the code on my github page:
https://github.com/racquemis/DomoticzSc ... ython/plex

Bugs
Please let me know if you encounter an error.
Last edited by racquemis on Sunday 05 March 2017 19:15, edited 24 times in total.
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by gizmocuz »

Are we using domoticz for a home automation system, or for media center control ?
Quality outlives Quantity!
racquemis
Posts: 73
Joined: Monday 02 November 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by racquemis »

This doesn't control anything. Just returns the playback status so we can trigger lighting and such.
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by gizmocuz »

Okey, makes sense ! :mrgreen:
Quality outlives Quantity!
Bob123bob
Posts: 31
Joined: Monday 09 March 2015 7:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by Bob123bob »

Great works here !

Can you update your script because at the moment I don't see the status 21 et 22 for slideshow in the script ?

21: Showing Photo
22: Paused Slideshow
racquemis
Posts: 73
Joined: Monday 02 November 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by racquemis »

Glad you like it.

You're absolutely right btw, must have forgotten to implement it.
Script has been updated.
Bob123bob
Posts: 31
Joined: Monday 09 March 2015 7:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by Bob123bob »

Thanks for quick update. I appreciate. And again thanks for your contribution.
I will implement it quickly.
Bob123bob
Posts: 31
Joined: Monday 09 March 2015 7:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by Bob123bob »

I just implement it and it works like a charm
But I have a question about the crontab entry which run the script every 10 min while there an eternal loop in the script (while 1 == 1)
Do you think we can just run the script once at reboot using this crontab entry ?

Code: Select all

@reboot python /home/pi/domoticz/scripts/plex.py &
racquemis
Posts: 73
Joined: Monday 02 November 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by racquemis »

That should work just fine too :). I used a crontab under development just in case. But I think i have everything covered with catch/try so it should be fine.
Bob123bob
Posts: 31
Joined: Monday 09 March 2015 7:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by Bob123bob »

Ok the script is now running at boot using crontab
I made some corrections

old code

Code: Select all

 if plex_PreviousState[PlayerID] == state:
            StateChange = 0
          else:
            StateChange = 1
            plex_PreviousState=state
replace by

Code: Select all

 if plex_PreviousState[PlayerID] == state:
            StateChange = 0
          else:
            StateChange = 1
            plex_PreviousState[PlayerID] = state
I was having too many url request and some timeout that crash the script

I also remove the PIDFILE part because the file is owned by root so the user pi has no access in write
racquemis
Posts: 73
Joined: Monday 02 November 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by racquemis »

Thanks for bug fixing :). Noticed excessive logging myself, hadn't come around it fixing it. I'll change it in the script. I have no problems with the pidfile part myself. It seems to work as intended for me. Copied it from the ping script in the wiki.

But since the script works fine running at boot this code is unnecessary anyway. ;)
Thanks for the info.
cattoo
Posts: 60
Joined: Sunday 20 September 2015 17:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3072
Location: Sweden
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by cattoo »

Great script!
Im a newbie on domoticz stil, so how can i events triggered by the status of plex? Ill assume its by LUA. But could someone give me an example so ill know how to move on.
Tnx :)
gdg16
Posts: 7
Joined: Monday 21 September 2015 11:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by gdg16 »

Nice script, thank you.

But unfortunately crontab isn't executing the script :( when i run the script by hand its working great. I already changed the permissons on plex.py to 0755.

Does anyone know what is going wrong?
gdg16
Posts: 7
Joined: Monday 21 September 2015 11:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by gdg16 »

gdg16 wrote:Nice script, thank you.

But unfortunately crontab isn't executing the script :( when i run the script by hand its working great. I already changed the permissons on plex.py to 0755.

Does anyone know what is going wrong?
Already figured it out, i used the boot script from Bob123bob.
racquemis
Posts: 73
Joined: Monday 02 November 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by racquemis »

cattoo wrote:Great script!
Im a newbie on domoticz stil, so how can i events triggered by the status of plex? Ill assume its by LUA. But could someone give me an example so ill know how to move on.
Tnx :)
LUA is possible but I would start with the Event system in domoticz if i were you. Blockly is quite easy to ease. Read the wiki on the subject. The variable that contains the plex status should be under 'User Variables' on the Event editor.
cattoo
Posts: 60
Joined: Sunday 20 September 2015 17:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3072
Location: Sweden
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by cattoo »

Well now i have managed the events, but ill have problems with the time.
Ive set the poll time to 5 in the script, and the crontab to 10 just like the instruction, and the status only checks every 10 minutes. And it should poll every 5 seconds if i got it right?
So why doesn't it do it?
What have i missed?
racquemis
Posts: 73
Joined: Monday 02 November 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by racquemis »

Try to run the script manually. I suspect that the script encounters an fatal error which stops the script execution. Running it manually you can see the error as it occurs.

Post the error here for me to check, it could be a configuration error or a possible bug in the script.
cattoo
Posts: 60
Joined: Sunday 20 September 2015 17:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3072
Location: Sweden
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by cattoo »

This is what ill get when running the script

Code: Select all

 python /home/pi/domoticz/scripts/python/plex.py
09:43:52- Seems to be an old file, ignoring.
IDLE
Traceback (most recent call last):
  File "/home/pi/domoticz/scripts/python/plex.py", line 369, in <module>
    open(pidfile, 'w').close()
IOError: [Errno 13] Permission denied: '/home/pi/domoticz/scripts/python/plex.py_192.168.1.20.pid'
racquemis
Posts: 73
Joined: Monday 02 November 2015 18:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by racquemis »

Give plex.py 0775 permission, if that doesn't work, strip all the Pidfile stuff from the file and add the script to crontab to run at reboot as Bob123Bob suggested:

Code: Select all

@reboot python /home/pi/domoticz/scripts/plex.py &
cattoo
Posts: 60
Joined: Sunday 20 September 2015 17:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3072
Location: Sweden
Contact:

Re: Plex Status - Video, Audio, Photo, Play Progress

Post by cattoo »

Thank you, it seems to work when i run the script manually. So its a start, dont have the time to check the rest for the moment. The Pid removal seems to be the key.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest