Room-Dependent Alexa Switches: A Comprehensive Guide

Hello all! I wanted to share a fantastic automation I developed using snippets from others to achieve something truly handy: room-dependent Alexa switches! Imagine saying, ‘Alexa, turn on the television,’ and having it activate the TV in whichever room you’re currently in. It’s a game-changer for multi-room setups!

First off, I must give a huge shout-out to the Alexa Media Player custom component team. Without their work, none of this would be possible. This component allows seamless control of Amazon Alexa devices within Home Assistant, making the entire setup a breeze.

The only other component I used is the Harmony Hub integration, which is fantastic for controlling devices like TVs, but you can adapt this guide to work with any switch that turns on a TV or similar device.

Here’s a step-by-step breakdown of how I set it up:

  1. Create a Sensor: This sensor tracks which Alexa device was last used for voice commands. Add this to your configuration.yaml:
    yaml
  • platform: template
    sensors:
    last_alexa:
    entity_id:
    - media_player.bedroom
    - media_player.dining_room
    - media_player.living_room
    value_template: >
    {{ states.media_player | selectattr(‘attributes.last_called’,‘eq’,True) | map(attribute=‘entity_id’) | first }}
  1. Input Boolean: Create an input boolean to control the TV:
    yaml
    input_boolean:
    television:
    name: Television
    initial: off

  2. Template Switch: This switch allows you to toggle the input boolean:
    yaml

  • platform: template
    switches:
    television:
    friendly_name: “Television”
    value_template: “{{ is_state(‘input_boolean.television’, ‘on’) }}”
    turn_on:
    - service: input_boolean.turn_on
    entity_id: input_boolean.television
    turn_off:
    - service: input_boolean.turn_off
    entity_id: input_boolean.television
  1. Script: This script ensures the input boolean turns off after a short delay, allowing simultaneous use across rooms:
    yaml
    livingroomteleon:
    alias: Living Room Television On
    sequence:

    • alias: TV on
      service: switch.turn_on
      data:
      entity_id: switch.livingroomtv
    • delay: seconds: 5
    • alias: Switch Off
      service: switch.turn_off
      data:
      entity_id: switch.television
  2. Automation: This ties everything together, triggering the script based on the room:
    yaml

  • id: livingroomtvalexa
    alias: Living Room TV Alexa
    initial_state: ‘true’
    trigger:
    • entity_id: switch.television
      platform: state
      to: ‘on’
      condition:
    • condition: state
      entity_id: sensor.last_alexa
      state: media_player.living_room
      action:
    • service: script.turn_on
      entity_id:
      • script.livingroomteleon
  1. Alexa Integration: Link the switch with Alexa and create a routine for voice commands like ‘Alexa, turn on the television.’

I’ve successfully replicated this setup for other rooms and even extended it to handle volume control and mute functions. It’s incredibly versatile!

Recently, I added another layer of functionality by incorporating a Harmony Hub activity sensor. This allows the system to determine which device to control based on the current activity, such as Apple TV or Blu-ray player. Here’s how I did it:

  • Create a Sensor for the Harmony Hub activity:
    yaml

  • platform: template
    sensors:
    harmonyactivity:
    friendly_name: Living Room Harmony Hub Activity
    value_template: ‘{{ states.remote.living_room.attributes.current_activity }}’

  • Update the Automation to include the activity condition:
    yaml

  • id: appletvpausealexa
    alias: Apple TV Pause Alexa
    initial_state: ‘true’
    trigger:

    • entity_id: switch.pausealexa
      platform: state
      to: ‘on’
      condition:
    • condition: state
      entity_id: sensor.last_alexa
      state: media_player.living_room
    • condition: state
      entity_id: sensor.harmonyactivity
      state: Apple
      action:
    • service: script.turn_on
      entity_id:
      • script.appletvpause

This enhancement makes the system even smarter, adapting to the specific device in use without any manual intervention.

I’d love to hear if anyone else has implemented similar automations or if you have questions about adapting this setup for your home. Cheers to making our smart homes even smarter! :tada: