HA to respond to echo spoken to

Hi,

I was wondering if there was a way for HA to respond to the alexa device you spoke to? For example, I will tell one of the echoes to run a script in HA and I’d like HA to respond to that specific echo device.

Alexa Media Player custom integration

1 Like

Yes, I tried this a week or so ago and found it wasn’t working properly so I thought maybe it’s not designed to do what I want.

This is my script that i call from alexa but it doesn’t speak to the device i spoke to. Is there more that needs to be adjusted?

test_script:
  alias: Test Script
  sequence:
  - service: notify.alexa_media_last_called
    data:
      message: Hello
      data:
        type: tts
  mode: single

It seems that for me notify.alexa_media_last_called responds from whichever alexa device I had last asked Alexa a “normal” question. By “normal” I mean not a Home Assistant triggered response. I have tried explicitly calling alexa_media.update_last_called but this does not seem to make any difference.

Did you have any luck finding a solution or understanding the issue better?

Can you post an example of what you are talking about?

I’ll try to describe it in more detail:

  • I have an alexa routine triggered by an alexa voice command. For example “alexa, what is the status of the boiler”.
  • The alexa routine actions trigger a home assistant script I have written to handle the command.
  • The home assistant script does some stuff and then uses the home assistant alexa media player to respond using alexa_media_last_called.

For example:

service: notify.alexa_media_last_called
data:
  message: >-
    The boiler is not currently active but the target flow temperature would be
    {{ states('sensor.boiler_flow_target_temperature') }} degrees.
  data:
    type: tts

The response is read from whichever alexa device I had interacted with previously, rather than from the device I had just spoken the voice command to that triggered the routine that triggered the home assistant script.

At first I thought this might just be a race condition. But asking alexa again from the same device still responds from the previous device. It continues to respond from the wrong alexa device until I ask the Alexa device a “normal” voice command which it handles itself, such as “alexa, what is the time”. After that, home assistant starts responding from that device.

I tried manually calling the update_last_called service to see if that would help, but it didn’t make a difference to which alexa device responded.

1 Like

I have added a forced update and delay into all my scripts like that…

sequence:
  - service: alexa_media.update_last_called
    data: {}
  - delay: '00:00:02'
  - service: notify.alexa_media_last_called
    data:
      data:
        type: tts
      message: '{{active_windows}}'
variables:
  active_windows: >-
    {% set ow = expand('binary_sensor.all_window_sensors')|
    selectattr('state','eq','on')  | map(attribute='entity_id') |
    map('area_name')  | unique | reject('eq',None) | sort | map('lower') | list
    %} {% set qty = ow | count %} {% if qty != 0 %} There {{'is an' if qty==1
    else 'are'}} open window {{' ' if qty==1 else 's '}}in the {{' and
    '.join((ow|join(', ')).rsplit(', ', 1)) }}. {% else %} All windows are
    closed. {% endif %}
mode: single

A 2 second delay seems to be the minimum that I can use to consistently get a response from the echo I triggered the routine from. YMMV

1 Like

I tried that and it didn’t seem to help in my case. I’m not sure what is different with my setup, but it continues to respond from the wrong echo even hours later.

For example:

  • In the kitchen I say “Alexa, what is the weather” and Alexa responds using the kitchen echo (all handled by Alexa without any home assistant interactions).
  • I go to the living room and ask “Alex, what is the boiler status” (handled by a home assistant script) and Alexa responds from the kitchen echo instead of living room echo, even when the home assistant script explicitly calls alexa_media.update_last_called.
  • An hour later, still in the living room, ask “Alexa, what is the boiler status” and alexa still responds from the kitchen.
  • Say “Alexa, what is the weather”, and Alexa responds using the living room echo.
  • Say “Alexa, what is the boiler status”, and Alexa now responds from the living room echo (as desired).

I’m at a loss, but will continue to investigate.

Based on my analysis of the debug logs and reading of the code I think there’s a race condition where the newly active media device marks itself as last_called triggers an update of the alexa_media_last_called target, but the old media device has not yet marked itself as not being the last_called. Depending on the order of the list of devices this can result in the older media device being set as the target for alexa_media_last_called.

The workaround in a script would be to wait for 5 seconds before using alexa_media_last_called. If you do this then the code spots that it has failed to set the target to the desired value and retires using a slow toggle (unregistering the corresponding service and re-registering it). This process takes around 4 seconds. That code was intended to cope with old versions of HA which did not allow the service target to be changed, so you will see a debug log about old version of HA which can be a bit confusing. But it does the trick of setting things right in this race condition.

I’m experimenting with a code fix I have written and will submit an issue and/or PR in a couple of days time.

In the meantime anyone who is running into this issue can use the 5 second wait in their script as a workaround. In my Alexa routine I get Alexa to respond “On it” (or similar) to give some feedback so the 5 second pause before the response from HA doesn’t feel too unnatural.

1 Like

I’ve raised a github issue and PR that I think fixes the issue:

The fix has been merged now so will hopefully be in the next release of the Alexa media player.

1 Like

Hi, I realise that this thread is over a year old now but I’m getting the same issue. It’s making the announcement on the previous echo called and not the one that I am actually speaking to. Do you know if this issue was fixed?

Thanks!

It is fixed as far as I know. I don’t have any issues (or only rarely) with my scripts announcing from the wrong speaker anymore. I use alexa_media.update_last_called as the first step in my script sequence, followed by notify.alexa_media_last_called.

1 Like