Automating my TV with Alexa

TL;DR created software to mimic my TV remote, integrated this with my Amazon Echo

Image for post
Image for post
This isn’t my TV but I liked the picture

Sometimes I just find using the remote for my TV annoying, especially when I mostly only use it for my PS4 and Chromecast. For example, getting ready to play my PS4 involves

  1. Looking for the remote
  2. Looking for the remote (go to step 1 until complete)
  3. Finding the remote
  4. Turning the TV on
  5. Waiting a few seconds for the TV to become responsive to button presses (“Smart TV” operating system)
  6. Changing the input source to HDMI input 4
  7. Adjusting the volume to around level 15 or so

Doing this a few times a week just got annoying, especially when the remote slips down the back of the sofa.

So back in 2013 or so I embarked on seeing if I could automate this process, but it wasn’t as simple as integrating it into the more modern smart home solutions.

Luckily someone figured out the fact that quite a number of Sony TV’s run a HTTP server that responds to commands from their iOS/Android app that mimics the remote. If you issue the command in the format the server expects, you can control the TV via anything that can issue a HTTP POST.

This led me to build a small piece of software running on a Raspberry Pi that executes scripts that issue commands to the TV, as if it was the app. For example the script to activate the PS4 profile looks like

tvon 2000 1
hdmi4 2000 1
volumedown 600 30
volumeup 600 15

Unfortunately as the remote isn’t a two way broadcast, there’s no way of knowing the current state of the TV, so the first number after each command just dumbly makes the software wait for the specified amount of milliseconds before issuing the next command. Additionally adjusting the volume means having to go all the way to the bottom before climbing to level 15.

A bit awkward, but combining this with Alfred workflows on my mac, I could control the TV from a quick keyboard command.

Image for post
Image for post
Turns my TV on, changes input source to HDMI 4, sets volume to 15

This still involves my laptop being nearby though.

Alexa, ask my TV to turn to PS4

After getting my Amazon Echo, I wanted to see if I could take my crude TV automation solution one step further by making it voice enabled.

However this presented me with two challenges

  • Alexa Skills kit only works by either invoking a Lambda, or calling a HTTPS endpoint
  • All services running on my home network are only externally accessible by a second Raspberry Pi running NGINX, SSL protected using LetsEncrypt and requests require a valid self signed certificate for authentication.

This meant that the HTTPs endpoint option was out of the question as you can’t provide the certificate for authentication. So I had to write a Lambda and find some way of packaging up a certificate with that securely so it could

  1. Interpret the command provided from the Alexa skills kit
  2. Issue the correct command to the TV control software (using certificate based authentication)

What I ended up doing was creating a Lambda for the Alexa skill, using KMS to secure my certificate password and putting some simple logic in place to call the correct script on my TV control software.

Image for post
Image for post
Overall architecture

With all this in place the interaction has been reduced to

“Alexa, ask my TV to turn to PS4”


“Alexa, ask my TV to turn to chromecast”

And when I’m done

“Alexa ask my TV to turn off”

Doing this has somewhat streamlined things, at least when say, getting in from a hard days work, walking past the living room, getting Alexa to turn the TV on and have everything ready by the time I hit the sofa has been great.

Admittedly having to prefix all the commands with “ask my TV” can be a little awkward, especially when turning the TV off where using the remote is probably quicker.

More modern televisions might integrate with the Alexa “IoT” framework better, so some of that awkwardness would probably slip away, but due to the age of my television and the fact that you can’t query it for its current state (e.g. what is the volume set to right now? is it on? what channel is it set to?) it’s actually rather dumb but it works for me.

In the future I hope to create more skills to interact with my home network with the lessons I’ve learnt from this one, including the certificate based authentication, and while I don’t believe everything should be voice enabled, it certainly augments things in a nice way!

video game fan. software engineer @ Cloudflare (opinions own)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store