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:
To Reproduce
- Say “alexa”
- last_called used to change
- Now stays blank
Expected behavior
Screenshots Happens on all devices:
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)
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
.@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):
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, withdevice.deviceName
being the useful value for us) :me too
To install @Fredo70’s branch while we wait for merges, launch your HA Terminal and…
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:
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.
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.
But maybe there is hope. If you call https://alexa.amazon.de/api/activities Then you see the following:
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.
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
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
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?
Yes. Same scripts are working now untouched. Thanks much to all for this.
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:
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.
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
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?
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!
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
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.
This won’t work for me as the activity history for my account seems to shows nothing.