Hey there! In this post I will go through the process of setting up an Alexa skill that allows you to control Plex playback on a Chromecast device and let’s you play any show or movie from your Plex server.

Introduction

Over the last year or so I’ve slowly started adding more and more voice control elements to my smart home. One thing I thought was particularly cool was controlling media playback using my Google Home mini. Things like playing a movie, or show from Netflix or Plex and even playing specific episodes of shows. Since my main voice assistant is currently Alexa, I’ve been looking for a way to get something similar to work using Alexa skills. Of course it’s possible to control playback on Amazon Fire TV devices, but those are quite difficult to get here and I definitely love my Chromecasts too much to give them up.

I stumbled across the Phlex project, an interface that let’s you use Google Assistant to control playback from your Plex server on Chromecast devices. I found that it worked quite well and was quite easy to set up, the only problem was that Alexa wasn’t/isn’t quite supported yet. Luckily Phlex provides a sort of API, allowing you to pass requests in an URL. With that in mind, I got to work. My idea was to create an Alexa skill and write a sort of proxy that translates Alexa requests to Phlex requests. I hosted the code on Github. The easiest way to run this is using an Amazon Lambda function. But let me go over the things needed to get this to work.

Overview

The way this whole thing works is that we will create an Alexa skill that will handle the voice part. We will set up intents for playing a certain movie or show and set up the skill to forward this information to our proxy. The proxy, in my case a lambda function, will take the data from the Alexa intent and create and call a request to our Phlex instance. An example:

Alexa, ask (name of your skill) to play the movie Cars on the living room TV Alexa skill sends information to lambda Lambda creates url: https://your-phlex-instance/api.php?say&apiToken=TOKEN&command=play+cars+on+living+room+tv Phlex will look for the movie on Plex and start playback on the specified Chromecast device

Here’s a list of things you’ll need in order to do this:

With that in mind, let’s get started.

Setting up Phlex with the Cast plugin

We’ll start off by setting up our Phlex instance. The easiest way is to use phlexchat.com, but at the time I’m writing this the web version seems to be down for maintenance. The second easiest way is using docker or directly running it on a Raspberry Pi.

Once you finished that, go ahead and install the Cast plugin onto your Plex server. It’s as simple as dragging the Plugin file into the Plugins folder of your Plex server, the trickiest part being figuring out where that folder is. Follow the Plex support articles to find the location of that folder for your platform.

Now the interesting stuff: Alexa Skill

Amazon now offers “hosted skills” as a public beta. They allow you to edit and deploy your code directly from the Alexa Skills Kit console and make setting up a skill incredibly easy. I’ll go through what’s needed to get this set up using a hosted skill step by step.

If you’d prefer setting up lambda and everything by yourself and learn more about the AWS ecosystem, here’s an excellent tutorial on how to set up your first Alexa skill. You can pretty much follow that tutorial 1:1, the only things we’ll do differently is setting up different intents as well as a different lambda function.

Start off by creating an Alexa Skill, go to the Alexa Skills kit and press Start Skill. Click Create Skill and pick a name for your Alexa skill. I’ve noticed that if you pick something too common like “media center”, Alexa will get confused and occasionally open other skills instead. Flex TV should work fine, but don’t worry you will be able to change this at any time. Make sure to select Custom and Alexa-Hosted (Beta). Click Create Skill.

Create your Intent

Now you should see the Alexa Skills Kit console. It looks a bit intimidating but it’s actually quite simple. The main thing you should focus on is the column on the left. There’s a list of intents, you should see a Hello World intent there. You can either delete it or just leave it there, since we won’t be using it. Go ahead and press Add to add a new intent. Give it a name, for example PlayMedia and click Create Custom Intent. Now you should be able to see your new, empty Intent.

Create Slot Types

Next up we will set up the slots. Slots are places where the user will pass information on to the Alexa skill. In our case, we’re interested in the name of the Movie or Series and if you have multiple Chromecast players also the name of the player. If you only have one Chromecast, you can skip this part. On the left, press Add next to Slot types. Instead of creating a custom slot type, we can use Amazon’s built in types for Movie and Series names. Check Use an existing slot type and search for Movie. You should be able to see AMAZON.Movie, click Add Slot Type next to it. Now do the same thing for Series. For our player name we will need to create a custom slot type, so click Add next to Slot Types and this time choose a name for your custom slot type, for example player. Now you can start adding the names of your Chromecast devices, in my case that was Living Room TV and Bedroom TV. Go ahead and add some synonyms as well, those will help Alexa understand which player you mean exactly.

Finishing our Intent

Now we can head back to our PlayMedia intent and finish things up. First, we need to add our Intent slots. Add slots with the names movie, series and player. Select the corresponding slot types for those. Make sure to write the names in lower case, otherwise you’ll run into problems later on.

Now we can start adding the sample utterances, so things we would say to Alexa to start this intent. Try and add as many examples as possible, but here are some to start off with:

play the show {series} on {player}

play {series} on {player}

play {series}

play {series} in the {player}

play the movie {movie} on {player}

play {movie} on {player}

play {movie}

play {movie} in the {player}

Invocation name

One last thing to change is the invocation name. On the left, click Invocation. When picking a name, pick something that doesn’t sound too similar to other things. For example, don’t pick plex or anything that sounds close to it, otherwise Alexa will constantly open the Plex skill. Flex TV should work but you can play around with that a bit.

Now we’re pretty much all done with the Alexa Skill. Click Save Model and then Build Model. You can come back and make changes, but remember to save and build every time you do.

The coding part

Now that our skill is set up, we need some sort of backend to process the information received from our intents. At the top, select the tab Code. It should open up a code editor with some files on the left. We’re going to need to edit and create some new files. You’re going to need some files from my Alexa-Phlex-Proxy Github repository so go ahead and git clone or download as zip.

First off, we will add the required dependencies. Open (double click) the package.json file in the Alexa editor and replace the dependencies with the ones from the github package.json file:

"dependencies": { "alexa-message-builder": "^1.2.0", "alexa-sdk": "^1.0.25", "alexa-skill-kit": "^1.0.0", "node-fetch": "^2.2.0", "request": "^2.87.0", "request-promise": "^4.2.2" }

Remember to click Save when you’re done.



Next up we will create a file that stores your secrets, phlex URL and token. On the left, click New File and name the file secrets.js.

Add the following to this file:

module.exports = Object.freeze({ PHLEX_URL: 'http://url.to.phlex/api.php?say&apiToken=', PHLEX_TOKEN: 'YOUR-PHLEX-TOKEN' });

Replace the PHLEX_URL and PHLEX_TOKEN with your values. You can get the token by logging in to Phlex, going to the settings and there should be your API Key. If you’re on app.phlexchat.com, you might have to copy your IFTTT URL and take the token from there.

Next up, open up the index.js file. Copy&paste the content of the skill.js file from GitHub. One line you might want to edit is line 45:

var playerInput = ‘Living Room TV’

Add the name of your main or most used Chromecast device there. If you don’t provide a player name in your request, for example by saying “Alexa, ask Flex TV to play the movie Cars” it will default to that device.

Now you’re pretty much done, click Save and Deploy.



Testing and Debugging

The easiest way to test your skill is using the Alexa Skill Kit test tools. At the top, click the Test tab. Make sure Skill testing is enabled in: Development is selected.





Now you’ll be able to type in commands that Alexa will process as if it was a real device. Go ahead and type in something like “ask (your invocation name) to play the movie (movie name)”

After about 5-10 seconds, you should get a response from Alexa and the movie should start playing, provided your Plex and Phlex servers both are up and running.

If you get any errors, the first thing you can do is examine the JSON Input and Output on the right. Check if the Intent got correctly recognized as an IntentRequest and that the slots got correctly filled out with the movie and player name.

The next thing to check is the logs from Lambda (CloudWatch). You can get there by navigating to the Code tab and clicking Logs: Amazon CloudWatch in the lower left corner. That should help you find the cause of the issue rather quickly.

Aaaaand we’re done

Once you got it up and running you should be good to go. Go ahead and try it from a real Alexa device. Test your invocation name to see if Alexa has any issues understanding it. It should be easy to go back and tweak any settings now.

I’ve been running this setup for about a year and haven’t ran into any bigger issues. One thing I’d like to add at some point is the ability to play certain seasons or episodes, which should be possible using Phlex.

I’ve since started expanding my Alexa skill and am working on turning it into my own sort of voice assistant. Some features I’ve added are playing Twitch streams, downloading movies and shows as well as lots of other things involving Home Assistant. I might write a separate post going through some of those things in detail.

I hope this was useful, thanks for reading 🙂