PLEX status (dzVents)  [SOLVED]

Moderator: leecollings

Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: PLEX status (dzVents)

Post by Number8 »

I consider it as a failure of the RFC! I'm not responsible if a French word contains such a character ;)
First option is not possible since a placeholder is used in the app emitting the webhook.
So remains 2nd option that I will test. Thanks a lot for the tip.
Just FYI, the same remote app offers to send messages to a telegram bot and telegram displays correctly the single quote in the received message. You might consider not applying strictly the RFC.
Debian buster on NUC and three RPi with buster.
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: PLEX status (dzVents)

Post by Number8 »

I can't get it working because as soon as it enters the code, dzVents throw the error and code execution is stopped. It just have the time to log item.data and then the exception is thrown. As you see, item.data reports correctly the single quote

Code: Select all

2020-06-06 11:42:50.286 Status: dzVents: Info: plex script: ------ Start external script: plex.lua: Custom event: "plex" 
2020-06-06 11:42:50.286 Status: dzVents: Info: plex script: item data is: {"action": "resume", "titre": "L'Arnaque"} 
2020-06-06 11:42:50.286 Status: dzVents: Info: plex script: ------ Finished plex.lua 
2020-06-06 11:42:50.286 Error: dzVents: Error: (3.0.9) plex script: Error parsing json to LUA table: /home/pi/domoticz/scripts/dzVents/../lua/JSON.lua:1234: /home/pi/domoticz/scripts/dzVents/../lua/JSON.lua:808: expected comma or '}' at byte 34 of: {"action": "resume", "titre": "L"Arnaque"}
Debian buster on NUC and three RPi with buster.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: PLEX status (dzVents)

Post by waaren »

Number8 wrote: Saturday 06 June 2020 11:48 I can't get it working because as soon as it enters the code, dzVents throw the error and code execution is stopped.
Can you show me the script as this should not happen and in fact in one of your previous posted log you already proved that the script will not abort when the JSON conversion fails. (it showed the dump)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: PLEX status (dzVents)

Post by Number8 »

waaren wrote: Saturday 06 June 2020 12:00
Number8 wrote: Saturday 06 June 2020 11:48 I can't get it working because as soon as it enters the code, dzVents throw the error and code execution is stopped.
Can you show me the script as this should not happen and in fact in one of your previous posted log you already proved that the script will not abort when the JSON conversion fails. (it showed the dump)
Sure. Even if I comment everything starting at "if item.isCustomEvent then" the exception is thrown

Code: Select all

return {
	active = true, -- optional
	on = { 
		customEvents = {"plex"}
	},

--	data = { 
--		sid = { initial = "" }
--	},

	logging = {
		level = domoticz.LOG_INFO,
		marker = "plex script"
	},

	execute = function(dz, item)
		local _u = dz.utils
		local _h = dz.helpers
		local _d = dz.data
		local _ = dz.utils._
		
		if item.isCustomEvent then
			if item.trigger == "plex" then
				dz.log ("item data is: " .. item.data)
				if not(item.json) then
					dz.log ('JSON KO')
				  	local data  = dz.utils.fromJSON(item.data:gsub("@","\\u0027"))
				else
					local data = item.json
				end
				dz.log ("data after processing: " .. data)
			end
		end
	end
}
Debian buster on NUC and three RPi with buster.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: PLEX status (dzVents)

Post by waaren »

Number8 wrote: Saturday 06 June 2020 12:33 I can't get it working because as soon as it enters the code, dzVents throw the error and code execution is stopped.
I left a wrong char in my code snippet. That was probably the reason you did not get the expected result.

If I send this curl

Code: Select all

#curl -d '{"action": "resume", "titre": "L'\''Arnaque"}' "http://nuc:8084/json.htm?type=command&param=customevent&event=plex"
to this script

Code: Select all

return
{
    on =
    {
        customEvents =
        {
            'plex'
        },
    },


    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'plex script'
    },

    execute = function(dz, item)
        if item.isCustomEvent and item.trigger == "plex" then

            dz.utils.dumpTable(item)

            if not(item.json) then
                dz.log('Extra conversion needed.....' ,dz.LOG_DEBUG)
                  item.json = dz.utils.fromJSON(item.data:gsub("'","\\u0027"))
                  item.isJON = item.json ~= nil
            end

            dz.utils.dumpTable(item.json)
        end
    end
}
I get this in the log

Code: Select all

2020-06-06 13:41:36.125  Status: dzVents: Info: plex script: ------ Start internal script: 20200606 dz customEvent single quote: Custom event: "plex"
2020-06-06 13:41:36.125  Error: dzVents: Error: (3.0.9) plex script: Error parsing json to LUA table: /opt/domoticz/scripts/dzVents/../lua/JSON.lua:1234: /opt/domoticz/scripts/dzVents/../lua/JSON.lua:808: expected comma or '}' at byte 34 of: {"action": "resume", "titre": "L"Arnaque"}
2020-06-06 13:41:36.125  Status: dzVents: > isVariable: false
2020-06-06 13:41:36.126  Status: dzVents: > isScene: false
2020-06-06 13:41:36.126  Status: dzVents: > isDevice: false
2020-06-06 13:41:36.126  Status: dzVents: > isXML: false
2020-06-06 13:41:36.126  Status: dzVents: > status: info
2020-06-06 13:41:36.126  Status: dzVents: > dump()
2020-06-06 13:41:36.126  Status: dzVents: > isTimer: false
2020-06-06 13:41:36.126  Status: dzVents: > isHTTPResponse: false
2020-06-06 13:41:36.126  Status: dzVents: > type: customEvent
2020-06-06 13:41:36.126  Status: dzVents: > isGroup: false
2020-06-06 13:41:36.126  Status: dzVents: > isSecurity: false
2020-06-06 13:41:36.126  Status: dzVents: > isSystem: false
2020-06-06 13:41:36.126  Status: dzVents: > isCustomEvent: true
2020-06-06 13:41:36.126  Status: dzVents: > customEvent: plex
2020-06-06 13:41:36.126  Status: dzVents: > isHardware: false
2020-06-06 13:41:36.126  Status: dzVents: > isJSON: false
2020-06-06 13:41:36.126  Status: dzVents: > data: {"action": "resume", "titre": "L'Arnaque"}
2020-06-06 13:41:36.126  Status: dzVents: > message:
2020-06-06 13:41:36.126  Status: dzVents: > trigger: plex
2020-06-06 13:41:36.126  Status: dzVents: Debug: plex script: Extra conversion needed.....
2020-06-06 13:41:36.126  Status: dzVents: > action: resume
2020-06-06 13:41:36.126  Status: dzVents: > titre: L'Arnaque
2020-06-06 13:41:36.127  Status: dzVents: Info: plex script: ------ Finished 20200606 dz customEvent single quote
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: PLEX status (dzVents)

Post by Number8 »

With this new code, the exception is still thrown just after the dump table instruction. Same exception even if all the code is commented. I copied/pasted your last code, a typo error has been corrected (item.isJSON = item.json ~= nil)

Code: Select all

        if item.isCustomEvent and item.trigger == "plex" then
           dz.utils.dumpTable(item)
            if not(item.json) then
                dz.log('Extra conversion needed.....' ,dz.LOG_DEBUG)
                  item.json = dz.utils.fromJSON(item.data:gsub("'","\\u0027"))
                  item.isJSON = item.json ~= nil
            end
            dz.utils.dumpTable(item.json)
        end
Here is the log.

Code: Select all

2020-06-06 16:11:18.128 Status: dzVents: Info: plex script: ------ Start external script: plex.lua: Custom event: "plex" 
2020-06-06 16:11:18.128 Status: dzVents: > isDevice: false 
2020-06-06 16:11:18.128 Status: dzVents: > isSystem: false 
2020-06-06 16:11:18.128 Status: dzVents: > isGroup: false 
2020-06-06 16:11:18.128 Status: dzVents: > isCustomEvent: true 
2020-06-06 16:11:18.128 Status: dzVents: > customEvent: plex 
2020-06-06 16:11:18.128 Status: dzVents: > data: {"action": "pause", "titre": "L'Arnaque"} 
2020-06-06 16:11:18.128 Status: dzVents: > dump() 
2020-06-06 16:11:18.128 Status: dzVents: > trigger: plex 
2020-06-06 16:11:18.128 Status: dzVents: > isHTTPResponse: false 
2020-06-06 16:11:18.128 Status: dzVents: > status: info 
2020-06-06 16:11:18.128 Status: dzVents: > message: 
2020-06-06 16:11:18.128 Status: dzVents: > isScene: false 
2020-06-06 16:11:18.128 Status: dzVents: > type: customEvent 
2020-06-06 16:11:18.128 Status: dzVents: > isXML: false 
2020-06-06 16:11:18.128 Status: dzVents: > isVariable: false 
2020-06-06 16:11:18.128 Status: dzVents: > isSecurity: false 
2020-06-06 16:11:18.128 Status: dzVents: > isTimer: false 
2020-06-06 16:11:18.128 Status: dzVents: > isJSON: false 
2020-06-06 16:11:18.128 Status: dzVents: > isHardware: false 
2020-06-06 16:11:18.128 Status: dzVents: Debug: plex script: Extra conversion needed..... 
2020-06-06 16:11:18.128 Status: dzVents: > action: pause 
2020-06-06 16:11:18.128 Status: dzVents: > titre: L'Arnaque 
2020-06-06 16:11:18.128 Status: dzVents: Info: plex script: ------ Finished plex.lua 
2020-06-06 16:11:18.128 Error: dzVents: Error: (3.0.9) plex script: Error parsing json to LUA table: /home/pi/domoticz/scripts/dzVents/../lua/JSON.lua:1234: /home/pi/domoticz/scripts/dzVents/../lua/JSON.lua:808: expected comma or '}' at byte 33 of: {"action": "pause", "titre": "L"Arnaque"} 
Debian buster on NUC and three RPi with buster.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: PLEX status (dzVents)

Post by waaren »

Number8 wrote: Saturday 06 June 2020 16:22 With this new code, the exception is still thrown just after the dump table instruction. Same exception even if all the code is commented. I copied/pasted your last code, a typo error has been corrected (item.isJSON = item.json ~= nil)
item.isJSON = item.json ~= nil
The point that I wanted to emphasize with this example script is that the error message you see does not stop the execution of the script.
The error message is correct and expected because a JSON string with a single quote in it is invalid. But this does not stop the script.

What you see as a typo is in fact a valid Lua statement. item.isJSON will be set to true if item.json is unequal to nil.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: PLEX status (dzVents)

Post by Number8 »

What you see as a typo is in fact a valid Lua statement. item.isJSON will be set to true if item.json is unequal to nil.
Go it
The point that I wanted to emphasize with this example script is that the error message you see does not stop the execution of the script.
The error message is correct and expected because a JSON string with a single quote in it is invalid. But this does not stop the script.
Go it as well. I was puzzled by this error and thought mistakenly that execution was stopped, which is indeed wrong. Thanks to your tip, it is working as expected.
Again, thanks a lot for the support of POST and your very kind support.
Debian buster on NUC and three RPi with buster.
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: PLEX status (dzVents)

Post by simon_rb »

This is just what I’ve been looking for, I’ve just set it up and getting this error? Anything obvious that I’ve done wrong?

Thank you

Code: Select all

 TTPResponse: " plex woonkamer release: v1.3: 20200306 getPlexInfo"
2020-11-11 18:24:01.597 Status: dzVents: Info: plex woonkamer release: v1.3: 20200306 : ------ Finished Plex.lua
2020-11-11 18:24:01.433 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_sqhomepod_volume.lua: ...pi/domoticz/scripts/lua/script_time_sqhomepod_volume.lua:10: attempt to index a nil value (global 'deviceinfo')
2020-11-11 18:24:01.596 Error: dzVents: Error: (3.0.16) plex woonkamer release: v1.3: 20200306 : HTTP/1.1 response: 404 ==>> Not Found
2020-11-11 18:24:01.596 Error: dzVents: Error: (3.0.16) plex woonkamer release: v1.3: 20200306 : Error parsing json to LUA table: (invalid json string) <!DOCTYPE html PUBLIC
2020-11-11 18:24:01.596 "-//W3C//DTD XHTML 1.0 Transitional//EN"
2020-11-11 18:24:01.596 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2020-11-11 18:24:01.596 <html>
2020-11-11 18:24:01.596 <head>
2020-11-11 18:24:01.596 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
2020-11-11 18:24:01.596 <title>404 Not Found</title>
2020-11-11 18:24:01.596 <style type="text/css">
2020-11-11 18:24:01.596 #powered_by {
2020-11-11 18:24:01.596 margin-top: 20px;
2020-11-11 18:24:01.596 border-top: 2px solid black;
2020-11-11 18:24:01.596 font-style: italic;
2020-11-11 18:24:01.596 }
2020-11-11 18:24:01.596
2020-11-11 18:24:01.596 #traceback {
2020-11-11 18:24:01.596 color: red;
2020-11-11 18:24:01.596 }
2020-11-11 18:24:01.596 </style>
2020-11-11 18:24:01.596 </head>
2020-11-11 18:24:01.596 <body>
2020-11-11 18:24:01.596 <h2>404 Not Found</h2>
2020-11-11 18:24:01.596 <p>The path '/v2' was not found.</p>
2020-11-11 18:24:01.596 <pre id="traceback"></pre>
2020-11-11 18:24:01.596 <div id="powered_by">
2020-11-11 18:24:01.596 <span>
2020-11-11 18:24:01.596 Powered by <a href="http://www.cherrypy.org">CherryPy unknown</a>
2020-11-11 18:24:01.596 </span>
2020-11-11 18:24:01.596 </div>
2020-11-11 18:24:01.596 </body>
2020-11-11 18:24:01.596 </html>
2020-11-11 18:24:01.596
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: PLEX status (dzVents)

Post by Number8 »

What version of dzVents are you running?
Debian buster on NUC and three RPi with buster.
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: PLEX status (dzVents)

Post by simon_rb »

Number8 wrote: Wednesday 11 November 2020 19:33 What version of dzVents are you running?
3.0.16
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: PLEX status (dzVents)

Post by Number8 »

Sorry version was in your last post. But what is your code. I share mine

Code: Select all

-- Tautulli webhook will be triggered only if player is HTPC (Plex media player or Plex player) or Shield
-- See Webhooks setup in Tautulli
return {
	active = true, -- optional
	on = { 
		customEvents = {"plex"}
	},

	logging = {
		level = domoticz.LOG_DEBUG,
		marker = "plex script"
	},

	execute = function(dz, item)
		local _u = dz.utils
		local _h = dz.helpers
		local _d = dz.data
		local _ = dz.utils._

		local Time = require('Time')
	 	local currentTime = Time()
		
		if item.isCustomEvent and item.trigger == "plex" then
--         dz.utils.dumpTable(item)
			dz.log("plex event")
			-- dz.log ("item data is: " .. item.data)
-- Single quote in a JSON string is not supported. This will NOT prevent the LUA exception.
         if not(item.json) then
             dz.log('Extra conversion needed.....' ,dz.LOG_DEBUG)
             item.json = dz.utils.fromJSON(item.data:gsub("'","\\u0027"))
             item.isJON = item.json ~= nil
         end

--			dz.log (item.json.title,dz.LOG_DEBUG)

				if item.json.action == "resume" then
					dz.log ("resumed")
					dz.devices(258).switchOff().checkFirst()  -- switch off Oeuf
				elseif item.json.action == "play" then
					dz.log ("play")
					dz.devices(65).switchOff() -- switch off all lights
				elseif item.json.action == "stop" then
					dz.log ("stopped")
					if (currentTime.matchesRule("between 1 minutes after civiltwilightend and 1 minutes before civiltwilightstart")) then
						dz.devices(44).dimTo(50) -- switch on Lustre
						dz.devices(314).switchOn().checkFirst()  -- switch on lampadaire
					end
					-- dz.devices(258).switchOn().checkFirst()  -- switch on Oeuf
				elseif item.json.action == "pause" then
					dz.log ("just paused")
					if (currentTime.matchesRule("between 1 minutes after sunset and 1 minutes before sunrise")) then
						dz.devices(258).switchOn().checkFirst()  -- switch on Oeuf
					end
				elseif item.json.action == "endOfShow" then
					dz.log ("Temps restant : " .. item.json.remainingTime)
--					if (currentTime.matchesRule("between 1 minutes after civiltwilightend and 1 minutes before civiltwilightstart")) then
					if (currentTime.matchesRule("between civiltwilightend and civiltwilightstart")) then
						dz.devices(44).dimTo(50)  -- switch on Lustre
					end
				end
			-- end
		end
	end
}

Debian buster on NUC and three RPi with buster.
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: PLEX status (dzVents)

Post by simon_rb »

This is my Plex.lua file

Code: Select all

 local scriptVersion = ' release: v1.3: 20200306 '
		local scriptVar = ' plex woonkamer ' .. scriptVersion
		
	--<[================================================================================================]>--	
	--<[ 				      dzVents: Plex status 2 Domoticz   			    ]>--
	--<[================================================================================================]>-
						
		local tautulli_ip = '127.0.0.1:8181'	-- tautulli ip:port 
		local tautulli_api = '92d29923e8be4757ab9ed9bf010112c5'	-- tautulli api key 
		
		local plex_player	= 'Living Room' 		-- the plex player you want the status off				
		local dz_plex_player 	= 1253				-- 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 
}
DarkG
Posts: 89
Joined: Friday 15 September 2017 18:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10076
Location: Germany
Contact:

Re: PLEX status (dzVents)

Post by DarkG »

OedzesG wrote: Sunday 08 March 2020 22:36
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 
}

There is a syntax error at get plex info. one slash too much before api. must be
getPlexInfo( tautulli_ip, 'api/v2?apikey=' .. tautulli_api .. '&cmd=get_activity', callback )
RPi4 Shelly1 Shelly2.5 ESPEasy Tuya Domoticz Beta Dashticz 3.6
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: PLEX status (dzVents)

Post by simon_rb »

DarkG wrote: Thursday 12 November 2020 0:32 There is a syntax error at get plex info. one slash too much before api. must be
getPlexInfo( tautulli_ip, 'api/v2?apikey=' .. tautulli_api .. '&cmd=get_activity', callback )
Thank you! Also noticed a space with my api key. All working now

Thanks again!
simon_rb
Posts: 612
Joined: Wednesday 07 August 2013 19:09
Target OS: -
Domoticz version:
Location: UK
Contact:

Re: PLEX status (dzVents)

Post by simon_rb »

Has anyone managed to get the newly created Plex Switch to control lights when state changes between Playing and Paused? I’ve tried LUA and blocky and the scripts run fine but the light switches don’t change.
OedzesG
Posts: 106
Joined: Monday 11 March 2019 0:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: PLEX status (dzVents)

Post by OedzesG »

Has anyone managed to get the newly created Plex Switch to control lights when state changes between Playing and Paused? I’ve tried LUA and blocky and the scripts run fine but the light switches don’t change.

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,
    	}
Sarcas
Posts: 86
Joined: Wednesday 11 October 2017 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1ß
Location: Friesland
Contact:

Re: PLEX status (dzVents)

Post by Sarcas »

Finally got it working, but not until I changed

Code: Select all

if record.player:match(plex_player) then


to

Code: Select all

if record.player == plex_player then
I'm glad it works, but can't stand not knowing what is wrong. Does anyone know?

My player:

Code: Select all

local plex_player	= 'LG OLED55E7N-Z'
Thx!
--

Domoticz on rPi4 - RFXCOM RFXtrx433 USB - ZW090 Z-Stick Gen5 EU - IKEA Tradfri - Philips HUE - YouLess meter - SolarEdge
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests