I recently saw a request in one of the forums online asking for help on how to trigger events when certain events happen in plex. I thought I would write a quick tutorial to show how this can be done in Node-red. With the help of webhooks you could create whatever automations you like. I am taking the most simplest approach here and that is whenever something is playing on plex I will trigger a sonoff basic to switch off and whenever something is stopped or paused, I will have the sonoff basic switch on. This will mimic a light. By using mqtt this could easily be expanded to some other entites or triggers.
In order to accomplish this, I am using the webhook relay node and the service provided for free at webhookrelay.com. So first we need to add the node into node-red. You can find the node here: node-red-contrib-webhookrelay
Once that node is installed, you need to set up an account at webhookrelay.com (the basic tier is free).
Once you’ve set up an account (you can sign in with google or github) you’ll be taken to my.webhookrelay.com where you’ll be faced with a rather intimidating looking screen. I’ll walk you through it.
Firstly, you need to set up a bucket: Give it a name i.e. plexbucket
Make a note of the default public endpoint it gives you. This is what we’ll be pasting into plex under the add webhook section (https://app.plex.tv/desktop#!/settings/webhooks)
Secondly, we need to go to Access tokens in the side menu. Here we need to create an access token. Be mindful here, you need to make a note of both the Access Key and the Secret. Once you’ve created one, you won’t be shown the secret again, so safely copy that somewhere for use shortly. If you miss it, you can delete that token and just create a new one.
Once complete, you’re done with webhookrelay.com. Go to your plex server settings and paste in the webhook URL you copied from step one above.
Now it’s time for Node-red. You should have already installed the node-red-contrib-webhookrelay node from earlier. find the node and drag it out. Double click the node to configure it. This is easy enough, there are three fields that need to be filled in: Buckets i.e. plexbucket (the name you created initially) and the access key and secret you just received. Deploy that and hopefully you should see it show as connected.
So far so good?
Here’s the complete flow, but I will break it down step by step for you:
At this point I recommend dragging a debug node to the output of this node and then pressing play on plex. You should see a complete payload message come down showing a wealth of information (this is in the debug window on the far right of the node-red screen under the little bug icon). Things here can be as complicated as you want them to be. For us here though, we’re only interested in the first bit:
Content-Disposition: form-data; name=”payload”
This shows us that when you play something on plex, this is the event that comes through the webhook i.e. “media.play”. Playing with the other operations, we get the following events: “media.stop”, “media.pause” and “media.resume”
So we model these in node-red using a switch node. I like to use these as a gate. Here we force payloads as we want them i.e. if play or resume I want the lights (sonoff basic in our test case) to turn off. If we stop or pause, I want the lights to turn on. See here for how I’ve configured the switch node.
Next we want to simplify the payload coming out of the switch node. I use the change node to set the outgoing (new) payload as either “on” or “off” according to whether the incoming payloads are “media.stop” or “media.pause” and “media.play” and “media.resume” respectively. You can see an example here.
Notice I have also set up several inject nodes for testing. These just inject the various strings/payloads as we would expect to see from the plex webhook.
Here’s an example of one here:
Next, we want to actually trigger the lights (sonoff basic). I am using simple MQTT to switch on and off a sonoff basic that’s running Tasmota 8.3 firmware. It cost me $6 on aliexpress and is perfect for a lounge lamp. I could just as easily have used a shelly Em1 (they use MQTT out of the box) to control the room’s mains lighting. I am sending the payload of on or off to cmnd/sonoff/power. I have a local MQTT server as well as a remote server CloudMQTT. Use whatever is appropriate for you.
See here for the MQTT node.
Once deployed you can play with the inject nodes and see what happens when you inject the relevant payloads. Once satisfied that you have everything the right way, you can ignore those and just start playing and pausing things on Plex. If everything went well, you should be golden!
Let me know in the comments if you found this useful, or if you have any questions or better methods. I’m open to suggestions! Remember this is just the most basic way of working with webhooks. With mode advanced node configuration you can make different things happen according to the user, server being accessed, parent rating etc. Imagine you have children and one of them wants to watch something rated above their age, or watching something past their bedtime. You can configure that accordingly and be notified either via voice assistant or a message to your phone. The possibilities are endless. If you’re new to node-red, ch
eck out my other posts where I describe how to set up voice confirmations and phone notifications. You can find them here.