Using Alexa Media Player “last called” service

In a recent Home Assistant podcast the guest mentioned how he could say, “Alexa, turn off TV”, and HA would know which TV he meant because it knew which Echo he was speaking to. I think this must be using the “last called” service in Alexa Media Player.

Eg. The bedroom tv would turn off because the request came from the bedroom Echo.

I can’t figure out how I would be able to set this up? Any ideas please?

Without more information it’s hard to tell you where to start…

In order for this to work, in addition to the Alexa Media Player custom integration, you must have a method for Alexa to access Home Assistant scripts (FYI: Alexa considers them scenes). To do this you can use Nabu Casa, Haaska, Amazon Alexa Smart Home Skill, or maybe some other/newer method that I don’t know about…

For TV’s you just follow everything under the Room-Aware Routines , but replace 'lights" with “tv” and put your tv entity_ids in the scripts.

The order of operations is:

  1. Setup your Alexa to Home Assistant connection
  2. If you haven’t already, create your group of Alexas and the Last Alexa Sensor
  3. Create the Scripts in Home Assistant
  4. Create the routines in the Alexa App

Here you go:

Thanks, both. I had read the Alexa Media Player Wiki, but wasn’t sure about how to adapt it to my need. The key, however, was the section on “Room Aware Routines”.

I got it working with the following (after creating the Last Alexa Sensor, as mentioned above):

sequence:
  - service: media_player.turn_off
    data_template:
      entity_id: >-
        {%- set room = states("sensor.last_alexa")|replace('media_player.','')
        -%}

        {%- if room == "bedroom_echo_james" -%}
          media_player.samsung_tv_remote_2
        {%- elif room == "bedroom_echo_krissy" -%}
          media_player.samsung_tv_remote_2
        {%- elif room == "living_room_echo" -%}
          media_player.samsung_tv_remote
        {%- elif room == "office_echo" -%}
          media_player.samsung_tv_remote
        {%- endif -%}

Hi guys,

I’m trying to use the last_alexa as was described, but the problem is that the attributes.last_called is not updated. Each time I get the same last_alexa, doesn’t matter to which dot I’m speaking.

groups.yaml

  echos:
    name: All Echos
    entities:
      - media_player.phone
      - media_player.echo_dot
      - media_player.fire

template.yaml

  - sensor:
      - name: last_alexa
        state: >
          {{ expand('group.echos') | selectattr('attributes.last_called','eq',True) | map(attribute='entity_id') | first }}

scripts.yaml

  alexa_id_test:
    sequence:
      - service: notify.alexa_media
        data_template:
          message: >
            Value is {{ states('sensor.last_alexa') }}
          target: "{{ states('sensor.last_alexa') }}"
        data:
          data:
            type: tts

For example, I’m executing the script by speaking to Alexa from my phone. After the execution the message: “Value is media_player.echo_dot” is read by my echo_dot device instead of my phone. If I restart the hassio, sometimes is read by the phone every time doesn’t matter to which device I’m speaking. Seems that it remain the first selected device and never update to the last_called.

Can you please help me?

@Sor You have some errors in your script… “data_template” is deprecated, and you have too many "data"s (See Below)

However, a couple months ago Amazon changed something about how the devices report their last_called status and it has been unreliable ever since. There have been some attempts to figure out a work around, but none have sucessfully fixed the issue.

For now, you can try adding a delay before your notify service call, but it’s hit-or-miss whether that helps.

FWIW: The integration added an automatic last called notification target in v3.6.0 so you do not have to use the template sensor for notifications (It still suffers from the reliability issue)

scripts.yaml

  alexa_id_test:
    sequence:
      - service: notify.alexa_media_last_called
        data:
          message: >
            Value is {{ states('sensor.last_alexa') }}
          data:
            type: tts

@Didgeridrew Thank you for your answer. I’ll integrate your suggestions.

I had to disable my scripts that used this service, because I found that it took around 10 seconds for the “last called device” to update, which made it unusable.

Same behavior, the last_alexa is not updated.

What the heck, I spent about two hours last night trying to make it speak on last called device and thinking all the time I’m doing something wrong :grin: , now I found this topic and @Didgeridrew post above (thanks).
Hope that someone will find workaround, cause current state is exactly hit or miss, with or without delay. To be honest, I think that delay is not doing anything. I have 2 echo speakers and I was monitored sensor.last_alexa last night. If being updated, it is updated immediately. If not, then it is not updated even after several minutes.

Hi. Any update to this? I just posted this on the GitHub
https://github.com/custom-components/alexa_media_player/issues/1369#issuecomment-1575878668

Reviving this thread. Has anything progressed on this front to get reliable room awareness? I find that last_called either updates almost immediately, or goes for hours without updating despite many calls to Alexa all over my house.

I’ve seen some improvement using the following to force updates:

  - service: alexa_media.update_last_called
    data: {}
  - delay:
      milliseconds: 500
  - service: homeassistant.update_entity
    data: {}
    target:
      entity_id: sensor.last_alexa
  - delay:
      milliseconds: 500
  - service: notify.alexa_media_last_called
    data:
      message: | 
        Lorem ipsum dolor sit amet.
      data:
        type: tts

Just FYI, if you are using any templating in your messages that relies on the last alexa sensor, make sure to define your variables in an action after the second delay.

Thanks for the code suggestion. I added this to my room aware Alexa routines and will see how it performs. Alexa seems to be a in a fast update mode at the moment, but I have been noticing issues in the evening in recent nights so we will hopefully see tonight.

Hi,

I have the same symptoms with the sensor updating randomly. Sometimes it updates straight away other times randomly. I initially started with a 750ms delay.

It seems that restarting the HA container seems to help the issue or otherwise it may be a coincidence.

I have implemented the work arounds by Didgeridrew. maybe that will help.

@didgeridrew,
I realize this topic is old and cold. I am just hoping you may have an answer for my question.
I have the sensor.last_alexa" working fine. However; I would like to add my mobile device, Alexa app to my group of Alexas so that I can integrate when I am out mobile. I am not sure how to capture that device name…
Is that possible? Thanks in advance!