My Home Theater Voice Automation
The details
(video and tutorial down the page)
Ever since I worked at a video store (‘member the ‘90s?) above a high-end home theater shop, it was my dream (among a thousand other dreams) to build my own home theater.
Fast-forward to 17 years later and the home theater I always wanted is built and it’s really awesome!
Some specs:
- For games: A Playstation 4
- For movies: A Raspberry Pi 3 running OSMC (Kodi)
- For storage: WD 3TB external hard drive connected to the wifi router (as NAS)
- For TV: Chromecast
- For sound: A Sony A/V Receiver (STRDH520) with HDMI, 7.1 surround, all speakers by Polk Audio
- For seeing it all: A BenQ 1080p projector (HT2050) and a HOMEGEAR 100 screen.
As you can imagine from the list above, I had A LOT of remote controls in that room, and I had to press maybe a single button or 2 on each of the remotes every time I entered or left the room. So I started looking into universal remotes. The Harmony Elite looked like it should do everything I needed. It comes with a Harmony Hub which can control a wide array of devices. Once I saw Amazon sells it with a free Echo Dot, I thought why the hell not. With some ease, I can set up IFTTT to do some nifty things.
Once I got the Harmony, and the Echo Dot, I started playing around. Setting up devices in the Harmony Hub was a breeze, and from there, setting up an IFTTT rule to forward Alexa commands to trigger activities was pretty easy.
For example, I had set it up such that when I say “Alexa, trigger play a movie”, it fires a IFTTT rule that runs the “play a movie” activity in Harmony, which turns on the receiver on the correct input, turns on the projector, gives Bluetooth control to the harmony remote over the Raspberry Pi to control Kodi, etc.
Sounds good, right? wrong.
I still had to manually lower/raise the projector screen because it has an RF remote (not IR) and the Harmony Hub doesn’t do RF.
I also had to manually turn on the ps4 for the “trigger play ps4” activity because the Harmony Hub can’t do that either.
I did some more research and found out about Home Assistant. It’s an open source home automation platform that I can run on my Raspberry Pi. It means I could add devices to it, that it can control remotely, and as long as the Raspberry Pi can run the command, Home Assistant can run it and make it look like any home automation device.
For the ps4, for example, I found out about ps4-waker, it’s a little program I can run on my pi that can turn my ps4 on and off. I can then set up Home Assistant to “see” the ps4 as a switch.
At this point, I needed a way for the Harmony or Alexa to control home automation devices that are announced by Home Assistant. Turns out Home Assistant has a component that allows it to “pretend” to be a Philips Hue hub, and expose all devices it knows about as switches (i.e. light bulbs) or other devices. Both Alexa and the Harmony Hub can control Philips Hue devices directly, so I eliminated the need for IFTTT completely, making everything faster and the commands cleaner (“Alexa, turn on ps4”, “Alexa, turn off AV”)
For the pièce de résistance, I needed a way to lower/raise the projector screen. As I mentioned, it has an RF remote and nothing currently in my house can send RF signals. After some more digging, I found that I can buy a cheap 433Mhz RF Transmitter and Receiver module to connect to the Raspberry Pi, and control that using rpi-rf. Home Assistant already has a switch for it.
Using my existing remote, I sniffed for codes, and found what I needed to define as “code_on” (lower the screen) and “code_off” (raise the screen).
I added both Harmony skills to Alexa, and since the Harmony Hub can call Home Assistant, I managed to create activities that turn on all relevant devices for each command, and lower the screen, etc.
Putting it all together, for the ps4 example, the end result is me walking into my home theater and saying “Alexa, turn on ps4” and the following will happen:
- The projector turns on
- The AV Receiver turns on to the correct input for the ps4
- The projector screen starts lowering
- The ps4 turns on
- The Harmony Remote can control Kodi via Bluetooth (not sure if it remains paired or that pairing happens when I fire the activity)
- The “mute” command is sent to the projector (I have audio coming out of the speakers and I don’t need the projector’s internal speaker)
Harmony has a default activity to turn off all AV devices. When I’m done playing, I say “Alexa, turn off AV” or “Alexa, turn off ps4” and it invokes the Hub’s activity to turn everything off. The ps4, projector, and receiver shut down, the screen goes up and I go to sleep because it’s 4am and I’ve been playing too much.
Here’s a video of it all:
Read the tutorial and let me know if you have any questions in the comments.
The tutorial
Requirements:
- Echo Dot or another variation of Alexa
- Harmony Hub or Elite (with free dot)
- Raspberry Pi 3 (it’ll probably also work with 2)
- Entertainment devices
- Basic understanding of running command on the Raspberry Pi by ssh’ing to it
OSMC
There are a lot of tutorials out there, google it (here’s a good one).
Start by downloading.
Home Assistant
Follow the instructions on this page.
Once I was done and everything worked, I stopped the “hass” process because I wanted to run it on startup, so I followed the instructions on this page
I used the config from the part where it says
If you’ve setup Home Assistant in
virtualenv
following our manual installation guide, the following template should work for you.
These are my additions to the Home Assistant configuration.yaml file:
media_player basement:
- platform: kodi
name: basement kodi
host: http://localhostemulated_hue:
exposed_domains:
- switchswitch:
platform: command_line
switches:
ps4:
command_on: 'ps4-waker'
command_off: 'ps4-waker standby'
command_state: 'ps4-waker search | grep Code | grep -o [0-9]*'
value_template: '{{ value == "200" }}'
friendly_name: "ps console"
ps4-waker
In order to install ps4-waker, you need a recent version of nodejs:
$ sudo su -
# curl -sL https://deb.nodesource.com/setup_11.x | sudo bash -
# apt-get install nodejs -y
# npm install ps4-waker -g
Then run ps4-waker and follow the instructions on the screen.
Note: When I initially ran ps4-waker, it created its config file in /root but the ‘user’ account I normally use is ‘osmc’, its home directory being /home/osmc, if you end up running this from home assistant, depending on your set up, you might have an additional user account ‘homeassistant’. Anyway, I created a soft-link to all the respective home directories, so I can run ps4-waker from anywhere.
Since I only have one ps4 (the ‘search’ command has one result), this config works great for me:
switch:
platform: command_line
switches:
ps4:
command_on: 'ps4-waker'
command_off: 'ps4-waker standby'
command_state: 'ps4-waker search | grep Code | grep -o [0-9]*'
value_template: '{{ value == "200" }}'
friendly_name: "ps console"
Combined with emulated_hue, this means Alexa/Harmony will think there’s a light switch called “ps console”, if you turn it on or off it will turn the ps4 on or off.
Raspberry Pi (RF Module)
I bought this little module here.
In order to connect them, I also got female-female jumper wires.
Connect the RF module to the pi:
RPI GPIO HEADER
____________
| ____|__
| | | |
| 01| . x |02
| | . x__|________ RX
| | . x__|______ | ________
| | . . | | | | |
TX | ____|__x . | | |__|VCC |
_______ | | __|__x . | | | |
| | | | | | x____|______|____|DATA |
| GND|____|__| | | . . | | | |
| | | | | . . | | |DATA |
| VCC|____| | | . . | | | |
| | | | . . | |____|GND |
| DATA|_________| | . . | |________|
|_______| | . . |
| . . |
| . . |
| . . |
| . . |
| . . |
| . . |
39| . . |40
|_______|
TX:
GND > PIN 09 (GND)
VCC > PIN 02 (5V)
DATA > PIN 11 (GPIO17)
RX:
VCC > PIN 04 (5V)
DATA > PIN 13 (GPIO27)
GND > PIN 06 (GND)
Install rpi.gpio and rpi-rf as root:
$ sudo su
# apt-get update
# apt-get install python3-pip python3-dev gcc -y
# pip3 install rpi-rf
Note: It’s possible that in doing this as root, I caused the permission issues later. Not entirely sure.
Most guides out there will tell you to add your user (homeassistant or osmc) to the gpio group, but osmc doesn’t release with that group, so I went with manually creating it and fixing permissions. If you see some error message related to “no access to /dev/mem”:
$ sudo su -
# groupadd gpio
# adduser homeassistant gpio
# chown root.gpio /dev/gpiomem
# chmod g+rw /dev/gpiomem
# sudo adduser osmc gpio
However, the dev nodes are actually created at boot, so any changes you make to them will not persist. You can make that permanent by adding a udev rule.
$ sudo su -
# echo 'KERNEL=="gpiomem", MODE="0660", GROUP="gpio"' > /etc/udev/rules.d/gpio.rules
Using the existing screen RF remote, sniff for its codes (run this command and use your existing remote next to the receiver, the console will print out the codes it detects):
# rpi-rf_receive
Once you have the codes you’re interested in, add a switch in Home Assistant (with your codes instead of 1234/5):
switch screen:
platform: rpi_rf
gpio: 17
switches:
projector_screen:
code_on: 1234
code_off: 1235
Harmony Hub
There are a lot of explanations in the Harmony site saying how to add devices and create activities. Follow those.
The only tricky part was adding Kodi in the Harmony app:
- Add kodi to Harmony: add a new device, the manufacturer is “Apple” (don’t ask me why) and the model name is “kodi”.
- Choose the device and press any button (like “info”)
- The app will say you need to Bluetooth pair. It puts the hub in pairing mode.
- Then In OSMC, go to MyOSMC -> Network, enable Bluetooth and search for devices, it should find a Harmony Keyboard.
- Pair
I created an activity for “play ps4” that handles the receiver, projector, screen, ps4. Play around with that app, I keep discovering new features.
Alexa
- Add the Harmony skills to your Alexa.
- By far the easiest part: “Alexa, discover devices” and it will find everything.
- Use the Alexa app to tweak whatever devices you don’t want it to control. (e.g. I had both the Harmony activity and the Home Assistant ps4 switch named “ps4” so saying “Alexa, turn on ps4” might have had a conflict.
The next step for this project is to control Kodi completely with Alexa. I plan on following this tutorial.
I really want some more cool ideas, so let me know if you have any thoughts!