alexa_media_player: last_called state attributes showing as null; api/activities returned 404:Not Found:text/html

IMPORTANT: Please search the issues, including closed issues, and the FAQ before opening a new issue. The template is mandatory; failure to use it will result in issue closure.

Describe the bug

As the subject says: image

To Reproduce

  1. Say “alexa”
  2. last_called used to change
  3. Now stays blank

Expected behavior

Screenshots Happens on all devices: image

System details

  • Home-assistant (version): 2023.10.5
  • alexa_media (version from const.py or HA startup): 4.7.7
  • alexapy (version from pip show alexapy or HA startup):
  • Amazon 2FA is enabled (y/n). <!—We will not debug login issues if unanswered—>: yes

Logs Please provide logs.

home-assistant_2023-10-25T09-39-31.640Z.log

Additional context

About this issue

  • Original URL
  • State: closed
  • Created 8 months ago
  • Reactions: 11
  • Comments: 131 (4 by maintainers)

Most upvoted comments

Hi. I fix the issue in alexapy. But… It could not be perfect. Especially as far as the wake word is concerned. But basically it works. I have made a merge request: https://gitlab.com/keatontaylor/alexapy/-/merge_requests/361 Request to Contributors. Please check the changes and improve them if necessary. What is the next step? If it is ok, this version must be published on pypi. Who will do it? Then the new version must be referenced in alexa media player. Who does this? Please, I think I have done the roughest part. Can someone take care of the rest? Thank you

EDIT: I didn’t fix clear_history.

You can try replacing the url in alexapy and perhaps the component will have the right auth.

The delay was always there since it has to round trip up to Amazon and then back. It’s possible this api is slower. I forgot if we rely on the http2 push stream for the data before the url returns but it seems like something I would’ve done before.

@alandtse I tried this but the current auth is not sufficient.

I have narrowed down the request to a bare minimum set of headers and tokens that this api endpoint requires before rejecting us, but TBH I am not sure how to actually get real valid values for all of these (.co.uk used in the example below, REQUIRED_VALUE placeholder needs to be substituted for real & current valid values):

curl 'https://www.amazon.co.uk/alexa-privacy/apd/rvh/customer-history-records-v2?startTime=REQUIRED_VALUE&endTime=REQUIRED_VALUE' \
  -H 'anti-csrftoken-a2z: REQUIRED_VALUE' \
  -H 'content-type: application/json;charset=UTF-8' \
  -H 'cookie: REQUIRED_VALUE' \
  -H 'referer: https://www.amazon.co.uk/alexa-privacy/apd/rvh' \
  -H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36' \
  --data-raw '{"previousRequestToken":"REQUIRED_VALUE"}'

If you copy and run the request from your browser using curl you will get a JSON response of this format (customerHistoryRecords[0] being the most recent, with device.deviceName being the useful value for us) :

{
    "customerHistoryRecords": [
        {
            "recordKey": "...",
            "recordType": "VOICE_HISTORY",
            "timestamp": 1698406415222,
            "customerId": "...",
            "device": {
                "deviceName": "Office Alexa Show",
                "deviceEntityId": null
            },
            "isBinaryFeedbackProvided": false,
            "isFeedbackPositive": false,
            "utteranceType": "GENERAL",
            "domain": "Global",
            "intent": "UnMuteIntent",
            "skillName": "",
            "voiceHistoryRecordItems": [
                {
                    "recordItemKey": "...",
                    "recordItemType": "CUSTOMER_TRANSCRIPT",
                    "utteranceId": "..",
                    "timestamp": 1698406414732,
                    "transcriptText": "unmute",
                    "agentVisualName": "",
                    "personsInfo": []
                }
            ],
            "personsInfo": [],
            "cardResponse": null
        },
        { ... },
        { ... },
        { ... }
    ],
    "encodedRequestToken": "",
    "noDataFoundWithinTimeLimit": false,
    "lastRecordTimestamp": null,
    "voiceProfileAssociatedWithPerson": false
}

I use the “last alexa” for a lot of automations and it is very important in HA. All of this is useless now. Please someone help.

me too

To install @Fredo70’s branch while we wait for merges, launch your HA Terminal and…

wget https://gitlab.com/Fredo70/alexapy/-/archive/dev/alexapy-dev.zip
unzip alexapy-dev.zip
mv alexapy-dev/alexapy config/
rm -rf alexapy-dev*
ha core restart

@alandtse could you take a look into that PR?

I feel like everyone just has to stop trying to revive Alexa already, and ditch it for local voice control. It will be painful, but it’s like bandaging rotten leg again and again instead of cutting it off…

I sometimes use Alexa for more than just HA automation and I fail to see how HA Assist can answer a question like “Alexa, how long does it take a banana to ripen?” or “Alexa, what is the temperature in Puerto Rico?”. I’m still interested in HA Assist though but wondering if anyone has figured out how to gut an Amazon Echo and replace with ESP32 and HA Assist??

On a related note, DFROBOT’s Gravity: Offline Language Learning Voice Recognition Sensor for Arduino / Raspberry Pi / Python / ESP32 - I2C & UART is quite interesting…

I’m using a bit of a silly workaround but all my HA automations that were relying on “last_called” are now working again seamlessly as before.

I added to my alexa routines (which used to call my scripts and my scripts determined which alexa it came from using the last called attribute), I am now adding for it to set the volume to 10 (1.0)… then I created my own “last_called” sensor that checks for volume to be at 1.0 and uses that to track which alexa was called.

The Alexa routine looks like this: Set Volume to 10 (100% or 1.0 in HA) > Call HA Script (check for which device is at volume 100% (1.0), execute script > Wait 5 seconds (minimum in alexa routines) > Set Volume to 3.

The Binary Sensor in configuration.yaml is like this. template:

  • binary_sensor:
    • name: LAST ALEXA CALLED - OFFICE state: “{{ is_state_attr(‘media_player.office_echo_studio’, ‘volume_level’, 1) }}”

P.S - The volume attribute seems to update almost immediately, so you could just add a 1 second delay in your HA script before your actions are called and it should work faster than before since the last_called attribute actually had about a 3 second or so delay.

I use the “last alexa” for a lot of automations and it is very important in HA. All of this is useless now. Please someone help.

@alandtse could you take a look into that PR?

I feel like everyone just has to stop trying to revive Alexa already, and ditch it for local voice control. It will be painful, but it’s like bandaging rotten leg again and again instead of cutting it off…

I have 9 echo devices in the house and I have them very well integrated with the HA ecosystem (90+ devices integrated, including Wifi, ZWave and Zigbee). Everything works perfectly. Besides HA integration, Alexa provides a lot of value for information access, etc… which honestly, I would pay a nominal fee to Amazon even to keep it going, improving and opening access to it. So, use of Alexa is much broader than just HA integration. The update_last_called / notify last called is a great way to achieve pseudo request/response for those Alexa routines that call HA scripts (as scenes) that return information back - since there is no other way for a script to send back response to the device that was spoken to (I wish there was). The use of https://www.amazon.com/alexa-privacy/apd/rvh in the update to alexaapy by Fredo70 seems to be a good approach (I know it is webscraping and not a real API) that I would love to see the Alexa Media Player custom integration uptake as it gets proven out - because currently the scripts that return information back are all failing because of “Unable to find service notify.alexa_media_last_called” error.

@carlos33193 Thanks for the inspiration. I made a trigger-based sensor for this, so it updates whenever the volume of one is changed to be 1.0, and keeps that value until it’s replaced by another device. That way I can easily reset the volume (I have a separate script that sets different ones to different hard-coded values) of all devices before playing tts without having to set variables to avoid it screaming at me.

- trigger:
    - platform: template
      value_template: |-
        {{ expand(integration_entities('alexa_media') | select('search', 'media_player')) | selectattr('attributes.volume_level', 'defined') 
        | selectattr('attributes.volume_level', 'eq', 1)
        | map(attribute='entity_id') | first is defined }}
  sensor:
    - name: Last Alexa Volume Based
      unique_id: last_alexa_volume_based
      state: |-
        {{ expand(integration_entities('alexa_media') | select('search', 'media_player')) | selectattr('attributes.volume_level', 'defined') 
        | selectattr('attributes.volume_level', 'eq', 1)
        | map(attribute='entity_id') | first }}

But maybe there is hope. If you call https://alexa.amazon.de/api/activities Then you see the following: generic.png

So, maybe the Service is just temporarly unavailable and works again in a few days

Unfortunately this isn’t working 100% of the time. Where another Alexa picks up the same command and they decide which one should handle the message, this sometimes throws things off (the wrong Alexa is set as last alexa).

Looks like we need to ignore items in the history with attribute: “utteranceType”:“DEVICE_ARBITRATION”

Have added a comment on the pull request as such.

To install @Fredo70’s branch while we wait for merges, launch your HA Terminal and…

wget https://gitlab.com/Fredo70/alexapy/-/archive/dev/alexapy-dev.zip
unzip alexapy-dev.zip
mv alexapy-dev/alexapy config/
rm -rf alexapy-dev*
ha core restart

Confirmed. Commands are accurate and procedure is valid. Results in a working “last alexa” sensor. Kudos to @dlgoodr

For those who are having difficulty like I was, I will show how I performed the procedure on a Debian terminal.

To access the directory just run the command “docker exec -it homeassistant bash”, then run “cd …” to return to the root directory. Then just explore the folders, with the command “cd /usr/local/lib/python[your python version]/site-packages/alexapy/”

To start, enter “docker exec -it homeassistant” in a terminal that is not running:

1-To make the change, upload the alexapy folder (https://gitlab.com/Fredo70/alexapy/-/tree/dev/alexapy?ref_type=heads) somewhere on your server. 2-For safety, make the backup with the command “docker cp homeassistant:/usr/local/lib/python[version]/site-packages/ /backup”. 3-After that, remove the old alexapy folder by running the command “docker exec homeassistant rm -r /usr/local/lib/python[version]/site-packages/alexapy”. 4-Move the new alexapy folder to docker with the command “docker cp /folder-on-host/alexapy homeassistant:/usr/local/lib/python3.11/site-packages”. 5-Restart docker with “docker restart homeassistant”

Do it at your own risk. I performed the procedure and it resolved the problem.

thanks @Fredo70 !

It works like a charm! Big thanks!!! I have replaced the modified alexaapi.py within the container. It works. Thanks thanks thanks

My current solution, a bit complicated but works okay.

  • all routines start with volume 2.
  • or your desired volume, and also adjust it in the sensor.
  • All scripts must end with a time delay and volume 0.

sensor

    template:
      - sensor:
          - name: Last Alexa
            state: >
              {{ expand('group.all_alexa_echos') | selectattr('attributes.volume_level','eq', 0.2) | map(attribute='entity_id') | first }}
            availability: >
              {{ expand('group.all_alexa_echos') | selectattr('attributes.is_volume_muted','eq', True) | first is defined }}

script

alias: Test Last Alexa State
sequence:
  - service: notify.alexa_media
    data:
      data:
        method: all
      message: |
        *
      title: empty message to activate
      target: "{{states.sensor.last_alexa.state}}"
    enabled: true
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - service: notify.alexa_media
    data:
      data:
        method: all
      message: |
        The temperature is {{ states('sensor.terrassen_temperatur') }}°C
      title: your script
      target: "{{states.sensor.last_alexa.state}}"
    enabled: true
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - service: media_player.play_media
    data:
      media_content_id: volume 0
      media_content_type: custom
    target:
      entity_id:
        - media_player.alexa_echo_dot
        - media_player.home_fire_tab
    enabled: true
mode: single
icon: mdi:thermometer

Does setting the volume to 0 make the echo boop when the volume changes? I quite like this as a solution, but the constant boop of changing volumes bugs me so I have an automation that sets them all to 4 during the day and 2 in the evening and at night.

@Fredo70 stand down as it looks like the integration does already handle this. I’m seeing this in the HA logs:

[alexapy.alexaapi] Backing off _static_request(…) for 0.6s (alexapy.errors.AlexapyTooManyRequestsError: Too Many Requests)

Looks like it tries 5 times then gives up, and I’ve just been unlucky and had five in a row. Maybe we should up the retries to 10, but apart from that not much we can do.

@master-kenobi

Thanks @Fredo70. Everything works like it should now. The alexapy folder is now added to my config folder. I assume once it is merged into the official release, I can remove the folder?

you should absolutely remove the folder after it’s in the official release. you won’t get new updates if you don’t.

Thanks @Fredo70. Everything works like it should now. The alexapy folder is now added to my config folder. I assume once it is merged into the official release, I can remove the folder?

To install @Fredo70’s branch while we wait for merges, launch your HA Terminal and…

wget https://gitlab.com/Fredo70/alexapy/-/archive/dev/alexapy-dev.zip
unzip alexapy-dev.zip
mv alexapy-dev/alexapy config/
rm -rf alexapy-dev*
ha core restart

Confirmed. Commands are accurate and procedure is valid. Results in a working “last alexa” sensor. Kudos to @dlgoodr

Yes. Same scripts are working now untouched. Thanks much to all for this.

Will these changes get overriden when I update to newer version of HA?

I would expect this to be included in an official update to the Alexa_Media_Player custom integration, as long as it pans out

Using Advanced SSH & Web Terminal (with Protection mode turned off):

Run the command “docker exec -it homeassistant bash”. This will give you access to /usr/local/lib/python[your version here]/site-packages/alexapy/

Linux commands to backup/rename the original file, and then copy the new file to the location of the original. I am by no means well-versed in the workings of docker and linux. I can’t even name the particular type of HA install I am running (the recommended imager install on a RPi).

@AngelofromTurin Here is my fork: https://gitlab.com/Fredo70/alexapy But I can’t really tell you how to test it. I have tested it in my dev environment for HA on another PC (not my productive system). I had to fight my way through the dev documentation. I have no idea how to do it. Note: the change is in the external pypi module “alexapy”, not in the integration. And I just noticed that there is something wrong with the versioning. On pypi the current version is 1.27.6. And this is required by the integration. But in the repository, the latest version was 1.27.4, so I created a version 1.27.5. I would probably have to do 1.27.7. But why is 1.27.6 not in the repo? I don’t understand that.

You just need to reload the alexa media player integration. You can create an automation with this action:


      - service: homeassistant.reload_config_entry # alexa integration reload
        target: {}
        data:
          entry_id: {{ config_entry_id('media_player.<some alexa device>') }}

here as well. Setting the volume level has no immediate effect on the entity in HA

Update: I changed the volume level of one of my speakers 30 Minutes ago. Up to now no change in the attribute. I feel, that this information is not updated anymore as well as the last_called attribute. 😦

Try restarting your HA. It works on mine after restart.

here as well. Setting the volume level has no immediate effect on the entity in HA

Update: I changed the volume level of one of my speakers 30 Minutes ago. Up to now no change in the attribute. I feel, that this information is not updated anymore as well as the last_called attribute. 😦

IOBroker auf einem Py oder unter Proxmox installieren, danach Alexa2 und Mqtt Client installieren. Die letzte Alexa einfach per MQTT senden lassen und unter Homeassistant per Mqtt Sensor abfragen. Funktioniert fast in Echtzeit. Das Alexa2 Modul muss von Github installiert werde.

same for me. tried several devices without any luck. neither changing the volume via voice, device buttons nor alexa app works 😦

Try reloading the integration and recheck.

@sarigaus go it @Morpheus2018 open the routine, add a new action, set volume to 0.1, add another action set a 5sec wait, add another action set volume to 0.3 It does work well

I may have a quite good work around for the meantime. Here it solves 80% of the issues and works for all guys, who have motion sensors in at least most of all rooms. Its not 100% accurate even when two or more persons moving around in the house but it can increase the situation

I had an automation whitch wrotes the location of the latest movement into a variable:

- service: variable.update_sensor
  data:
	value: "{{now().strftime('%H:%M:%S - %d-%m-%Y')}}"
	attributes:
	  location: "{{trigger.to_state.attributes.friendly_name}}"
  target:
	entity_id: sensor.last_general_motion_detected

I created an additional sensor around the location attribute in order to display that in a button_card

Now I already wrote a custom template which translates a given movement into an alexa media_player

{% macro last_movement_alexa_target(palexa) %}
{% set map = {
   "Wohnzimmer": "media_player.wohnzimmer",
   "Gästezimmer": "media_player.gastezimmer",
   "Office": "media_player.office",
   "Küche": "media_player.kuche",
   "Fitnessraum": "media_player.fitnessraum",
   "Schlafzimmer": "media_player.schlafzimmer"}
   %}
   {{ map[palexa] if palexa in map else 'media_player.wohnzimmer' }} 
{% endmacro %}

and now I can build my own alexa last_called

command_announce_something:
  sequence:
    - service: notify.alexa_media
	  data:
		target: >
		  {%set loc = states('sensor.current_motion_location') %}
		  {% from 'global_functions.jinja' import last_movement_alexa_target %}
		  {{ last_movement_alexa_target(loc) }}
		data:
		  type: tts
		message: "bla bla"

with a little more time it can shurely be defined a little more sophisticated, but on the other hand we hope that its only for a short time

Good idea but not a viable solution for home with multiple occupants where they roam freely from room to room. maybe can put a light sensor on the Echo LED ring as workaround?

Thats great… (sarcasm) It would be really great, if someone from HA would tell us something. I posted in the forum, but apparently I am the only one who uses it, since there is no response.

You are not the only one using it as there are 1,200 “*” but it has nothing to do with anyone at Home Assistant per se since Alexa Media Player is a custom component by Alan Tse and contributed to by 35 other contributors over the years. Not to mention it’s using an unofficial API to accomplish its miraculous feats! I applaude them for what they’ve been able to achieve and if it’s to be be no more then so be it. You can blame Amazon but not HA. But, I’ve not given up hope yet!

I’ve edited my scripts/automations and fortunately, only good_night/good_morning were my biggest calls, which 99.99% of the time I make from bedroom so I have hard coded them to bedroom_echo_right (on my night stand). Car arrival script asking to close garage door was already hard coded to garage echo. The only other script/scene was “light” which I developed to turn on the lights in the area I’m in but will have to use Alexa, turn on the light and ensure Alexa’s areas are properly set up. I’m sure for other users/implementations, it won’t be so easy!

Seems to be an intentional change on Amazon’s part. This went out to ifttt users today.

We’re writing to let you know that on October 31st, 2023 the Amazon Alexa service will no longer be available on IFTTT.

Amazon has made the decision to no longer support their integration on IFTTT. Like you, many of us here on the IFTTT team use the Alexa IFTTT integration daily for things ranging from controlling lights and music in our homes to integrating with task management apps to boost productivity. We were disappointed to hear about this change, we understand it is always difficult to see an existing service removed from IFTTT.

We encourage you to update your Alexa Applets to use alternative triggers prior to October 31st. You can find more information on how to update your Applets, as well as suggested alternative triggers [here].

Starting November 1st, Applets that use the Say a specific phrase trigger will be migrated to instead use the IFTTT Button Widget. Applets that use any Amazon Alexa trigger other than Say a specific phrase or any Amazon Alexa query will be archived on October 31st if they are not updated to use a different trigger and/or query prior to that date.

IFTTT Community team

Thats great… (sarcasm) It would be really great, if someone from HA would tell us something. I posted in the forum, but apparently I am the only one who uses it, since there is no response. LOL

Cause is due to activities API being down. We’ll need a new API endpoint as found by #2090 (comment)

https://www.amazon.com/alexa-privacy/apd/rvh may be a workaround url but will require webscraping. Preference is still an actual API as webscraping is incredibly brittle.

If you check the network xhr requests made from that URL, there is a direct api request URL (different from the above) that returns JSON with most recent used alexa device as the first entry in an array of objects.

It seems to be:

https://[amazon domain for your region]/alexa-privacy/apd/rvh/customer-history-records-v2?startTime=1698274800000&endTime=1698308483234&disableGlobalNav=false

Of course, it will only work if passed the correct headers, cookies, tokens etc. so it might be hard to access it from outside of a logged in Amazon account.

One other thing to note, I tested the latency between issuing a command to an alexa device and this api returning the latest data, there is currently about a 10 second delay/cache mechanism… so this probably makes it almost worthless/pointless to be used for automations that require an immediate response.

Cause is due to activities API being down. We’ll need a new API endpoint as found by #2090 (comment)

https://www.amazon.com/alexa-privacy/apd/rvh may be a workaround url but will require webscraping. Preference is still an actual API as webscraping is incredibly brittle.

This won’t work for me as the activity history for my account seems to shows nothing.