Examples: Automation - alandtse/alexa_media_player GitHub Wiki

Guides

Playing media

Using tts (does not work with the everywhere speaker group)

# Script
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
      message: 'Ding-dong. Someone is at the door.'
      target:
        - media_player.downstairs
        - media_player.upstairs

Using announce (plays a chime before the text)
:bulb: Try using templating to play personalized info.

# Automation
- 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
      message: 'The washing machine is finished. Get off the couch.'
      target:
        - media_player.downstairs
        - media_player.upstairs

Using routines
:bulb: First you have to create a routine in the Alexa app. Make sure to use media_content_type: routine and set media_content_id: so it exactly matches the name of your Routine in the Alexa App.
:bulb: If you have a Fire TV Cube with external speakers, you can't easily control them. However if you can control them through the Cube (Settings > Device Control), you can create routines to control the power and volume, and run the routines with buttons.

# Script
remco_awake:
  alias: Remco woke up
  sequence:
  - service: media_player.play_media
    data:
      media_content_id: remcowakeup
      media_content_type: routine
    target:
      entity_id: media_player.upstairs
  - wait_template: '{{ is_state("sensor.broadlink_s1c_living_room_door", "open") }}'
  - service: media_player.play_media
    data:
      media_content_id: "I've Gone Downstairs"
      media_content_type: routine
    target:
      entity_id: media_player.downstairs

Using sequence commands
:bulb: Sequence names need to be exact to work.

# Automation
- 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: []

Room-aware routines

Room-aware lights
:bulb: If you create a room in the Alexa app with a light and an Alexa, saying "turn on the lights" to the Alexa will turn on the light. This is another way to do that. You shouldn't do this, but this could help you understand the Last Alexa sensor.

  1. Set up the the Last Alexa sensor
  2. Set up something like this for both turning lights on and off:
# Script
alexa_turn_off_the_lights:
  sequence:
    - service: homeassistant.turn_off
      data_template:
        entity_id: >-
          {%- 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 -%}
  1. Expose the scripts, and create a matching Alexa Routine for each script.

Room-aware temperature
:bulb: You would need to expose this script, and create a routine to call it. This script could be modified so you can request other data.

# Script
alias: Alexa Room-Aware Temperature
variables:
  echo_room:
    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")}}'
  active_therm: |-
    {% set last = states("sensor.last_alexa") -%}  
    {%- 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 %}
sequence:
  - service: notify.alexa_media
    data:
      target: '{{ states("sensor.last_alexa") }}'
      data:
        type: tts
      message: '{{active_therm}}'
mode: single

Room-aware lights, using HA Areas
:bulb: Same warning and instructions as the previous room-aware lights.

# Script
alexa_turn_off_the_lights:
  alias: Alexa Turn Off Area Lights
  sequence:
  - service: light.turn_off
    data:
      area_id: '{{area_id(states("sensor.last_alexa"))}}'
  mode: single

Room-aware temperature, using HA Areas
:bulb: You would need to expose this script, and create a routine to call it.

# Script
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 %}

Selecting bluetooth speakers

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
alexa_connect_kitchen_speaker:
  sequence:
    - service: media_player.select_source
      data:
        source: "Kitchen Speaker."
      target:
        entity_id: media_player.echo_living_room
alexa_disconnect_kitchen_speaker:
  sequence:
    - service: media_player.select_source
      data:
        source: "Local Speaker."
      target:
        entity_id: media_player.echo_living_room

Time remaining announcements

Notes: 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 requires the Time & Date to be enabled.

# Automation
- id: timer_announcement
  alias: Timer Announcement
  description: 'Announce the time remaining on a timer at 5 minute intervals, if the timer is for longer than 5 minutes.'
  trigger:
    - platform: template
      value_template: >
        {% if not states('sensor.kitchen_next_timer') in ["unavailable", "unknown"] -%}
        {%- set sorted_active = state_attr('sensor.kitchen_next_timer', 'sorted_active') | from_json -%}
        {%- set duration = (sorted_active[0][1].originalDurationInMillis / 60000)|int %}
        {% set remaining = ((as_datetime(states('sensor.kitchen_next_timer')) - states.sensor.time.last_changed|as_local).seconds // 60) -%}
        {{ duration > 5 and remaining != duration and remaining != 0 and remaining % 5 == 0 }}
        {% endif -%}
  action:
    - service: notify.alexa_media_kitchen
      data:
        message: >
          You have {{ time_remaining }} {{ "minutes" if time_remaining > 1 else "minute" }} left on your {{ timer_duration }} minute timer.
        data:
          type: announce
          method: all
  mode: single
  variables:
    timer_duration: >
      {% if not states('sensor.kitchen_next_timer') in ["unavailable", "unknown"] -%}
      {%- set sorted_active = state_attr('sensor.kitchen_next_timer', 'sorted_active') | from_json -%}
      {{ (sorted_active[0][1].originalDurationInMillis / 60000)|int }}
      {% endif %}
    time_remaining: >
      {% if not states('sensor.kitchen_next_timer') in ["unavailable", "unknown"] -%}
      {{ ((as_datetime(states('sensor.kitchen_next_timer')) - states.sensor.time.last_changed|as_local).seconds // 60) }}
      {% endif %}

Notification when Amazon login required

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 accessed through the Persistent Notification drawer of Home Assistant.

# Automation
- 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