Hi
Since i have used these forums to get me out of pickles i thought that i would share an automation i developed using snippets of others to achieve one goal - Room Dependent Alexa switches! i.e. If you say "Alexa Turn on the Television - it turns that rooms Television on!
First of, none of this is possible without the Alexa Media Player customer component, so big thanks to that team:
The only other component this uses is Harmony Hub:
However as long as you have a switch that turns a TV (or any other devices) and this will work.
Here are the steps:
- Create a sensor in configuration.yaml which connects to alexa_media.update_last_called. This tells HASS which Alexa was used for voice command:
- 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 }}
Under entity_id list the alexa’s that you want to include in the automations.
- Create an input boolean in configuration.yaml:
input_boolean:
television:
name: Television
initial: off
- Create a switch that controls that input boolean in configuration.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
- Next create a script in script.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
This step ensures that when it is used after a few seconds the input boolean turns off so that you can technically do this in every room at pretty much the same time bar a few seconds!
- Now for the automation. Create this automation in automation.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
- Ensure the switch created at step 3 is linked with Alexa and create a routine like this:
When: Alexa, Television On Alexa, Television power on.
Thats it! Obviously you need to repeat steps to add the other room TV’s, dining room, bedroom etc.
You also need to tweak it slightly for Television off. Steps 2 - 6 and call it all Television off.
I have also used this for Television volume up, down and mute. You can also use this method for any devices repeated in each room such as (time of year of writing this) Christmas lights which are on smart plugs.
Feedback on this is welcome and let me know if you have successes as this is my first share of an automation.
Cheers
Ash!
UPDATE
Since posting this i was attempting to add pause and play options, however even though in some rooms this was easy as it is only one device that controlled pausing or playing, in my living room it depended on more than the TV to pause such as an Apple TV or BluRay Player. As I use Harmony Hub i realised i had to add another condition which was which Harmony Activity was Active so i created a sensor:
- platform: template
sensors:
harmonyactivity:
friendly_name: Living Room Harmony Hub Activity
value_template: '{{ states.remote.living_room.attributes.current_activity }}'
This is now another condition to add to the automation, here is an example:
- 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 works a treat!