Run a HA notification script on the alexa device that receives the trigger - ideas welcome

Hi everybody, I made a script in HA to ask alexa who’s at home. The script for the notification is triggered by a phrase in alexa and works fine so far. But the problem is: I have more than one alexa and I can only hard code, which device will answer me (in the example it’s media_player.schlafzimmer). Better solution would be: The device that receives the trigger, answers me. Any ideas for solution? Thank you very much.

Here the script in HA:

sequence:
  - if:
      - condition: state
        entity_id: person.nina
        state: home
    then:
      - service: notify.alexa_media
        data:
          message: Nina
          data:
            type: announce
            method: speak
          target: media_player.schlafzimmer
  - if:
      - condition: state
        entity_id: person.jonas
        state: home
    then:
      - service: notify.alexa_media
        data:
          message: Jonas
          data:
            type: announce
            method: speak
          target: media_player.schlafzimmer
  - if:
      - condition: state
        entity_id: person.ellen
        state: home
    then:
      - service: notify.alexa_media
        data:
          message: Ellen
          data:
            type: announce
            method: speak
          target: media_player.schlafzimmer
  - if:
      - condition: state
        entity_id: person.elke
        state: home
    then:
      - service: notify.alexa_media
        data:
          message: Elke
          data:
            type: announce
            method: speak
          target: media_player.schlafzimmer
  - if:
      - condition: state
        entity_id: person.admin
        state: home
    then:
      - service: notify.alexa_media
        data:
          message: Hannes
          data:
            type: announce
            method: speak
          target: media_player.schlafzimmer
  - if:
      - condition: state
        entity_id: person.antje
        state: home
    then:
      - service: notify.alexa_media
        data:
          message: Antje
          data:
            type: announce
            method: speak
          target: media_player.schlafzimmer
mode: queued
icon: mdi:account-group
max: 10

And here the routine in alexa:

The Alexa Media Player integration creates a variable last_alexa - could you use that?

1 Like

As Stiltjack commented, Alexa Media Player tracks the last_called property… but it goes one further and creates a specific service you can use:

- service: notify.alexa_media_last_called
  data:
    message: 
    data: 
      type: tts

Be aware that the way you have constructed the automation will be problematic because your service calls to the Alexa servers will overlap each other and it is likely that not all the names will be spoken. You can solve that a couple ways:

  1. Add a 2-5 second delay between each If action.
  2. Use a template to join all the names into a single message.
sequence:
  - variables:
      anyone_home: "{{ state_attr('zone.home', 'persons')|count > 0 }}"
  - condition: template
    value_template: "{{ anyone_home }}"
  - service: notify.alexa_media_last_called
    data:
      message: "{{ expand(state_attr('zone.home', 'persons')) | map(attribute='name') | join(', ') }}"
      data: 
        type: tts
mode: single
icon: mdi:account-group
1 Like

Thank you for your help.

Okay, next step is done. But the alexas have different accounts and the message is spoken only by the alexa of my daughter. Now I tried to define the “last_alexa” via groups.yaml and configuration.yaml as described here:
https://github.com/custom-components/alexa_media_player/wiki#last-called-device