Cast Youtube (or other app function) to Roku

So I've wanted to setup my TV's to play a YouTube video as an alarm. I saw a lot of nodes for casting to Chromecast but I have Rokus at my house which luckily has a simple api. Thus I created this simple flow that takes a YouTube url, parses out the YouTube ID and then formats it into a YouTube app request to a Roku device. Comment nodes added for supported url formats and to highlight which node you need to edit your IP address in. I also left it as generic as possible so it can be easily edited to support other apps that can be triggered via the Roku API just by changing the endpoint; though you'd probably have to modify the function to support the specific app you're trying to use.

To use as-is, change the IP in the second node and click the injector. For alarm clock functionality change the injector to a scheduled even. To change the video just change the injector URL.

Flow Link

[{"id":"5fed5dca.adb674","type":"tab","label":"Cast Youtube To Roku","disabled":false,"info":""},{"id":"7b9f3d57.477c24","type":"inject","z":"5fed5dca.adb674","name":"Youtube URL","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"https://www.youtube.com/watch?v=Lrj2Hq7xqQ8","payloadType":"str","x":170,"y":100,"wires":[["7dc76446.58a2bc"]]},{"id":"eadf4a1e.e682b8","type":"debug","z":"5fed5dca.adb674","d":true,"name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":950,"y":100,"wires":[]},{"id":"523bf68d.b454d8","type":"function","z":"5fed5dca.adb674","name":"Convert To Roku Request","func":"function youtube_parser(url){\n    var regExp = /^.*((youtu.be\\/)|(v\\/)|(\\/u\\/\\w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#&?]*).*/;\n    var match = url.match(regExp);\n    return (match&&match[7].length==11)? match[7] : false;\n}\nvar ips = msg.roku_ip;\nif(!Array.isArray(ips)){\n    ips = [ips];\n}\nvar endpoint = msg.roku_endpoint;\nvar id = youtube_parser(msg.payload);\nvar port = msg.roku_port;\n\nips.forEach(function(ip) {\n    var request = \"http://\" + ip + \":\" + port + endpoint + id;\n    node.send({\"url\": request})\n});","outputs":1,"noerr":0,"initialize":"","finalize":"","x":570,"y":100,"wires":[["22d5408d.5b563"]]},{"id":"22d5408d.5b563","type":"http request","z":"5fed5dca.adb674","name":"Send To Roku","method":"POST","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":780,"y":100,"wires":[["eadf4a1e.e682b8"]]},{"id":"d1529c96.dd481","type":"comment","z":"5fed5dca.adb674","name":"Supported Youtube URL Formats","info":"Functionality pulled from:\nhttps://stackoverflow.com/questions/3452546/how-do-i-get-the-youtube-video-id-from-a-url/8260383#8260383\n\nCredit for the regex magic goes to Lasnv and the StackExchange community.\n\nhttp://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index\nhttp://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o\nhttp://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0\nhttp://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s\nhttp://www.youtube.com/embed/0zM3nApSvMg?rel=0\nhttp://www.youtube.com/watch?v=0zM3nApSvMg\nhttp://youtu.be/0zM3nApSvMg","x":170,"y":60,"wires":[]},{"id":"7dc76446.58a2bc","type":"change","z":"5fed5dca.adb674","name":"Roku Info","rules":[{"t":"set","p":"roku_ip","pt":"msg","to":"[\"192.168.11.104\",\"192.168.11.193\"]","tot":"json"},{"t":"set","p":"roku_endpoint","pt":"msg","to":"/launch/837?contentID=","tot":"str"},{"t":"set","p":"roku_port","pt":"msg","to":"8060","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":100,"wires":[["523bf68d.b454d8"]]},{"id":"f5af682a.eb7ed8","type":"comment","z":"5fed5dca.adb674","name":"Change roku_ip.","info":"Change roku_ip in this node. This is a json entry so you can specify multiple ip's:\n\n[\"192.168.11.104\",\"192.168.11.193\"]\n\nor a single ip:\n\n[\"192.168.11.104\"] or just \"192.168.11.104\"","x":340,"y":140,"wires":[]}]
2 Likes

Edited the flow to support multiple IP's so you can get all your TV's playing the same thing at the same time if desired.

Also just added a flow to get the top trending YouTube video and play it as well!