-
Notifications
You must be signed in to change notification settings - Fork 300
Examples: Automation
- Text to Speech (TTS)
- Last Alexa Sensor
- Live Sensor Reports
- Activating Alexa Routines
- Activating Alexa Sequence Commands
- Room Aware Alexa Routines
- Room Aware Live Sensor Reports
- Room Aware Scripts Using Areas
- Connecting Bluetooth Speakers
- Time Remaining Announcements for Timers
- Notification when Amazon Login Required
(Be aware that using "type: announce" will cause Alexa to make a "ding" sound before speaking, while using "type: tts" will cause Alexa to speak right away)
doorbell:
sequence:
- service: media_player.volume_set
entity_id: media_player.downstairs
data:
volume_level: '1.0'
- service: notify.alexa_media
data:
data:
type: tts
target:
- media_player.downstairs
- media_player.upstairs
message: 'Ding-dong. Someone is at the door.'
- id: washing_machine_finished
alias: Washing machine
trigger:
- entity_id: sensor.washing_machine
platform: state
from: Washing
to: Not washing
action:
- service: notify.alexa_media
data:
data:
type: announce
target:
- media_player.downstairs
- media_player.upstairs
message: 'Yo Remco.. The washing machine is finished. Get off the couch.'
remco_home:
alias: Remco comes home
sequence:
- wait_template: '{{ is_state(''sensor.broadlink_s1c_sliding_door'', ''open'') }}'
timeout: 00:08:00
- delay: 00:00:12
- service: notify.alexa_media
data:
data:
type: announce
target: media_player.downstairs
message: 'Hello Remco. Welcome home. Cherry is {% if is_state("device_tracker.cherry",
"home") -%} also at home. {%- else -%} not at home. {%- endif %} The temperature
inside is {{ states("sensor.broadlink_sensor_temperature") }} degrees. And
outside it is {{ states("sensor.weather_temperature") }} degrees.'
Setting up your own last_alexa sensor is no longer needed for TTS notifications. You can simply set your service to notify.alexa_media_last_called
- alias: "Alexa: Jug is Hot"
initial_state: 'on'
trigger:
- platform: state
entity_id: switch.kettle_power
to: "off"
action:
- service: notify.alexa_media_last_called
data:
message: "Jug Boiled. It's Your turn to make it."
data:
type: announce
By using information provided by your Alexa devices, we can create a sensor to know which device was the last one spoken to. First you will need to create a group in your configuration.yaml file containing all your Alexa devices.
group:
echos:
name: Echomania
entities:
- media_player.this_echo
- media_player.that_echo
- media_player.his_echo_show
- media_player.her_echo_show
You have two options when creating a template sensor in your sensors.yaml
Option 1: This works for most standard use cases.
template:
- sensor:
name: "Last Alexa"
state: >
{{ expand(states.group.echos) | selectattr('attributes.last_called','eq',True) | map(attribute='entity_id') | first | default }}
Option 2: If you have older echo devices where "last called" is undefined or you are using more than one Amazon account use the following. (When using this method it may be necessary to call the homeassistant.update_entity service on the sensor in your automation before calling the desired alexa function.)
template:
sensor:
- name: "Last Alexa Timestamp"
state: >
{% for echo in (expand(states.group.echos) | selectattr('attributes.last_called_timestamp') | sort(attribute='attributes.last_called_timestamp', reverse=true)) %}
{% if loop.first %}
{{ echo.entity_id }}
{% endif %}
{% endfor -%}
Using the Last Alexa Sensor and Alexa routines to have Alexa provide information about your HA sensors.
Put this in your configuration.yaml. If you have a lights.yaml, put this there.
light:
- platform: template
lights:
alexa_virtual:
friendly_name: "Alexa Dummy Light"
turn_on:
turn_off:
set_level:
This is the automation
- alias: "Alexa Report"
trigger:
- platform: state
entity_id: light.alexa_virtual
to: 'on'
condition:
action:
- service: alexa_media.update_last_called #this will force update the last Alexa attribute used by the sensor
- delay: 00:00:01 # you may need to increase this value if you dont get the response on the echo you asked the question
- service: notify.alexa_media_last_called
data:
data:
type: tts
message: >-
{% if (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | int == 1 %}
Words. What you want to say {{ states('sensor.name_of_sensor') }} follow on informtion. For example Degreess
{% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | int == 2 %}
Words. What you want to say {{ states('sensor.name_of_sensor') }} follow on informtion. For example miles
{% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | int == 3 %}
Words. What you want to say {{ states('sensor.name_of_sensor') }} follow on informtion For example minutes
{% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | int == 4 %}
Words. What you want to say {{ states('sensor.name_of_sensor') }} follow on informtion
{% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | int == 5 %}
Words. What you want to say {{ states('sensor.name_of_sensor') }} follow on informtion
{% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | int == 6 %}
Words. What you want to say {{ states('sensor.name_of_sensor') }} follow on informtion
{% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | int == 7 %}
Words. What you want to say {{ states('sensor.name_of_sensor') }} follow on informtion
{% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | int == 9 %}
Words. What you want to say {{ states('sensor.name_of_sensor') }} follow on informtion
{% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | int == 10 %}
Words. What you want to say {{ states('sensor.name_of_sensor') }} follow on informtion
{% else %}
{{ states('light.alexa_virtual.attributes.brightness') }}
{% endif %}
- service: light.turn_off
entity_id: light.alexa_virtual
You need to then expose the "light.alexa_virtual" to your alexas. This is the same as exposing any HA device to your alexa's and not directly related to this component. You can do this with emulated_hue, haaska, or the HA cloud.
Once the light is exposed and you can turn it on/off, etc with your voice start the next steps
- Use the Alexa app to create a routine
- Click + to create a new routine
- Click the + next to "when this happens"
- Click on Voice and enter what you want to say. For example "Alexa, give me the surf report"
- Then click on add action
- I recommend you add a step for "Alexa Says" and enter a short phrase "Let me check for you" is good example
- Then add another step
- Smart Home --> Control Device --> Select your Alexa Dummy Light and then select brightness
- Select a value from 1 to 100, the number will correlate to the automation above
- Click Save, give it a few minutes, and try it out.
Good uses of this would be to get temp info of a sensor, distance/time to a location using a waze/google sensor, tides, doors open/closed, etc.
First make a routine in the Alexa App. Make the invocation, the same phrase you want to use in the media_content_id:. You can let Alexa read your flash briefing, news, traffic, the weather, your next calendar event, random/custom phrases and all other features available in the Alexa App under Routines. With the entity_id , you can set wich echo will carry out the routine.
scripts.yaml
remco_awake:
alias: Remco woke up
sequence:
- data:
entity_id: media_player.upstairs
media_content_id: remcowakeup
media_content_type: routine
service: media_player.play_media
- wait_template: '{{ is_state("sensor.broadlink_s1c_living_room_door", "open") }}'
- data:
entity_id: media_player.downstairs
media_content_id: remcodownstairs
media_content_type: routine
service: media_player.play_media
Look for available sequences in Sequence commands This is case sensitive, so copy it exactly as it is in the list.
automation.yaml
- id: Remco_welcome_home
trigger:
- platform: state
entity_id: device_tracker.remco
to: home
action:
- wait_template: '{{ is_state("sensor.broadlink_s1c_sliding_door", "open") }}'
- data:
entity_id: media_player.downstairs
media_content_id: Alexa.ImHome.Play
media_content_type: sequence
service: media_player.play_media
condition: []
If you have an echo in every room you can use the Last Alexa Sensor to turn lights or switches on or off in the room you are currently in.
Here are four variations of turn on the lights
routines you may like to setup.
- "turn on the lights"
- "turn on the light"
- "turn on lights"
- "turn on light"
Don't forget to add the "off" Alexa Routines as well. You could setup more routines for on/off, open/close, raise/lower... you get the idea.
In your Alexa app match your Alexa Routines to following scripts. (Using homeassistant.turn_on
allows you to turn on both light.
, switch.
, and group.
entities.)
scripts.yaml
# "Alexa, turn on the lights"
alexa_turn_on_the_lights:
sequence:
- service: homeassistant.turn_on
data_template:
entity_id: >-
{# Use the name of each Echo to determine which room the command likely came from. #}
{%- set room = states("sensor.last_alexa")|replace('media_player.','') -%}
{%- if room == "kitchen" -%}
group.kitchen_lights
{%- elif room == "family_room" -%}
light.family_room
{%- elif room == "master_bedroom" -%}
light.bedroom
{%- elif room == "play_room" -%}
light.play_room
{%- endif -%}
# "Alexa, turn off the lights"
alexa_turn_off_the_lights:
sequence:
- service: homeassistant.turn_off
data_template:
entity_id: >-
{# Use the name of each Echo to determine which room the command likely came from. #}
{%- set room = states("sensor.last_alexa")|replace('media_player.','') -%}
{%- if room == "kitchen" -%}
group.kitchen_lights
{%- elif room == "family_room" -%}
light.family_room
{%- elif room == "master_bedroom" -%}
light.bedroom
{%- elif room == "play_room" -%}
light.play_room
{%- endif -%}
Bonus Tip: Do you have an echo in one room which overrides the Echo in another room? This limitation can be circumvented by changing the wake word to something different on one of the Echos (ie. change one to "Computer").
Using the Last Alexa Sensor and Alexa routines, scripts can be extended to get room-specific sensor values using the same trigger phrase throughout the house.
scripts.yaml
# "Alexa, room temperature"
alias: Alexa Room-Aware Temperature
sequence:
- service: notify.alexa_media
data:
target: '{{ states("sensor.last_alexa") }}'
data:
type: tts
message: '{{active_therm}}'
mode: single
variables:
active_therm: |-
{% set last = states("sensor.last_alexa") -%}
{% set echo_room = dict([
('media_player.living_room_dot', ['living room', states("sensor.living_room_temperature")] ),
('media_player.basement_dot', ['basement', states("sensor.basement_temperature")] ),
('media_player.kitchen_dot', ['kitchen', states("sensor.kitchen_temperature")] )
]) %}
{%- if last in echo_room -%}
{%- set room = echo_room[last][0] -%}
{%- set temp = echo_room[last][1] -%}
The temperature in the {{ room }} is {{temp}} degrees
{%- else -%}
The average temperature in the house is {{states("sensor.average_household_temp")}} degrees
{%- endif %}
The expansion of the Areas integration in Home Assistant 2021.9 allows templates with the same function as Room Aware Alexa Routines and Room Aware Live Sensor Reports but with reduced maintenance needs. When adding new devices the scripts do not need to be updated manually like previous methods. You will only have to add new entities to their respective groups and assign areas to them:
# "Alexa, turn on the lights"
alexa_turn_on_the_lights:
alias: Alexa Turn On Area Lights
sequence:
- service: light.turn_on
data: {}
target:
area_id: '{{my_area}}'
variables:
my_area: >-
{% set ns = namespace(my_lights = []) %}
{%- set echo_area = area_id(states('sensor.last_alexa')) -%}
{%- for s in states.light | map(attribute='entity_id') | list -%}
{%- if area_id(s) == echo_area -%}
{% set ns.my_lights = ns.my_lights + ['{}'.format(area_id(s)) ] -%}
{%- endif %}
{% endfor %}
{% set active = ns.my_lights| unique | first %}
{{active}}
mode: single
# "Alexa, turn off the lights"
alexa_turn_off_the_lights:
alias: Alexa Turn Off Area Lights
sequence:
- service: light.turn_off
data: {}
target:
area_id: '{{my_area}}'
variables:
my_area: >-
{% set ns = namespace(my_lights = []) %}
{%- set echo_area = area_id(states('sensor.last_alexa')) -%}
{%- for s in states.light | map(attribute='entity_id') | list -%}
{%- if area_id(s) == echo_area -%}
{% set ns.my_lights = ns.my_lights + ['{}'.format(area_id(s)) ] -%}
{%- endif %}
{% endfor %}
{% set active = ns.my_lights| unique | first %}
{{active}}
mode: single
scripts.yaml
# "Alexa, room temperature"
alias: Alexa Room-Aware Temperature
sequence:
- service: notify.alexa_media
data:
target: '{{ states("sensor.last_alexa") }}'
data:
type: tts
message: '{{active_therm}}'
mode: single
variables:
active_therm: |-
{%- set echo_area = area_name(states('sensor.last_alexa')) %}
{%- for s in expand('group.indoor_thermometers')|map(attribute = 'entity_id') | list %}
{% if area_name(s) == echo_area %}
The temperature in the {{ area_name(s)|lower }} is {{states(s)}} degrees Farenheit
{% endif %}
{% endfor %}
While the components source_list only lists input (bluetooth devices that can stream sound to the Alexa device), you can call the select_source service to connect to any previously paired Bluetooth Speaker. Switching it back to "Local Speaker" disconnects it again.
scripts.yaml
alexa_connect_kitchen_speaker:
sequence:
- service: media_player.select_source
data:
entity_id: media_player.echo_living_room
source: "Kitchen Speaker."
alexa_disconnect_kitchen_speaker:
sequence:
- service: media_player.select_source
data:
entity_id: media_player.echo_living_room
source: "Local Speaker."
This automation will announce the time remaining on a timer at 5 minute intervals, if the timer is for longer than 5 minutes.
- id: timer_announcement
alias: Timer Announcement
trigger:
- platform: template
value_template: >
{%- if states.sensor.kitchen_next_timer.state != "unavailable" -%}
{%- set sorted_active = states.sensor.kitchen_next_timer.attributes.sorted_active | from_json -%}
{%- set duration = (sorted_active[0][1].remainingTime / 60000) | round(0) -%}
{%- set remaining = (((as_timestamp(states.sensor.kitchen_next_timer.state) - as_timestamp(states.sensor.time.last_changed)) / 60) | int) -%}
{{ duration > 5 and remaining != duration and remaining != 0 and remaining % 5 == 0 }}
{%- endif -%}
action:
- service: notify.alexa_media_kitchen
data:
message: >-
{%- if states.sensor.kitchen_next_timer.state != "unavailable" -%}
{%- set sorted_active = states.sensor.kitchen_next_timer.attributes.sorted_active | from_json -%}
{%- set duration = (sorted_active[0][1].remainingTime / 60000) | round(0) -%}
{%- set remaining = (((as_timestamp(states.sensor.kitchen_next_timer.state) - as_timestamp(now())) / 60) | int) -%}
You have {{ remaining }} {{ "minutes" if duration > 1 else "minute" }} left on your {{ duration }} minute timer.
{%- endif -%}
data:
type: announce
method: all
(Replace sensor.kitchen_next_timer and notify.alexa_media_kitchen as required for your setup.)
Note: This may have issues determining the duration of the timer if there are multiple overlapping times. If this is an issue, you may need to remove the duration from the announcement (the time remaining should still work).
This automation will notify a phone when the alexa_media_player_relogin_required
event is fired. This happens when Amazon requires the user to answer a CAPTCHA question. The login form can be access through the Persistent Notification drawer of Home Assistant.
- id: alexa_media_player_authentication_required
alias: Alexa Media Player Authentication Required
trigger:
platform: event
event_type: alexa_media_relogin_required
condition: []
action:
- service: notify.mobile_app
data:
title: Alexa Media Player Integration
message: 🔑 Authentication Required
data:
actions:
- action: URI
title: Resolve now!
uri: /lovelace/home
Note: The notify.mobile_app
includes an actionable notification in order to simply open your Lovelace dashboard and quickly access the Persistent Notification drawer. (Reported working on Android)