alandtse / alexa_media_player

This is a custom component to allow control of Amazon Alexa devices in Home Assistant using the unofficial Alexa API.
Apache License 2.0
1.5k stars 287 forks source link

Question: How to trigger an automation when someone speaks to Alexa? #487

Closed birdie60 closed 4 years ago

birdie60 commented 4 years ago

Hello, I have several Echo devices in my home. I'd like to create a poor man's presence detection by triggering an automation when someone talks to any Alexa. Is that possible? How would I do that efficiently?

alandtse commented 4 years ago

That is currently not supported. You're just hoping to have presence detection if someone triggers the wake word right? Not if they're talking in the room right? Wake word is probably possible; other is not.

birdie60 commented 4 years ago

Yes, I'm aware that just by talking in a room there are no events created with Alexa. I meant detection when someone speaks the wake word. And I'd like to trigger some automation when that happens, on any or on some specified Echos. If that is technically possible, I'd like to ask you to consider a feature request for this. Maybe a binary sensor "interaction_detected" for every Echo?

alandtse commented 4 years ago

The last called attribute could be used in a template for this I think. Essentially if the last called turns true in the last few seconds then there's an interaction.

birdie60 commented 4 years ago

I investigated last_called as a possible trigger. Thanks for the hint. Alas, it's only half of the solution. Yes, a change of last_called attribute to "true" indicates an interaction and could be used as a trigger for presence detection. But there is no change of last_called when a user interacts with the same echo sometimes later again. How would I detect that?

alandtse commented 4 years ago

That's true, it looks like we'll have to do a specific event.

alandtse commented 4 years ago

I just realized there is already an HA event triggered that may work.

Look for the alexa_media event type. The data will include the serial number.

{
    "event_type": "alexa_media_email@g*******m",
    "data": {
        "last_called_change": {
            "serialNumber": "G090XXXXX9FJ",
            "timestamp": 1577992084523
        }
    },
    "origin": "LOCAL",
    "time_fired": "2020-01-02T19:08:04.821440+00:00",
    "context": {
        "id": "e79baf39f940418b9e0e6d69b7ce551f",
        "parent_id": null,
        "user_id": null
    }
}
birdie60 commented 4 years ago

Brilliant! I guess that will do the trick. I'll have a look into it asap.

birdie60 commented 4 years ago

Just to confirm: With above alexa_media event type it is possible to implement a presence detecting automation that is triggered when someone says the wake word to alexa. Nice! Thanks a lot for your ongoing work on this superbe HA component! Very appreciated.

alandtse commented 4 years ago

Awesome! Glad you got it to work. To the extent you're able to share your discovery/sensors in the Wiki, I think others may appreciate it.