Alexa Media Player - "Last Alexa" showing as "unavailable" all of a sudden

FYI, there’s a new feature in 4.13.0+ that I added and it’s an event that fires when the alexa_media.update_last_called action has completed. So rather than a fixed delay in scripts/automations to give it time to complete, you can now wait for the event. I then set a boolean variable True if the wait succeeded before timeout or False if wait_remaining is 0. I then use that variable to conditionally have the last_called alexa say something or not…

alias: Good Night
sequence:
  - wait_for_trigger:
      - platform: event
        event_type: alexa_media_last_called_event
        event_data:
          summary: good night
    timeout:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
    continue_on_timeout: true
  - variables:
      alexa_last_called_updated: "{{ wait.remaining > 0 }}"

  - if:
      - alias: Test if {{ not wait.trigger }} renders value equal to true
        condition: template
        value_template: "{{ alexa_last_called_updated }}"
      - condition: state
        entity_id: input_boolean.coffee_prepared
        state: "off"
    then:
      - data:
          text: Did you prepare your coffee?
          event_id: actionable_notification_coffee_prepared
          alexa_device: "{{ states('sensor.last_alexa') }}"
        action: script.activate_alexa_actionable_notification

  - if:
      - alias: Test if {{ alexa_last_called_updated }} is true
        condition: template
        value_template: "{{ alexa_last_called_updated }}"
    then:
      - alias: "Media Player: Say Goodnight"
        continue_on_error: true
        data:
          media_content_id: Alexa.GoodNight.Play
          media_content_type: sequence
        target:
          entity_id: "{{ states('sensor.last_alexa') }}"
        action: media_player.play_media
2 Likes

Further to the above, I created this automation that fires when an Alexa Scene/HA Script has run which automatically updates last_called for me.

alias: Alexa - update_last_called_via_scripts
description: >-
  Calls the `alexa_media.update_last_called` action whenever Alexa Scenes are
  turned on (i.e. when HA scripts are run). The scripts should begin by waiting
  for the event `alexa_last_called_updated` to fire before proceeding. If the
  event fails to fire, they should avoid using `sensor.last_alexa`.
trigger:
  - platform: state
    entity_id:
      - script.bought_coffee_filters
      - script.business_trip_end
      - script.business_trip_start
      - script.feed_fish
      - script.good_morning
      - script.good_night
      - script.lights
      - script.make_coffee
      - script.may_is_coming
      - script.shower
      - script.sleep_mode
    to: "on"
condition: []
action:
  - action: alexa_media.update_last_called
    data: {}
mode: single
trace:
  stored_traces: 30

After doing the above, I had a brainwave and created a new HA user for the Amazon Alexa skill to authenticate with. By doing that, I now know when Alexa has interacted with HA by listening to state_changed events by that user_id. For each interaction though, there are several state_changed events in sequence but I only want to call alexa_media.update_last_called once:

alias: Alexa - Triggered by Smart Home Skill user account
description: >-
  Perform action `Alexa Media Player: Update Last Called Sensor` when entity
  states are changed by Alexa Smart Home Skill user account
trigger:
  - platform: event
    event_type: state_changed
    context:
      user_id: 0df463c1efc4490f900b09b143cae9c4
condition: []
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - action: alexa_media.update_last_called
    data: {}
  - delay:
      hours: 0
      minutes: 0
      seconds: 7
      milliseconds: 0
trace:
  stored_traces: 30
mode: single
max_exceeded: silent

Note: Do not use both of the above at the same time or you will have update_last_called firing too often and you’ll encounter Amazon’s infamous “Too many requests” error,

2 Likes

Thanks again for the suggestions.

I’ll look into those and see what’s what and try them out.

1 Like

I had the same problem, tried everything, and it got worse and worse
Until yesterday, it was enough to reload the authomatio to have the last alexa and last called properties updated

Today I can’t even make announce and I get this “warning”

  • alexaapi.get_customer_history_records((<alexapy.alexalogin.AlexaLogin object at 0x7f58758b6d50>,), {‘max_record_size’: 10}): An error occurred accessing AlexaAPI: An exception of type AlexapyLoginError occurred. Arguments: (‘Unauthorized’,)

Every single time, even if the integration can login

as far as I know I’m still getting announcements. I have a morning tts that happened at 8 am that worked.

I typically never use “last_alexa” except in one or two cases so it’s not a complete show stopper for me.

and I’ve begun the process of switching my tts functions to my sonos speakers for which the code is now waiting in the wings for the day AMP stops working (if ever and I certainly hope not)

For now I use node-red with the alexa applestrudel node and most of the automations work, the only ones that I still can’t get to work are those related to the Alexa actionable notification because I can’t use the alexa applestrudel node but the event node and when Alexa media player doesn’t work those automations are “blocked”… Is there anyone who is using applestrudel for actionable notifications?

1 Like