When you have many Alexa enabled devices around your home, an easy assumption to make is that each device ought to know where it is in your home in relation to the other “smart” devices around it. Simply saying “turn on the lights” should turn on the lights nearest to the speaker’s location. And while setting up this behavior is techically possible in the Alexa app (via Groups), I’d argue the overall experience leaves something to be desired. Additionally, if you are already using Home Assistant (HA), the last thing you’ll probably want to do is manage some of your automations outside your existing HA environment. Fortunately, if you have the Alexa integration* installed, you don’t have to by following the steps below.

Updated: 2020-10-16 - removed entity_id reference from last_active_alexa sensor

Part I - Setup in Home Assistant

Step 1: Create a sensor to identify the last Echo device spoken to

Add the code block below to either your configuration.yaml file or to any other file where you have your sensors defined.
Note: I have much of my configuration split in separate files by entity type. The example below is in my sensors.yaml file and other examples that follow will be in similar file types. More info about splitting your configuration can be found: here.

- platform: template
        value_template: >
          {{ states.media_player | selectattr('attributes.last_called','eq',True) | map(attribute='entity_id') | first }}

Update the Echo devices above with the device names in your configuration.

Step 2: Define light groups

If you are like me, you may have several lights in a room or area that you want to be controlled by a single voice command. This is where groups come into play. For example, I have three lights in my family room and one in my dining room that I want to be controlled with a single command.

Here is the definition for the family room lights group in my groups.yaml file.

    - switch.entertainment_center_lights
    - switch.couch_light
    - light.family_room_ceiling_lights
    - switch.dining_room_light

Create your own groups as needed. Groups are obviously only needed for two or more lights.

Step 3: Create executable scripts

With this step we need to create two scripts; one for the On service, and the other for the Off condition. The scripts are basic if-then constructs to pair the echo device (from step 1) with an associated light group (from step 2). Ultimately, these scripts will be triggered by Alexa through the integration. The example script below is the On case. For the Off case, just copy & paste, rename to “lights_off” and change the service to homeassistant.turn_off.

NOTE: You can get away with a single script if you use the homeassistant.toggle service, but this is not advised when groups are used. Users still retain the ability to manually turn individual lights on/off which means some devices within a group may be in an On state while others are Off. So a toggle here mostly likely will eventually frustrate your family and/or friends …which wouldn’t exactly inspire much confidence in your technical wizardry! Also worth mentioning, it appears that the service can not be passed as an arguement, which would be nice. If someone knows this to be otherwise, please do tell! :-)

Place this in your scripts.yaml file

  - service: homeassistant.turn_on
      entity_id: >-
        {# Retrieve the last active device name. #} 
        {%- set area_device = states("sensor.last_active_alexa")|replace('media_player.','') -%}
        {# Pair area device with desired group/light. #}
        {%- if area_device == "echodot_kitchen" -%}
        {%- elif area_device == "echo_family_room" -%}
        {%- elif area_device == "echodot_master_bedroom" -%}
        {%- elif area_device == "echodot_office" -%}
        {%- elif area_device == "echodot_kids1_room" -%}  
        {%- elif area_device == "echodot_kids2_room" -%}
        {%- endif -%}

Update the Echo device names and groups above per your own configuration.

Step 4: Restart Home Assistant

Now that we’ve completed the setup in Home Assistant, it needs to be restarted so the configuration elements can be properly loaded. Be sure to “Check Configuration” (Configuration/Server Controls) prior to restarting to make sure your changes are free of typos or formatting errors.

Part II - Create Routines in the Alexa App

This part is not exactly an optimal user experience. We need to create a Routine and for whatever reason, Amazon has made the decision that it can only be done in the Alexa app. So go ahead and fire up that shiny app or to download it if you haven’t already.

Step 1: Verify your scripts are available as scenes in Alexa

First, note a terminology change here, what were scripts in Home Assistant are now exposed as scenes in the Alexa app. To view these scenes, select Devices, then Scenes. Press the Discover button at the bottom of the screen if your scripts are not listed. Discovery takes about a minute. You should see your scripts listed on the Scene screen once the process is complete.

Step 2: Create a Routine

A Routine is basically like a macro; a short recipe to do things. The main elments are very simple. First, input a routine name, then enter the phrase Alexa needs to recognize, and finally, in our case, assign the “scene” to be invoked.

Create a routine by clicking the hamburger menu in the upper left hand corner of the Alexa app screen, choose Routines, then in the upper right hand corner press the plus icon. On the next screen enter:

Name: HA Turn the lights on*
Phrase: “Alexa, turn the lights on”
Scene: lights_on
*NOTE: the name can be whatever you want. Using HA may help remind you this is linked to Home Assistant…

Repeat the above for the lights off case and you’ve completed your first lights on/off cycle. Congratulations! You can stop here, but you many want to go a little further…

Alexa only understands the exact utterance: “turn the lights on” but there are many other close variants you, your family, or guests may use from time-to-time.

“turn on the lights”
“turn lights on”
“turn on lights”
“lights on”
“light on”
…or insert common variants for your locale…

Creating a Routine for each of these phrases (along with their “off” conditions) would go a long way to cover all the possible ways someone could ask to turn a light on/off. Now, your solution would seem a little magical because it would just work irrespecitve of the phrase used.

*Note: This integration specifically uses the Alexa Media Player Component for Home Assistant. The examples in this post, and more like them can be found here.