[OBSOLETE] Plex Home Theatre Manager SmartApp

Depending on how technically competent you are, I think you could use CoRE…

sendEvent(name: “trackDescription”, value: text)

The trackDescription event should* be readable by CoRE I think, you would need to put this in to a variable to send in an SMS…

There is a CoRE thread specifically for things like this… they should be able to help… I don’t currently have this app running as I use the other method for reporting state…

The easiest way is to use PlexPy and a service such as Pushover. PlexPy is simply amazing. Gives full stats on what has been watched by who, how many times it’s been run etc and keeps all historic data. Not hard to setup - I have PlexPy running on my ReadyNas and Pushiver on my phone so I get instant notifications through.

The only thing is PlexPy doesn’t work on the Plex Cloud beta at the moment

1 Like

I just started using Core. I’m not at all familiar with this at the moment, but it sounds like it would solve my problem.

Are you saying that I need to send the trackDescription variable to Core or can Core already access the trackDescription variable and I just need to call it in my SMS message?

Also, if you could guide me to the page you were talking about, that would be helpful for me to learn. Thanks.

My suggestion would be to post in the CoRE help thread as they will be able to be more specific … but in principle you would setup a piston that says when state changes to playing, get the trackDescription (I expect called as trackDescriptionState.value) from the device and save to a custom variable and then Send an SMS to X including the custom variable in the content… one trigger and two actions…

I’ve not done it myself but I did do something in the past similar…

@Brian_Peterson honestly give PlexPy a go. If you have a PMS server running all the time you should be able to install it. Why reinvent the wheel when a much better alternative is out there. I love using ST but it’s a case of using the right tool for the job

Yeah, I have to admit that my current test code is not live in my public GitHub since I didn’t want to risk that someone grab it and it not work for them like I said here that it did. I did add code to create a Token for the Controller that I pass along. I have looked at the subscribe thing a little more today and noticed that I need to send a port number for it to post responses to, which I am trying to figure out blind for now if ST supports that. I will try soon to see if I just get an HTTP response immediately that I can parse or if I need to use “/player/timeline/poll?wait=0” in order to get instant feedback, but the polling every <=10 seconds and incrementing command numbers will be another challenge.

1 Like

Okay… I just installed PlexPy. I’m now messing around with the notification options. It looks like I’ll get there. Thanks for the help!

1 Like

@Entityxenon - There is a lot to clean up (settings, incrementing commandID, states, logging, etc) and the timeline parsing needs to come next, but I did get commands working and subscriptions are working. This is working now on both my Apple TV 4 and my PMP install. I get the same kind of returns from each, we just need to parse what returns. The navigation keys are weird, but are “working” so I need to log stalk the official remote again to see how they set focus elements. I don’t see me having time to do more on this for a week or more, and it isn’t clean for distribution. I’m really stumped about why the commandID doesn’t increment, but once it does we need to implement a rollover point.

TRY AT YOUR OWN RISK!
Device Handler
SmartApp

Nice! I’m over the other side of the UK at the moment, so wont get back until late tonight, but will see if I can take a look tomorrow evening! Sounds like it’s getting close to being complete too, great work!

for commandID instead of using state, you may have more luck referencing atomicState, as state isn’t an immediate write, so can get out of sync, atomicState puts it in to memory and only commits on completion…

E.g. use the below to increment…

atomicState.commandID += 1

and you will need to update any reference to state to atomicState…

I’ll take a look tomorrow and try to work out what your code is doing in more detail :slight_smile:

So I’ve only had a quick chance to have a go…

Roku doesn’t appear to be working…

And Plex HT seems to crash I think at the point it polls… but not certain…

I also got a java.lang.ClassCastException immediately after the below, but I haven’t had it since…

Plex Windows 10 app does appear to work for home and play/pause… but not for navigation…

Also I think you have broken status reporting, possibly with the device name change in the list…

[index:17, mac:6036DD08DFE5, ip:C0A8190A, port:0BBD, headers:[x-plex-client-identifier:9a199a5c-6037-4cca-b2e5-xxxxxxxxxxxxxx, content-length:76, http/1.1 200 ok:null, access-control-allow-origin:*, content-type:text/xml, date:Tue, 17 Jan 2017 21:55:30 GMT], body:<?xml version="1.0" encoding="utf-8" ?>
<Response code="200" status="OK" />
, header:HTTP/1.1 200 OK 
Content-Length: 76 
X-Plex-Client-Identifier: 9a199a5c-6037-4cca-b2e5-xxxxxxxxxxx
Content-Type: text/xml 
Access-Control-Allow-Origin: * 
Date: Tue, 17 Jan 2017 21:55:30 GMT, status:200, data:, xml:]

Also seeing an error in the DTH:

def lastState = device.currentState(‘switch’).getValue();

I think should be:

def lastState = device.currentState(‘switch’).value;

Sorry for the rambling… but just got back and quite tired… I might take another look tomorrow… but have a feeling it’s going to take me a while to work out how all your new code works! and may just be easier for you to work it out… but I may have time over the next couple of days if so I’ll give it a go, you’ve definitely done a lot to get where you have though!!

I think your crash and the cast exception are from the commandID issue. I was having similar issues when I was adding that in, and I had to force it to one once and now on my install it never leaves one. As for the status updates, I commented that out for now since the old logic was parsing /sessions/status and I would like to parse the returned structure instead. The system isn’t actually polling per se, I implemented what the official API says is needed which is to send the timeline request every 30 seconds. So, I get the Hubs local IP and Port number for it’s http server listener socket which I pass to the Player device in the timeline request. This allows that the Player can send me information everytime there is a change without me needing to go ask for it (if content is playing, it sends an update every second with the current point in the content for example). I found that the bad error message that I was getting back from PMP was because my Client ID was not a registered subscriber which went away now that I have a subscription socket that stays open. I need to get the CommandID incrementing so that I can tell what message/device the response was intended for since I am getting responses from all players in the SmartApp. Another thing that may be contributing to some clients not working is that the way the HubAction Headers work didn’t seem to be fully recognized across my hosts. You may see that I included a couple of the headers inline with the path setting now to overcome that. I am thinking that perhaps including them all directly instead of in the Headers collection will be the best bet since something does not seem to be working properly there. It appears that these commands can also be pointed at the server instead of the client, but I get an error in the log that it couldn’t open the return port when I try to go that way, which I think could just be that I need to setup a port forward in my router so that the ST Hub port number is available from a public IP. As for navigation “working” I meant that I see the client receives them and doesn’t throw an error, but they don’t seem to do anything as you likely saw as well (except Home does a kind of Stop action).

1 Like

Ahh interesting, I can see that being a much better approach and removes the polling from ST, which is not the most reliable process… I will try to get my head around it tonight!

I was able to address my personal issues faster than expected tonight, so I took some time to work on this.

I just updated my dropbox files, I think you [@Entityxenon] will be happy…
I fixed the incrementing, I fixed all the cast errors that I found, and it is now parsing the results to update the devices rather quickly.

Please test with your devices.

There is one opportunity that I am aware of: sometimes when a command occurs, I get two messages, one of the last state, and one of the new state. So if you press Pause, I will get Paused, Playing, Paused, Paused, etc. Each time I see this both messages are processed with the same timestamp, but I’m not sure how to filter out the “old” one from being processed.

If this looks good for your devices, the next thing that I would like to implement is taking actions based on the device states like you do in PlexPlus (so I get the instant results without the 10s polling the PC App does). It would be nice if you could listen to the state of the device as a master as an option in your device. I’d rather try this than play around with creating it all in CORE.

After that, on to getting the navigation stuff to really do something and perhaps trying to make the timeline subscription via the server instead of the devices / making it configurable.

UPDATE: I just tested my apple tv 4 and it receives the commands, but I’m not getting the state back properly, but I can’t look at the logs tonight to see why.

UPDATE2: I checked the logs and found that the AppleTV4 is returning error 500 that it could not connect back to the hub for some reason. I added a port forward in my router, but it is still not working.

1 Like

Great work! I haven’t had any time to look yet and not likely to until maybe Sunday… if I manage to get 10 minutes this evening I’ll give it a go though…

Could you not just ignore anything received that reflects the current state of the DTH?

So a few things with this…

I’m not sure what you mean about CoRE, but are you talking about the light control etc? If so this DTH should already work with PlexPlus, it works with the PC app or this DTH, in fact should work with any “multimedia” DTH, I designed it to be fairly open and I know some people are using the existing DTH with Plex Plus.

Also just for reference I have the PC App on a 1s poll with no detrimental impact, although the push aspect as part of this DTH should be far superior as will get rid of the need for an additional exe!

“Timeline subscription” in this instance meaning the device sending play state etc? I guess server would be better hopefully if only to ensure consistency… may also solve the below?

I assume both are on the same network? In which case you should not need to port forward, everything should have a LAN IP, and sent via a Hub Action rather than ST cloud? You may want to check you are communicating to the right IP in the first instance, some devices will carry a LAN and a WAN IP (2 entries at the same time - my code filtered out external IPs, but I expect it’s not being used anymore?). What’s actually returning the 500 error, if it’s the apple TV then this would imply that it is receiving the request, just doesn’t know what to do with it… you should get a 404 if not received at all…

Also if there is anything you want me to specifically look at anything I can do to help feel free to give me some home work!

I found that the polling timer was being cancelled by the system, so I replaced it with a CRON Job that has now run for over a day (updated both in DropBox). For the room with the PMP Install, I tried having PlexPlus read the status from this DTH and except that it catches the inherent glitching at status changes, it works fast and smooth!! I don’t have much test time on it, but I added a kind of 900ms timer to ignore updates to status after we just issued a status changing command to hopefully remove the glitch.

I am still hoping to hear back from someone if they have tried this on some other client and how that went.

I am looking to borrow a USB-C to USB-A cable so that I can try and pull logs from my Apple TV to [hopefully] see why it won’t accept my timeline connection requests.

1 Like

Great! I will give it a go tonight across a few devices and let you know how it goes… sorry didn’t have the chance at the weekend… are the Dropbox links above still the correct ones to use?

Just installing from drop box and using the normal filtered list…

I’m seeing my Samsung TV as “-Plex for Samsung” I don’t think these hold a name, however I have two, so I should see two entries (which I think I did on the old app but will check later)…

I also think I should have two entries here, but I’m only seeing the first… but I just need to see if that is because I changed my laptop name as the old entry is there too…:

laptop-Plex for Windows
laptop-Plex Home Theatre

Will report properly later…

I added the suffix so I knew which instance of the program the connection and command was going to on that Player. Like you, my PC Users have multiple instances (Chrome, Win App, PMP) and a list of the pc name with just numbers after it was not helpful. Also, I had a blank in the list before I added those with the previous filters which now looks like yours “-something.” I welcome feedback or changes if you have an idea on how it should be. I’m thinking maybe it needs a second screen where you see whatever the first non-null entry is, and from there you can see all the meta data associated with it to pick a specific ip:port + name combination.

1 Like

I think what you have done is a good idea (and the right idea), and I was considering the same to be honest when I implemented my changes to the list… I can’t remember why I didn’t to be honest (I think it was because I wanted to keep the length down) but it might be that duplicates are being suppressed due to how maps work and unique keys… As for the name being missing for Samsung TVs I’m aware that is an issue in their implementation, and not an issue on your side… the joys of every vendor doing their own thing and not sticking to a single Plex schema!

I’m at work at the moment, so will have a proper look later, as really I need to look in the Plex XML and then at the code to pull the list to see which bit is catching it out… might just have to put a number on the end of the device name for duplicates like I did for the application name…

There is also filtering taking place on the list for local IPs only, I assume you are using the IP being stored and are expecting a local IP? If not then this logic can be stripped… It basically looks for anything on the same subnet as the hub as it currently stands…

Thanks again for all your work on this, and please don’t take any of my issues as criticism as it really isn’t :slight_smile:

Sorry I missed this bit earlier (my coffee had not kicked in)… for the glitching issue, Plex Plus has a delay available for Stop and Pause actions (under settings) this is usually to catch the delay between episodes etc… but this may also solve this issue… :slight_smile:

1 Like