Page 1 of 3

PLEX status (dzVents)  [SOLVED]

Posted: Sunday 08 March 2020 20:27
by OedzesG
Hallo everyone!

today i finisched my PLEX Status script.
it gives my the PLEX mediaplayer status so i can for example controle the lights when a movie is started or paused..
no media playing.PNG
no media playing.PNG (14.33 KiB) Viewed 9455 times

playing.PNG
playing.PNG (15.75 KiB) Viewed 9455 times

if someone is interested, please let me know so i can make an how-2 and share the script.

Re: PLEX status (dzVents)

Posted: Sunday 08 March 2020 21:51
by EdwinK
Please share.

Re: PLEX status (dzVents)

Posted: Sunday 08 March 2020 22:36
by OedzesG
Please share.

oke EdwinK... here we go..

The script uses the Tautulli api.. so first you need to install Tautulli..

Code: Select all

https://github.com/Tautulli/Tautulli-Wiki/wiki/Installation
Once tautulli is instaled you can go to settings and navigate to Notification Agents and create a "webhook'
* webhook url :

Code: Select all

http://<domoticzURL + port>/json.htm?type=command&param=customevent&event=plex_woonkamer
( you can change: woonkamer to something else, but then in the script you need to change the custom event in exactly the same name)
(becouse we use an customEvent, you need to be on dzVents: 3:0:0!!)

* Webhook Method: post
* description: what ever you want

on the page: Triggers select:
* Playback Start
* Playback Stop
* Playback Pause
* Playback Resume

On the page: Conditions:
* Parameter: Player
* Operator: is
* Value: (Here you have to search in Plex for youre plex player name (in my case: SHIELD Android TV)

Then save the webhook.
We also need the API Key from tautulli, you can find it: settings/Web Interface, scrol down en note the API. Also make sure API is enabled.
Now were done in Tautulli.

In Domoticz you have to create a dummy device... first switch and then change it to: media player in Domoticz UI.

The script whit some final instructions.

Code: Select all

		local scriptVersion = ' release: v1.3: 20200306 '
		local scriptVar = ' plex woonkamer ' .. scriptVersion
		
	--<[================================================================================================]>--	
	--<[ 				      dzVents: Plex status 2 Domoticz   			    ]>--
	--<[================================================================================================]>-
						
		local tautulli_ip = 'xxx'	-- tautulli ip:port 
		local tautulli_api = 'xx'	-- tautulli api key 
		
		local plex_player	= 'SHIELD Android TV' 		-- the plex player you want the status off				
		local dz_plex_player 	= 26				-- IDX number plex media player device 

		
	return 
		{
			on = { 	customEvents 	= { 'plex_woonkamer',},		-- if you change the webhook URL make sure this match 
				timer		= { 'every minute'  		-- this only runs when a movie is playing on selected player		
		},
	       		httpResponses 		= { scriptVar .. '*', },
		},
			logging	 		= { level 	= domoticz.LOG_DEBUG,	-- set to LOG_ERROR when testen and ok. 
						    marker 	= scriptVar,
		},
			data 			= { plex	= { initial = {}, },		
		},
			
   
	execute = function(dz, item, triggerInfo)
   	
		local jsonPlexInfo = {}
		local callback = scriptVar .. 'getPlexInfo'
		
		-- make sure timer only trigger script when pms is active 
		if item.isCustomEvent then 
			dz.data.plex.active = 'true' 
		end 
		
		if dz.data.plex.active == 'true' then 
			if item.isCustomEvent or item.isTimerer then 
	    end 
		
		-- get plex information	trough tautulli api 	
			local function getPlexInfo( target, json, callback)
            local url = 'http://' .. target .. '/' .. json   
            dz.openURL
			
			({ 
               			url = url, 
				method = 'GET', 
				callback = callback,
            		})
        
		end
		
		if item.isCustomEvent or item.isTimer then
			
		getPlexInfo( tautulli_ip, '/api/v2?apikey=' .. tautulli_api .. '&cmd=get_activity', callback )
		
			return
		end 

		 if item.isHTTPResponse then
            
		jsonPlexInfo = dz.utils.fromJSON(item.data)
            	if not(jsonPlexInfo) then return end
		
		else 
            		dz.log('Unexpected result from API ',dz.LOG_ERROR )
                dz.utils.dumpTable(item)
        end 

		dz.data.initialize('plex') 

		-- find match in player name (api showing more players when more streams are active) 
		for _, record in ipairs(jsonPlexInfo.response.data.sessions) do
				
			if record.player:match(plex_player) then 
				
					dz.data.plex.active = ('true')   
					dz.data.plex.state 	= (record.state) 
					dz.data.plex.title	= (record.full_title) 
					dz.data.plex.progress = (record.progress_percent)
										
				end 			
			end 
			
		-- Change json output for updating device 	
		if dz.data.plex.state == 'playing' then player_state = 7
			elseif dz.data.plex.state == 'buffering' then player_state = 7
			elseif dz.data.plex.state == 'paused' then player_state = 2
									
		end 
		
		-- updating device 
		if dz.data.plex.active == 'true' then 
		dz.devices(dz_plex_player).update(player_state, dz.data.plex.title .. ' : ' .. dz.data.plex.progress .. ' %').silent() 
			
		else 
			
		dz.devices(dz_plex_player).update(0, 'no media playing').silent() 

	     end 
	end 
   end 
}

Re: PLEX status (dzVents)

Posted: Sunday 08 March 2020 22:51
by EdwinK
Trying this tomorrow ;)

Re: PLEX status (dzVents)

Posted: Sunday 08 March 2020 22:57
by OedzesG
Trying this tomorrow

oké.. 8-)

please let me know if evertything worked out!

Re: PLEX status (dzVents)

Posted: Monday 09 March 2020 11:13
by EdwinK
So far it's working:
Spoiler: show

Code: Select all

2020-03-09 11:07:41 - DEBUG :: Thread-25 : Tautulli ActivityHandler :: Session 4 started by user 1480083 (EdwinK) with ratingKey 3413 ('Allo 'Allo! - The Return of 'Allo 'Allo!).
2020-03-09 11:07:41 - DEBUG :: Thread-1 : Tautulli NotificationHandler :: Notifiers enabled for notify_action 'on_play'.
2020-03-09 11:07:41 - DEBUG :: Thread-1 : Tautulli NotificationHandler :: Checking global notification conditions.
2020-03-09 11:07:41 - DEBUG :: Thread-1 : Tautulli NotificationHandler :: Global notification conditions evaluated to 'True'.
2020-03-09 11:07:41 - DEBUG :: Thread-1 : Tautulli NotificationHandler :: Checking custom notification conditions for notifier_id 1.
2020-03-09 11:07:41 - DEBUG :: Thread-1 : Tautulli NotificationHandler :: {1} player | is | 'plex server' > 'tv ue50j6200' > False
2020-03-09 11:07:41 - DEBUG :: Thread-1 : Tautulli NotificationHandler :: Condition logic [blank]: {1} > False
2020-03-09 11:07:41 - DEBUG :: Thread-1 : Tautulli NotificationHandler :: Custom conditions evaluated to 'False'. Conditions: [False].
2020-03-09 11:07:41 - DEBUG :: Thread-1 : Tautulli NotificationHandler :: Custom notification conditions not satisfied, skipping notifier_id 1.
2020-03-09 11:08:47 - DEBUG :: Thread-25 : Tautulli ActivityHandler :: Session 4 paused.
2020-03-09 11:08:47 - DEBUG :: Thread-2 : Tautulli NotificationHandler :: Notifiers enabled for notify_action 'on_pause'.
2020-03-09 11:08:47 - DEBUG :: Thread-2 : Tautulli NotificationHandler :: Checking global notification conditions.
2020-03-09 11:08:47 - DEBUG :: Thread-2 : Tautulli NotificationHandler :: Global notification conditions evaluated to 'True'.
2020-03-09 11:08:47 - DEBUG :: Thread-2 : Tautulli NotificationHandler :: Checking custom notification conditions for notifier_id 1.
2020-03-09 11:08:47 - DEBUG :: Thread-2 : Tautulli NotificationHandler :: {1} player | is | 'plex server' > 'tv ue50j6200' > False
2020-03-09 11:08:47 - DEBUG :: Thread-2 : Tautulli NotificationHandler :: Condition logic [blank]: {1} > False
2020-03-09 11:08:47 - DEBUG :: Thread-2 : Tautulli NotificationHandler :: Custom conditions evaluated to 'False'. Conditions: [False].
2020-03-09 11:08:47 - DEBUG :: Thread-2 : Tautulli NotificationHandler :: Custom notification conditions not satisfied, skipping notifier_id 1.
                        
but nothing changes on the switch.

Re: PLEX status (dzVents)

Posted: Monday 09 March 2020 11:18
by OedzesG
but nothing changes on the switch.
can you share the script?

Re: PLEX status (dzVents)

Posted: Monday 09 March 2020 13:55
by EdwinK
The same script you posted above, but adapted
Spoiler: show

Code: Select all

		local scriptVersion = ' release: v1.3: 20200306 '
		local scriptVar     = ' plex woonkamer ' .. scriptVersion
		
	--<[================================================================================================]>--	
	--<[ 				      dzVents: Plex status 2 Domoticz   			    ]>--
	--<[================================================================================================]>-
						
		local tautulli_ip   = '192.168.0.11:8181'	                -- tautulli ip:port 
		local tautulli_api  = '****'	-- tautulli api key 
		
		local plex_player   = 'Plex Server' 		                -- the plex player you want the status off				
		local dz_plex_player= 508				                    -- IDX number plex media player device 

		
	return 
		{
			on = { 	customEvents 	= { 'plex_woonkamer',},		-- if you change the webhook URL make sure this match 
				timer		= { 'every minute'  		-- this only runs when a movie is playing on selected player		
		},
	       		httpResponses 		= { scriptVar .. '*', },
		},
			logging	 		= { level 	= domoticz.LOG_DEBUG,	-- set to LOG_ERROR when testen and ok. 
						    marker 	= scriptVar,
		},
			data 			= { plex	= { initial = {}, },		
		},
			
   
	execute = function(dz, item, triggerInfo)
   	
		local jsonPlexInfo = {}
		local callback = scriptVar .. 'getPlexInfo'
		
		-- make sure timer only trigger script when pms is active 
		if item.isCustomEvent then 
			dz.data.plex.active = 'true' 
		end 
		
		if dz.data.plex.active == 'true' then 
			if item.isCustomEvent or item.isTimerer then 
	    end 
		
		-- get plex information	trough tautulli api 	
			local function getPlexInfo( target, json, callback)
            local url = 'http://' .. target .. '/' .. json   
            dz.openURL
			
			({ 
               			url = url, 
				method = 'GET', 
				callback = callback,
            		})
        
		end
		
		if item.isCustomEvent or item.isTimer then
			
		getPlexInfo( tautulli_ip, '/api/v2?apikey=' .. tautulli_api .. '&cmd=get_activity', callback )
		
			return
		end 

		 if item.isHTTPResponse then
            
		jsonPlexInfo = dz.utils.fromJSON(item.data)
            	if not(jsonPlexInfo) then return end
		
		else 
            		dz.log('Unexpected result from API ',dz.LOG_ERROR )
                dz.utils.dumpTable(item)
        end 

		dz.data.initialize('plex') 

		-- find match in player name (api showing more players when more streams are active) 
		for _, record in ipairs(jsonPlexInfo.response.data.sessions) do
				
			if record.player:match(plex_player) then 
				
					dz.data.plex.active = ('true')   
					dz.data.plex.state 	= (record.state) 
					dz.data.plex.title	= (record.full_title) 
					dz.data.plex.progress = (record.progress_percent)
										
				end 			
			end 
			
		-- Change json output for updating device 	
		if dz.data.plex.state == 'playing' then player_state = 7
			elseif dz.data.plex.state == 'buffering' then player_state = 7
			elseif dz.data.plex.state == 'paused' then player_state = 2
									
		end 
		
		-- updating device 
		if dz.data.plex.active == 'true' then 
		dz.devices(dz_plex_player).update(player_state, dz.data.plex.title .. ' : ' .. dz.data.plex.progress .. ' %').silent() 
			
		else 
			
		dz.devices(dz_plex_player).update(0, 'no media playing').silent() 

	     end 
	end 
   end 
}

Re: PLEX status (dzVents)

Posted: Monday 09 March 2020 18:46
by OedzesG
The same script you posted above, but adapted
you sure youre plex_player is: Plex Server?

Normaly its something like: Chrome, Chromecast, SHIELD Android TV, or something..

if youre not sure, start streaming with the device you want to know the status of, then go to plex en check: Activity.
stream.PNG
stream.PNG (190.56 KiB) Viewed 9377 times
In this example it: "Chrome" you need!

Changed the script a litlle, this should be working!

Code: Select all

	local scriptVersion = ' v1.3: 20200306 '
	local scriptVar = ' plex woonkamer ' .. scriptVersion
		
	--<[================================================================================================]>--	
	--<[ 				dzVents: Plex status 2 Domoticz					    ]>--
	--<[================================================================================================]>-
						
		local tautulli_ip 	= '192.168.0.11:8181'
		local tautulli_api 	= '****'
		
		local plex_player	= 'Plex Server' 				
		local dz_plex_player 	= 508
	
		
	return 
		{
			on 		= { customEvents 	= { 'plex_woonkamer',},
			timer		= { 'every minute'  			
		},
	       		httpResponses	= { scriptVar .. '*', },
		},
			logging		= { level 		= domoticz.LOG_ERROR,
					    marker 		= scriptVar,
		},
			data		= { plex		= { initial = {}, },		
		},
			
   
	execute = function(dz, item, triggerInfo)
   
   
		local jsonPlexInfo = {}
		local callback = scriptVar .. 'getPlexInfo'
		
		if item.isCustomEvent then 
			dz.data.plex.active = 'true' 
		end 
		
		if dz.data.plex.active == 'true' then 
			if item.isCustomEvent or item.isTimerer then 
	    	end 
		
				
		local function getPlexInfo( target, json, callback)
            		local url = 'http://' .. target .. '/' .. json   
            		
		dz.openURL
			
			({ 
                		url = url, 
				method = 'GET', 
				callback = callback,
            		})
        
		end
		
		if item.isCustomEvent or item.isTimer then
			
		getPlexInfo( tautulli_ip, '/api/v2?apikey=' .. tautulli_api .. '&cmd=get_activity', callback )
		
			return
		end 

		 if item.isHTTPResponse then
            
		jsonPlexInfo = dz.utils.fromJSON(item.data)
            	if not(jsonPlexInfo) then return end
		
		else 
            	dz.log('Unexpected result from API ',dz.LOG_ERROR )
                	dz.utils.dumpTable(item)
        	end 

		dz.data.initialize('plex') 

		for _, record in ipairs(jsonPlexInfo.response.data.sessions) do
				
		if record.player:match(plex_player) then 
			
				dz.data.plex.active = ('true')   
				dz.data.plex.state 	= (record.state) 
				dz.data.plex.title	= (record.full_title) 
				dz.data.plex.progress = (record.progress_percent)
										
			end 			
		end 
				
		if dz.data.plex.state == 'playing' then dz.data.plex.state = 7
			elseif dz.data.plex.state == 'buffering' then dz.data.plex.state = 7
			elseif dz.data.plex.state == 'paused' then dz.data.plex.state = 2
		end 
		
		if dz.data.plex.active == 'true' then 
		dz.devices(dz_plex_player).update(dz.data.plex.state, dz.data.plex.title .. ' : ' .. dz.data.plex.progress .. ' %').silent() 
			
		else 
			
		dz.devices(dz_plex_player).update(0, 'no media playing').silent() 

	    end 
	end 
    end 
}

Re: PLEX status (dzVents)

Posted: Monday 09 March 2020 22:58
by EdwinK
Got the following:
Screen Shot 2020-03-09 at 22.55.56.png
Screen Shot 2020-03-09 at 22.55.56.png (138.11 KiB) Viewed 9365 times
So I assume it should be TV UE50J6200?

Re: PLEX status (dzVents)

Posted: Tuesday 10 March 2020 6:52
by OedzesG
So I assume it should be TV UE50J6200?
exactly

Re: PLEX status (dzVents)

Posted: Tuesday 10 March 2020 10:38
by OedzesG
So I assume it should be TV UE50J6200
also make sure youre webhook settings match...
webhook Tautullia.PNG
webhook Tautullia.PNG (25.01 KiB) Viewed 9345 times

Re: PLEX status (dzVents)

Posted: Tuesday 10 March 2020 10:50
by EdwinK
That was the one part I forgot to change ;)

Re: PLEX status (dzVents)

Posted: Tuesday 10 March 2020 10:58
by EdwinK
And now i've got it working ;)

Thanks. Maybe you could put this in the WIKI?

Re: PLEX status (dzVents)

Posted: Tuesday 10 March 2020 11:45
by OedzesG
And now i've got it working
i hope you enjoy it! :)

im not an scripting expert... just create the script (whit lots of help from @waaren) becouse i could not find anything in dzVents for plex..

i think there can be some aprovements and have some ideas to add..

(to be honest, i have no clue how to place it on the wiki :lol: )

Re: PLEX status (dzVents)

Posted: Monday 06 April 2020 14:47
by finalcut62
What a great script. I had to update my Raspberry PI and Domoticz for it (which turned to be much easier than expected - previous versions I had to compile myself because of desired OpenZwave support), and it worked straight out of the box.

Do you all have any examples of attached Lighting-scripts? My preference was to dim a group of Hue lights to 0 (matching a cinema experience) but I'll settle of 'off' for 'playing' and 'resume', and for 'on' at all other events...

Re: PLEX status (dzVents)

Posted: Saturday 11 April 2020 23:17
by OedzesG
What a great script
thnx! @finalcut62

Code: Select all

 local mediaPlayerStates =
    	{ 	
    		Off = 0,
    		On = 1,
    		Paused = 2,
    		Stopped = 3,
    		Video = 4,
    		Audio = 5,
    		Photo = 6,
    		Playing = 7,
    		Disconnected = 8, 
    		Sleeping = 9, 
    		Unknown = 10,
    	} 
In the script: 0,2,7 used..

Re: PLEX status (dzVents)

Posted: Thursday 07 May 2020 22:48
by dennis075
Script looks great, trying it tomorrow. Is it correct that you need the plex pass to let this work? That's something to keep in mind when trying this :D

Re: PLEX status (dzVents)

Posted: Thursday 07 May 2020 23:26
by OedzesG
Is it correct that you need the plex pass to let this work?
No script is not using PLEX but Tautulli.. and as far i know, you can run Tautulli withoud PLEX pass.

Good luck!
Please let me know if everything went well...

Re: PLEX status (dzVents)

Posted: Friday 08 May 2020 11:42
by dennis075
OedzesG wrote: Thursday 07 May 2020 23:26
Is it correct that you need the plex pass to let this work?
No script is not using PLEX but Tautulli.. and as far i know, you can run Tautulli withoud PLEX pass.

Good luck!
Please let me know if everything went well...
Installed everything and works pretty good! And indeed no plex pass needed.