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

I’ve been using the Alexa Media Player integration, and the Last Alexa template sensor, for a long time.

All of a sudden Last Alexa is now showing as unavailable, and I’m not sure why. For reference the template sensor is configured in my configuration.yaml as:

template:
  - sensor:
    - name: Last Alexa
      state: |-
        {{ expand(integration_entities('alexa_media') | select('search', 'media_player'))
        | selectattr('attributes.last_called', 'eq', True) | map(attribute='entity_id') | first }}
      availability: |-
        {{ expand(integration_entities('alexa_media') | select('search', 'media_player'))
        | selectattr('attributes.last_called','eq',True) | first is defined }}

And if I go to Developer Tools > Template and look at what these evaluate to, there doesn’t seem to be any issues.

Input:

{{ expand(integration_entities('alexa_media') | select('search', 'media_player'))
        | selectattr('attributes.last_called', 'eq', True) | map(attribute='entity_id') | first }}

{{ expand(integration_entities('alexa_media') | select('search', 'media_player'))
        | selectattr('attributes.last_called','eq',True) | first is defined }}

Output:

media_player.office_speaker

True

Calling alexa_media.update_last_called doesn’t update the value, and there doesn’t seem to be any errors in the logs.

Anyone have any idea why it would the template sensor would suddenly stop working?

Have you looked at the state in dev tools to see if media_player.office_speaker has an attribute last_called: true

EDIT: Just tried your template and it works fine for me

I found the same a while back and just replaced the notify action with this instead:

notify.alexa_media_last_called

It just notifies on the alexa that was used for the instruction

Have you looked at the state in dev tools to see if media_player.office_speaker has an attribute last_called: true

Yeah - it does.

last_called: true
last_called_timestamp: 1724864853539

I found the same a while back and just replaced the notify action with this instead:
notify.alexa_media_last_called
It just notifies on the alexa that was used for the instruction

Thanks! I’d still be interested if anybody knows why the custom template doesn’t work, but I will try this.

I’m getting a lots of these errors with the template, maybe AMP is dropping connection ?

Template variable warning: No first item, sequence was empty. when rendering ‘{{ expand(integration_entities(‘alexa_media’) | select(‘search’, ‘media_player’)) | selectattr(‘attributes.last_called’, ‘eq’, True) | map(attribute=‘entity_id’) | first }}’

I’m not convinced it is a AMP issue… Surely if the template is evaluating correctly in Developer Tools > Template it should also populate the entity correctly?

That sounds like a HA issue to me.

Check you are on the latest versions of both HA and AMP - I had the same issue after upgrading to HA 2024.8, as did others, and it was fixed by applying the latest update for AMP.

@richcyjafro - it certainly looks like I am. I’m on HA version 2024.8.3 and AMP version 4.12.11 which looks like the latest release on GH.

If either the notify.alexa_media_last_called or Last called sensor has been working for you over the last 9 months or so, consider yourself lucky. As I understand it, Amazon changed which actions automatically update the last_called attribute as well as how often new values are posted.

If I use notify.alexa_media_last_called on it’s own, the device that responds seems almost random. Instead I use the following, which mostly works:

...
  - service: alexa_media.update_last_called
    data: {}
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - service: notify.alexa_media_last_called
    data:
....
1 Like

Do you have a link to the issue where this is being discussed?

This might be an issue with the availability template… If I create the template sensor again, but from the UI, it works fine with the below:

{{ expand(integration_entities('alexa_media') | select('search', 'media_player'))
| selectattr('attributes.last_called', 'eq', True) | map(attribute='entity_id') | first }}

Never-mind that’s gone unavailable too now haha. Not sure what is going on.

@Didgeridrew - notify.alexa_media_last_called works, but it’s not very flexible with some more complex automations I believe. Can’t use it with media_player.play_media for example?

The notify action will not replace a media_player action, but the alexa_media.update_last_called action looks like it should update the media player entities so that the correct one is marked as last_called.

alexa_media.update_last_called is what I’m using, but the sensor is Unknown (why I started this thread) :slight_smile:

Bizarre! Your template works fine in my Dev Tools > Template.
This is my “superchargedsensor.last_alexa template.
Give it a whirl as it’s basic structure is different than yours.

sensor:
  - name: Last Alexa
    unique_id: last_alexa
    icon: phu:amazon-logo
    state: >
      {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id') | first | default(none) }}
    availability: >
      {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.last_called_timestamp') | first is defined }}
    attributes:
      source_list: >
        {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.source_list') | first | default(none) }}
      is_volume_muted: >
        {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.is_volume_muted') | first | default(none) }}
      volume_level: >
        {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.volume_level') | first | default(none) }}
      media_content_type: >
        {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.media_content_type') | first | default(none) }}
      media_position_updated_at: >
        {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.media_position_updated_at') | first | default(none) }}
      source: >
        {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.source') | first | default(none) }}
      available: true
      last_called_timestamp: >
        {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.last_called_timestamp') | first | default(none) }}
      last_called_summary: >
        {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.last_called_summary') | first | default(none) }}
      connected_bluetooth: >
        {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.connected_bluetooth') | first | default(none) }}
      bluetooth_list: >
        {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.bluetooth_list') | first | default(none) }}
      actual_friendly_name: >
        {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.friendly_name') | first | default(none) }}
      supported_features: >
        {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.supported_features') | first | default(none) }}

Correct. The http2push command PUSH_ACTIVITY stopped being issued my Amazon in early June, which is what provided near instant updating of last_called.

I’ve added an async_fire event into media_player.py at the correct point to inform me when the last_called service has actually finished so my scripts/automations can wait for that event and if it occurs, set a variable which controls executing or skipping the TTS commands in the script. I will submit a feature PR when I’m comfortable with it.

I also created this automation to update last_called whenever my Amazon Scenes/HA Scripts are executed:

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:
  - parallel:
      - wait_for_trigger:
          - platform: state
            entity_id:
              - sensor.last_alexa
            from: unavailable
        continue_on_timeout: false
        timeout:
          hours: 0
          minutes: 0
          seconds: 20
          milliseconds: 0
      - action: alexa_media.update_last_called
        data: {}
  - event: alexa_last_called_updated
    event_data: {}
  - action: input_boolean.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.alexa_media_update_last_called_trigger
  - action: counter.increment
    metadata: {}
    data: {}
    target:
      entity_id:
        - counter.alexa_media_update_last_called_service_calls_today
mode: single
trace:
  stored_traces: 30

My script.good_night looks like this:
(other scripts still need editing)

Summary
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 }}"
  - parallel:
      - if:
          - condition: state
            entity_id: cover.bedroom_blind
            state: open
        then:
          - data:
              entity_id: cover.bedroom_blind
            action: cover.close_cover_tilt
      - continue_on_error: true
        data: {}
        target:
          entity_id:
            - input_boolean.sleep_mode
            - input_boolean.daniel_sleeping
        action: input_boolean.turn_on
      - continue_on_error: true
        data:
          value: 0
        target:
          entity_id: input_number.bathroom_delay_off
        action: input_number.set_value
      - continue_on_error: true
        data:
          option: Sleeping
        target:
          entity_id: input_select.sleep_status
        action: input_select.select_option
      - continue_on_error: true
        data: {}
        target:
          area_id:
            - patio
          entity_id:
            - light.pole_lamp_1
            - light.pole_lamp_2
            - light.table_lamp_1
            - light.table_lamp_2
        action: light.turn_off
      - continue_on_error: true
        target:
          entity_id:
            - media_player.bedroom_tv
        data: {}
        action: media_player.turn_off
      - repeat:
          count: 1
          sequence:
            - 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:
                - continue_on_error: true
                  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 {{ not wait.trigger }} renders value equal to true
                  condition: template
                  value_template: "{{ alexa_last_called_updated }}"
                - condition: numeric_state
                  entity_id: sensor.garbage
                  below: 2
                - condition: state
                  entity_id: input_boolean.garbage_is_out
                  state: "off"
                - condition: or
                  conditions:
                    - condition: and
                      conditions:
                        - condition: state
                          entity_id: sensor.garbage
                          state: "0"
                        - condition: time
                          before: "09:00:00"
                          after: "00:00:00"
                    - condition: and
                      conditions:
                        - condition: state
                          entity_id: sensor.garbage
                          state: "1"
                        - condition: time
                          after: "12:00:00"
                          before: "00:00:00"
              then:
                - continue_on_error: true
                  data:
                    text: Did you put the garbage out?
                    event_id: actionable_notification_garbage_pickup
                    alexa_device: "{{ states('sensor.last_alexa') }}"
                  action: script.activate_alexa_actionable_notification
            - if:
                - alias: Test if {{ not wait.trigger }} renders value equal to true
                  condition: template
                  value_template: "{{ alexa_last_called_updated }}"
                - condition: numeric_state
                  entity_id: sensor.recycling
                  below: 2
                - condition: state
                  entity_id: input_boolean.recycling_is_out
                  state: "off"
                - condition: or
                  conditions:
                    - condition: and
                      conditions:
                        - condition: state
                          entity_id: sensor.recycling
                          state: "0"
                        - condition: time
                          before: "09:00:00"
                          after: "00:00:00"
                    - condition: and
                      conditions:
                        - condition: state
                          entity_id: sensor.recycling
                          state: "1"
                        - condition: time
                          after: "12:00:00"
                          before: "00:00:00"
              then:
                - continue_on_error: true
                  data:
                    text: Did you put the recycling out?
                    event_id: actionable_notification_recycling_pickup
                    alexa_device: "{{ states('sensor.last_alexa') }}"
                  action: script.activate_alexa_actionable_notification
      - if:
          - condition: state
            entity_id: lock.patio_door_lock
            state: unlocked
        then:
          - target:
              entity_id: lock.patio_door_lock
            data: {}
            action: lock.lock
  - parallel:
      - if:
          - condition: not
            conditions:
              - condition: state
                entity_id: climate.living_room
                attribute: preset_mode
                state: eco
                alias: Preset of Furnace
            alias: If Preset of Furnace is NOT  'eco'
          - condition: numeric_state
            entity_id: climate.living_room
            attribute: temperature
            above: 17
            alias: Furnace temperature is above 17
        then:
          - continue_on_error: true
            data:
              temperature: 17
            target:
              entity_id: climate.living_room
            action: climate.set_temperature
          - if:
              - alias: Test if {{ alexa_last_called_updated }} is true
                condition: template
                value_template: "{{ alexa_last_called_updated }}"
            then:
              - continue_on_error: true
                data:
                  message: Heat is now set to 17
                action: script.notify_alexa_media
      - if:
          - condition: state
            entity_id: lock.front_door_lock
            state: unlocked
        then:
          - continue_on_error: true
            data: {}
            target:
              entity_id:
                - lock.front_door_lock
            action: lock.lock
          - if:
              - alias: Test if {{ alexa_last_called_updated }} is true
                condition: template
                value_template: "{{ alexa_last_called_updated }}"
            then:
              - continue_on_error: true
                data:
                  message: The front door was unlocked but I locked it for you.
                action: script.notify_alexa_media
      - if:
          - condition: state
            entity_id: cover.garage_door
            state: open
        then:
          - if:
              - condition: state
                entity_id: sensor.season
                state: summer
            then:
              - if:
                  - alias: Test if {{ alexa_last_called_updated }} is true
                    condition: template
                    value_template: "{{ alexa_last_called_updated }}"
                then:
                  - continue_on_error: true
                    data:
                      message: Hey. Did you know the garage door is open?
                      milliseconds: 0
                    action: script.notify_alexa_media
            else:
              - continue_on_error: true
                data:
                  entity_id: cover.garage_door
                action: cover.close_cover
              - if:
                  - alias: Test if {{ alexa_last_called_updated }} is true
                    condition: template
                    value_template: "{{ alexa_last_called_updated }}"
                then:
                  - continue_on_error: true
                    data:
                      message: I closed the garage door for you
                      milliseconds: 0
                    action: script.notify_alexa_media
  - 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
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - data:
      sequence:
        - action: light.turn_off
          continue_on_error: true
          data: {}
          target:
            entity_id:
              - light.nightstand_lamps
              - light.headboard_indirect
      expected_state:
        - "off"
    action: retry.actions
  - if:
      - condition: state
        entity_id: binary_sensor.bathroom_occupancy
        state: "on"
    then:
      - wait_for_trigger:
          - platform: state
            entity_id:
              - binary_sensor.bathroom_occupancy
            to: "off"
        timeout:
          hours: 0
          minutes: 1
          seconds: 30
          milliseconds: 0
  - data: {}
    target:
      entity_id: light.bathroom_light
    action: light.turn_off
mode: single
icon: hass:weather-night

I also had a “Woo Hoo!” moment earlier this evening after studying OpenHAB’s implementation of their version of Alexa Media Player. I think I danced around my office for like 10 minutes!!
I created a new user in HA to be used by my Amazon Alexa Smart Home Skill.
Then I disabled the skill in Alexa and re-enabled it with the new HA user account.
With that in hand, I can now update last_called in HA whenever that Amazon Alexa user account changes any HA entity state that is exposed to it via this automation:

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:
  - action: alexa_media.update_last_called
    data: {}
mode: single

@dbrunt - sorry I totally missed your response!

There was some weirdness, even with your version of the template entity, but backing up the configuration file, removing 90% of it, restarting, restoring it, then restarting again fixed the issue :person_shrugging:

1 Like