Page 1 of 6

Plex Status - Video, Audio, Photo, Play Progress

Posted: Saturday 07 November 2015 18:38
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.

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

Posted: Saturday 07 November 2015 19:30
by gizmocuz
Are we using domoticz for a home automation system, or for media center control ?

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

Posted: Saturday 07 November 2015 20:02
by racquemis
This doesn't control anything. Just returns the playback status so we can trigger lighting and such.

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

Posted: Sunday 08 November 2015 10:29
by gizmocuz
Okey, makes sense ! :mrgreen:

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

Posted: Monday 09 November 2015 11:23
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

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

Posted: Monday 09 November 2015 12:03
by racquemis
Glad you like it.

You're absolutely right btw, must have forgotten to implement it.
Script has been updated.

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

Posted: Monday 09 November 2015 21:50
by Bob123bob
Thanks for quick update. I appreciate. And again thanks for your contribution.
I will implement it quickly.

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

Posted: Wednesday 11 November 2015 11:26
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 &

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

Posted: Wednesday 11 November 2015 14:10
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.

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

Posted: Wednesday 11 November 2015 21:27
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

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

Posted: Thursday 12 November 2015 19:22
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.

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

Posted: Tuesday 24 November 2015 8:47
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 :)

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

Posted: Tuesday 24 November 2015 16:28
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?

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

Posted: Tuesday 24 November 2015 16:50
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.

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

Posted: Thursday 26 November 2015 12:17
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.

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

Posted: Friday 11 December 2015 12:34
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?

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

Posted: Saturday 12 December 2015 7:47
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.

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

Posted: Saturday 12 December 2015 9:44
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'

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

Posted: Saturday 12 December 2015 10:06
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 &

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

Posted: Saturday 12 December 2015 10:55
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.