Home automation from nothing to done: Part 6


This is one of a serie of 7 posts about home automation from nothing to done. Each of them marks a milestone in the project and none of them should be too long to comfortably read in the metro when commuting.

In this post we’ll see how to setup an Amazon Echo device to interact with our Smart Home setup and control our appliances. In case you didn’t read the first five parts, please take into account that I’m using Home assistant as my home automation software. I have it running on a Raspberry Pi 3, but if you just want to try it out and don’t want to commit buying a device you can install it on your desktop or laptop. I strongly recommend using their docker image in this case since it’s the fastest and less intrusive way to try it out.

At the end of this post we’ll be able to:

  • Turn on and off the lights
  • Set the temperature of the heating
  • Activate the scripts we wrote in the previous posts

all with our voice.

Amazon Echo

The original Amazon Echo

From the Amazon website:

Amazon Echo is a hands-free speaker you control with your voice. Echo connects to the Alexa Voice Service to play music, make calls, send and receive messages, provide information, news, sports scores, weather, and more—instantly. All you have to do is ask.

Echo has seven microphones and beam forming technology so it can hear you from across the room—even while music is playing. Echo is also an expertly tuned speaker that can fill any room with 360° immersive sound. When you want to use Echo, just say the wake word “Alexa” and Echo responds instantly. If you have more than one Echo or Echo Dot, Alexa responds intelligently from the Echo you’re closest to with ESP (Echo Spatial Perception).

Available Echo devices

At the time of writing there are 5 Echo devices available for purchase:

  • The original Echo, with a high quality speaker
  • The Echo Dot, cheaper but with a lower quality speaker
  • The Tap, a portable Echo system
  • The Echo Look, acting as a personal fashion assistant
  • The Echo Show, the latest and greatest with a touchscreen display
Echo Echo Dot Tap Echo Look Echo Show
Wake word Only in the hands-free mode
Camera
Hi-Fi speaker
Touch screen
Portable
Price 179.99€ 59.99€ 129.99$ 199.99$ 229.99$

You can pick your favorite, I chose an Echo Dot since I just wanted to try how it worked. I’m now looking forward for the Echo Show to be available in Germany!

In the last days I also started reading about Snips.ai, an interesting take on the Alexa software that allows tinkerers to build their own voice assistant (with out-of-the-box intents and the possibility to build your own) and install it on their own Raspberry Pi. This means you have the performance of wake words but the privacy of voice utterances processed directly on the device. To be honest, I didn’t have time to try it yet, and I’m not really motivated to do so since I already have an Echo Dot, but it really sounds like an awesome alternative for people who are more concerned about privacy.

Emulating a Hue bridge

The key part in configuring our Echo device to work with our system is enabling an Home Assistant feature called “Emulated Hue”. This is exactly what it sounds like, that is, quoting the website:

The emulated_hue component provides a virtual Philips Hue bridge, written entirely in software, that allows services that work with the Hue API to interact with Home Assistant entities. The driving use case behind this functionality is to allow Home Assistant to work with an Amazon Echo or Google Home with no set up cost outside of configuration changes. The virtual bridge has the ability to turn entities on or off, or change the brightness of dimmable lights. The volume level of media players can be controlled as brightness.

Given that the Echo devices work with Hue bridges out of the box, this looks like exactly what we need!

To enable the emulated hue, all we need to do is change our configuration.yaml file and add the following:

emulated_hue:
  host_ip: [IP_OF_YOUR_HASS_SYSTEM]
  type: alexa
  off_maps_to_on_domains:
    - script
    - scene
  expose_by_default: false
  exposed_domains:
    - light
    - switch
    - script
    - media_player
    - group

Now, since we set expose_by_default to false, none of our entities will be available to our Echo device. This is because we have a limited number of entities we can expose (around 50) and we shouldn’t waste some of these slots with entities we’re not interested in using.

We can use the customize feature of Home Assistant to expose our entities through the emulated Hue bridge.

Following is an example of the configuration.yaml file exposing the sofa light:

customize:
  light.sofa:
    emulated_hue: true
    emulated_hue_name: "sofa"

With this snippet we’re telling Home Assistant to make the sofa light with identifier light.sofa available to Alexa with the name “sofa”.

switch.tv_setup:
  emulated_hue: true
  emulated_hue_name: "movies"
  icon: mdi:television

With this other snippet we told Home Assistant to make the tv_setup digital switch (that triggers the cinema experience we discussed about in the previous post) available to Alexa with the name “movies”. Now we can say “Alexa, turn on the movies” and Home Assistant will activate the switch triggering the script.

Testing our emulated bridge

If we reboot our Home Assistant system and open the URL in the form of http://[OUR_HASS_IP_ADDRESS]:8300/api/pi/lights, we should see a JSON containing the list of all the entities we expose to Alexa. This means the configuration is working and we can scan for devices from the Alexa app.

Configuring the Echo to connect to our bridge

Open alexa.amazon.com and go to “Smart Home”. Then click “Devices” and then “Discover”. After a few seconds you should see all the entities we exposed in the previous section.

Feel free to add all your desired entities to the emulated Hue bridge through the customize section of the configuration.yaml and scan again your devices from the Alexa app to be able to interact with them.

A cool feature of the emulated Hue is that when we say “Alexa, set the [entity] to 30” or “Alexa, set the [entity] to 30 percent”, we get the value 30 in our Home Assistant script and can interact with it. I immediately took advantage of this clever Home Assistant integration, and exposed my thermostats through the bridge to set the temperature in Celsius degrees through my voice!

Here is how this works:

In the customize section I added the following entity:

script.control_flat_heating:
  emulated_hue: true
  emulated_hue_name: "flat heating"

The script is defined in the scripts.yaml file and looks like this:

control_flat_heating:
  alias: Set the flat thermostats
  sequence:
    - service: script.turn_on
      entity_id: script.control_bedroom_heating
      data:
        variables:
          requested_level: '{{ requested_level }}'
    - service: script.turn_on
      entity_id: script.control_bathroom_heating
      data:
        variables:
          requested_level: '{{ requested_level }}'
    - service: script.turn_on
      entity_id: script.control_diningroom_heating
      data:
        variables:
          requested_level: '{{ requested_level }}'
    - service: script.turn_on
      entity_id: script.control_livingroom_heating
      data:
        variables:
          requested_level: '{{ requested_level }}'

This script triggers other scripts, one for each room, passing the {{ requested_level }} variable that is populated with the number we tell Alexa. The scripts all look alike and this is a sample one:

control_bathroom_heating:
  alias: Set the bathroom thermostat
  sequence:
    - condition: and
      conditions:
        - condition: template
          value_template: '{{ requested_level > 14  }}'
        - condition: template
          value_template: '{{ requested_level < 30}}'
    - service: climate.set_temperature
      data_template:
        entity_id: climate.bathroom_thermostat
        temperature: '{{ requested_level }}'

We just check that the temperature is between 14 and 30 and then call the climate.set_temperature service exposed by Home Assistant to change the temperature of the thermostat.

This is all we need to do in order to expose our smart heating system we configured in one of the previous posts to Alexa and be able to control it with our voice. It’s quite neat, isn’t it?

With this post we were able to control our appliances and interact with our Home Assistant setup just with our voice.

With the next and last post I’ll write about how to add some groove to your flat and be able to play synchronized music in multiple rooms, just like expensive multi-room audio systems do, but for much less. You’ll be able to reproduce songs and playlists on-demand and automatically, in one or multiple rooms at the same time.

Keep reading!