Last Alexa Sensor using Keaton Taylor Alexa Media Player custom component

I meant 1.1.0. Sry should confuse someone. I changed that.

Thanks for the insights @alandtse. I was under no impression that this was a trivial endeavor so thank you for all your contributions. This functionality is awesome. I can now say ‘Alexa, Goodnight.’ and she and HA do a complete security sweep of the doors and windows and determines if everyone is in the house. And if all is in order, sets the alarm system. If not, she tells me what is not secured.
Brilliant!
Thank you. I’ll be updating to 1.1.0 soon and hope I don’t break anything.
Cheers,
Bart

Ok. Rookie here again. (This starting to feel embarrassing :blush:)

I need to update last_alexa immediately. How do I put that

“alexa_media.update_last_called”

into an automation?

I updated to 1.1.0 and broke everything then fixed it by deleting the old media_player entities and renaming the new ones to the old names. But now I’m scratching my head on updating the last_alexa as soon as it enters an automation or script. I was hoping to find an example on the wiki page

I create the sensor like this:

- platform: template
  sensors:
    last_alexa:
      entity_id:
        - media_player.<all my echo devices>
        - media_player.<all my echo devices, etc.>
      value_template: >
        {{ states.media_player | selectattr('attributes.last_called','eq',True) | map(attribute='entity_id') | first }}

in my script I have:

- data_template:
      entity_id: '{{ states.sensor.last_alexa.state }}'
      message: Sorry, a door or window is still open. Please check and try again.
    service: media_player.alexa_tts

But this will send speech to a previously used Echo device, not necessarily the one I spoke to. How does a script update it before sending a message back?

First make sure you have 1,1,0 installed OK.

Check Services in the developers tools. Find the service “alexa_media.update_last_called” It should be the first service in the list. You can call this with no service data . If this works it should update the sensor immediately. Check by asking different dots questions.

I don’t use yaml automatons, but try this

Yes. The call works as expected so I’m thinking it is the 1.1.0 version. So far, I’m finding automations that execute scripts sequentially erratic but haven’t pinned down the cause. I’m in the middle of writing an automation that calls 3 scripts sequentially (I think). Each step along the way has a spoken progress sentence. On my first try, the automation did not speak and only the last script had something to say.

Automation:

- id: test3
  alias: test sequence of scripts
  trigger:
  - entity_id: light.security_check
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - service: alexa_media.update_last_called
  - data_template:
      entity_id: '{{ states.sensor.last_alexa.state }}'
      message: Please wait while I run a teast a.
    service: media_player.alexa_tts
  - data:
      entity_id: script.test_a
    service: script.turn_on
  - data_template:
      entity_id: '{{ states.sensor.last_alexa.state }}'
      message: Npw running test b.
    service: media_player.alexa_tts
  - data:
      entity_id: script.test_b
    service: script.turn_on
  - data_template:
      entity_id: '{{ states.sensor.last_alexa.state }}'
      message: Now running test C.
    service: media_player.alexa_tts
  - data:
      entity_id: script.test_c
    service: script.turn_on

and the scripts 1, 2, and 3 look like this:

test_a:
  alias: test_1
  sequence:
  - data_template:
      entity_id: '{{ states.sensor.last_alexa.state }}'
      message: this is test A.
    service: media_player.alexa_tts

test_b:
  alias: test_2
  sequence:
  - data_template:
      entity_id: '{{ states.sensor.last_alexa.state }}'
      message: this is test B.
    service: media_player.alexa_tts

test_c:
  alias: test_3
  sequence: 
  - data_template:
      entity_id: '{{ states.sensor.last_alexa.state }}'
      message: this is test C.
    service: media_player.alexa_tts

My next step is to put a delay (or wait?) in the automation between script executions and I’ll post back here when I get this sequence to run correctly and sequentially.

EDIT: Ok, the automation seems to run sequentially after I put:

- delay:
    seconds: 5

After every TTS call. I’m sure the amount of seconds will depend on how long the message is to keep it moving along. Some I dropped to 2 seconds for sentences such as ‘This is test A’

Hope it helps others having the same difficulty.

I use the last alexa to ensure I speak back to the correct echo triggered by the alexa routine .

To hide the delay your experiencing I construct the alexa routine like this

  • Trigger the alexa app with the wake words

  • trigger the dummy bulb ( this will trigger HA with the response)

  • get alexa app to say “please wait while I check” (anything phase will do)

This ensures the last alexa sensor triggers correctly and the correct echo responds. I suspect the delay is caused by the cloud updating

I think I’m doing all that @lonebaggie. Here is the beginning of the automation:

- id: lockup
  alias: Lock Up
  trigger:
  - entity_id: light.security_check
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - service: alexa_media.update_last_called
  - data_template:
      entity_id: '{{ states.sensor.last_alexa.state }}'
      message: Please wait while I run a security check. Checking garaj doors now.
    service: media_player.alexa_tts
  - delay:
      seconds: 4
  - data:
      entity_id: script.security_1
    service: script.turn_on

I have 4 scripts doing certain checks in sequence by this automation. They only speak if something is not right (like a door is open) if they run, they turn off the light.security_check so the next script won’t run.

But that first “alexa_media.update_last_called” doesn’t bring the TTS to the echo that I asked to run lockup. When I put a delay right after the call it appears to work fine.

The automation and scripts run too fast and cut off the TTS responses either partially, or fully. So I need the delays after each stage to allow Alexa to finish speaking before the next part runs. This should have been so easy, but it’s being unbelievably problematic. If I knew python scripting, I just might have done this in appdaemon.

Move The delay to the alexa app. I assume the light.security_check is the dummy bulb triggered by the alexa app.

So if the alexa routine is triggered by say “alexa run security check”. Then in the alexa app

  • turn on the dummy bulb (this will trigger your automation)

  • in the alexa app get alexa to say “Please wait while I run a security check. Checking garaj doors now”

This should give you enough of a delay

That sounds like it may work, but when the automation kicks off, it will fire the alexa_media.update_last_called service call pretty quick and not wait for Alexa to finish her intro speech. If the first script finds an issue, the speech from that script will cut off Alexa’s intro unless I delay. So either way I would have to put a delay in before running script 1.

My automation worked flawlessly last night while all the windows and doors were closed. Next I’ll be testing various states that the 3 scripts were designed to detect and either do something about it or simply tell me to go close the door and stop processing.

You’ve been a big help @lonebaggie. Thank you.

Ah, finally I understand. I had similar issues. You have to gather all the information, before you issue the TTS to alexa. The “,” is the only tool you have to make the speech sound more natural with a pause . If you issue multiple TTS requests they will cut each other off

Adding delays to script may have issues as HA may not be able to react to anything else while its waiting

Not sure if HA automations allow you to store information or pass parameters between scripts. This is why I wrote Speech Parser script , but basically you need to gather all the information , then speak

FYI…another data point:
After playing around with this I found I needed to add a delay of about 1 second between the service call to update the “last alexa” and the service call to play tts.

  - delay: 00:00:01
  - service: media_player.alexa_tts 
    data_template:
      entity_id: '{{ states.sensor.last_alexa.state }}'
      message: >

If I did not have this delay, the update was either too slow (sending tts to the wrong device), or would end up with an exception in the alexa custom component itself (ex. a JSON Decode “expecting value” exception).

Thanks @wmaker. I’ve noticed the occasional “I’m sorry, but something went wrong” speech from Alexa when I move to a different echo device. It’s not consistent, but I’ll try adding the delay to see if that stops it.

I’ve just got last_alexa working today after following this thread, but I’ve noticed a strange issue I can’t seem to figure out.

I’m using this sensor in an automation to ask Alexa what my pool temperature is, and it works flawlessly as long as I ask my basement echo dot, or my bedroom echo spot, but for some reason if I ask the echo dot in my spare bedroom, I get the wrong temperature.

My sensor is set up correctly, as when I update the sensor it gets the right device, and it replies to me on whichever device I’m asking, but in the bedroom and basement I get “The current temperature is sixty-four point four degrees”, while in the spare bedroom she responds with “The current temperature is 32 point zero degrees”

The TTS message is the same, regardless of which device is playing it, so I’m not even sure how one device could possibly get a different response than the rest:

message: The current temperature is {{states('sensor.converted_pool_temp')|replace('.', ' point ') }} degrees

Hi all, after a long time I use alexa media component, updated to the last version, I created sensor last alexa, including 2 entities of my 2 echo devices, but the sensor shows always only 1 of the media players even if I use them calling tts, also waiting for the scan interval time (I set 30 seconds).
I don’t know why the sensor is not refreshing to the last alexa device used

Thanks for this thread!

Now I’m able to receive sensors information very easily.

Is it possible to have it work also from my smartphone or tablet app?

I’ve done ./alexa_remote_control.sh -a and I see

Vittorio's Alexa Apps
This device

in addition to my Echos and TV Fire Sticks.

I’ve added both to my configuration.yaml, but only the first one is added.

In any case, does not work on my phone (for example) .

Is there a way to let it work this way?

Thanks

Using the code edited in the top post, I have a sensor.last_alexa that when I check the states is appropriately updating showing:

media_player.echo_show

(same as when I type {{ states.sensor.last_alexa.state }} into template editor)

When I try to reference it in a media card though I get

a.entity.split is not a function
artwork: full-cover
entity:
‘[object Object]’: null
type: ‘custom:mini-media-player’

code is:

  - artwork: full-cover
    entity: {{ states.sensor.last_alexa.state }}
    type: 'custom:mini-media-player'

What am I doing wrong here? Tried with and without 's and "s

Last Alexa Called has stopped working for me in v2.5.1. Only 2 out of 5 Alexa’s I have triggered it, reverted back to 2.5.0 and works fine.

Now fixed in 2.5.2 :slight_smile:

I don’t know how long this will work, but this seems to work for now.

  - service: media_player.play_media
    data:
      entity_id: media_player.echo_show_8
      media_content_id: "play Dog Bark by halloween haunters"
      media_content_type: "AMAZON_MUSIC"

Hello. I am using this template.

sensor:
  - platform: template
    sensors:
      last_alexa:
        entity_id:
        - media_player.1
        - media_player.2
        value_template: >
          {{ states.media_player | selectattr('attributes.last_called','eq',True) | map(attribute='entity_id') | first }}

Entity_id has been deprecated since 0.115 update. How to rewrite the list within the template and not create a group list?