Skip to content
swissgamedev edited this page Dec 26, 2022 · 23 revisions

Setting up your first actionable notification

Now that you have the skill setup and configured, it is time to setup your first actionable notification.

Creating the script.

Below is a helpful script that will allow you to trigger actionable notifications easily within your automations.

script:
  activate_alexa_actionable_notification:
    description: 'Activates an actionable notification on a specific echo device'
    fields:
      text:
        description: 'The text you would like alexa to speak.'
        example: 'What would you like the thermostat set to?'
      event_id:
        description: 'Correlation ID for event responses'
        example: 'ask_for_temperature'
      alexa_device: 
        description: 'Alexa device you want to trigger'
        example: 'media_player.bedroom_echo'
    sequence:
      - service: input_text.set_value
        data:
          entity_id: input_text.alexa_actionable_notification
          value: '{"text": "{{ text }}", "event": "{{ event_id }}"}'
      - service: media_player.play_media
        data:
          media_content_type: skill
          media_content_id: <Your Skill ID>
        target:
          entity_id: "{{ alexa_device }}"

Replace <Your Skill ID> with the skill id for the custom skill you created. You can find this on the Alexa Developer Console by clicking "View Skill ID" on the skill you created in the previous step.

Triggering the Actionable Notification

Using the script above we can trigger the actionable notification in any automation similar to the below example code:

automation:
  - alias: Front door left unlocked for 5 mins
    trigger:
      platform: state
      entity_id: lock.front_door
      to: 'on'
      for:
        minutes: 5 

    action:
      - service: script.activate_alexa_actionable_notification
        data_template:
          text: 'The front door has been unlocked for 5 mins, would you like me to lock it?'
          event_id: 'actionable_notification_lock_left_unlocked'
          alexa_device: 'media_player.living_room_echo'

Performing actions on user response.

When the user says yes or no the custom component will trigger an event in Home Assistant that can be used for automations to perform actions based on how the user responds. For example, the above automation is using the event_id: actionable_notification_lock_left_unlocked so if they respond with "yes" you can capture that even with an automation like:

automation:
  - alias: Lock the door via actionable notification
    trigger:
      platform: event
      event_type: alexa_actionable_notification
      event_data:
        event_id: actionable_notification_lock_left_unlocked
        event_response_type: ResponseYes
        
    action:
      - service: lock.lock
        entity_id: lock.front_door

There are six different event_response_types types you can trigger off of.

  1. ResponseYes - The response was a "yes"
  2. ResponseNo - The response was a "no"
  3. ResponseNone - There was no response
  4. ResponseNumeric - The response was a numeric value
  5. ResponseDuration - The response was a duration
  6. ResponseSelection - The response was a predefined selection as defined by the Advanced Features section.

Note: There is no requirement to create automations for a specific response. If saying "no" should perform no action in home assistant, there is no need to create an automation for it.

Adding additional event responses.

Please see the Advanced Features Wiki section

Other Fun Stuff

In your automation the text you want Alexa to speak supports the full range of Speech Synthesis Markup Language. This allows you to create automations where the skill will speak the test exactly as you define it.

For example to whisper the question, simply change the text to:

    action:
      - service: script.activate_alexa_actionable_notification
        data:
          text: "<amazon:effect name='whispered'>The front door has been unlocked for 5 mins, would you like me to lock it?</amazon:effect>"
          event_id: "actionable_notification_lock_left_unlocked"
          alexa_device: "media_player.living_room_echo"

NOTE: Not setting event_id will allow you to only make alexa say something and not expect a response nor trigger an event.

Thank you

If you've made it this far, let me thank you! It is the encouragement of the community that continues to inspire me to find new and creative ways to take home automation to the next level. Please, if you like what you see, consider buying me a coffee to help fuel my late nights :)

coffee