Recognizing Alexa calling device

I have integrated HA with Alexa and I can now see my HA scripts in Alexa scenes so that I can call a HA script from Alexa.
Is there a way to know at HA side which is the device calling that script (i.e. Echo_1_living_room or Echo_2_kitchen)?

How do you have HA integrated with Alexa? There’s many different ways.

I integrated HA with Alexa through the procedure using haaska and a custom skill with AWS.
I see the HA scripts in Alexa as scenes.
Does this make sense for you?

If you went that route, make an alexa user and attach it to a person. Then when a script is called or executed, its state object should have the context of ‘alexa’ as a user_id.

EDIT: I should clarify that I’ve never tried this but I think it will work. I did make an alexa user but I’ve never seen the need to use it that way.

EDIT2: It works that way for lights. I just turned on my basement lights through the alexa app (using the devices in alexa app) and the logbook had her user attached to it. This should work for scripts too.

What do you mean by:

?
How can I do it?

Did you make a user for yourself? Do that, but for Alexa.

In HA I created a user alexa and a person Alexa, but when I start a script from Alexa and I check
{{ states.script..context.user_id }}, it is always equal to my user_id, not to Alexa’s…

Yes, because when you logged into Home Assistant on your Alexa app, you used your user name. You have to log in from alexa as Alexa’s user name.

This may require you to disable the skill and re-enable it.

If you never logged in, then it can’t be done. There’s 2 versions of Alexa Smart Home Skill. Haaska, and non-Haaska. If I recall correctly, using Haaska doesn’t have a log in. If you used the other python script that requires a log in, this will work.

Alternatively you could try the custom component Alexa Media Player from HACS. This gives you a sensor last_alexa, which is the last active device.

1 Like

That doesn’t tell him who turned on and off a light or who ran a script.

To clarify, he wants to know If alexa ran a script or if he ran the script.

I am not aware of a good way to do this but I can think of a really hacky way lol. Although I don’t actually have alexa so I can’t really test this so YMMV.

When I use google assistant to change a light, start a script, etc. then two events come in. One is obviously the call_service event that does whatever I told it to do but the first is specifically from google, it has event type google_assistant_command and contains the info of what I told it to do. Looking at the source code of alexa I think it has the same thing, looks like it listens for an event of type alexa_smart_home and then makes service calls from there.

So a potential hacky solution here could be this:

  1. Add a parameter to your script of source. Modify the script so that it simply exits when source is blank. Alexa can’t provide parameters to scripts so it will always exit when alexa calls it.
  2. Update any places within HA (UI, automations, etc.) where you call that script to now call it with source = 'HA'
  3. Create an automation that listens for the event alexa_smart_home and checks its data to see that it is calling your script. When it hears this have it call your script with source = 'Alexa'
  4. Modify your script however you wanted to based on the source

Note that I can’t write the automation for #3 for you because I don’t have an Alexa so I don’t know what this event looks like. You’ll have to do some exploring. I’d suggest logging the entire trigger object to see what it looks like and see if you can find your entity ID in there, I think something like this will work for that:

trigger:
  platform: event
  event_type: alexa_smart_home
condition: []
action:
  service: system_log.write
  data:
    message: "{{ trigger | to_json }}"

Then you can tell alexa to do something and look in your log to see what data the trigger has and adjust your automation accordingly.

EDIT: Actually come to think of it, if you just add a parameter of type source and update everywhere in HA to provide a value for that parameter then you’ll know its alexa if source is blank. That’s way way easier.

This is working, thank you Stiltjack!

1 Like

Thank you for the suggestion; i tried your automation (after cutting off the “| to_json” part because it gives error). The result in the log is:

2021-01-12 09:21:20 INFO (MainThread) [homeassistant.components.system_log.external] 
{'platform': 'event', 'event': <Event alexa_smart_home[L]: request=namespace=
Alexa.SceneController, name=Activate, entity_id=script.<name-of-my-script>,
response=namespace=Alexa.SceneController, name=ActivationStarted>, 
'description': "event 'alexa_smart_home'"}

Now, this is very interesting, but my wish is to understand which of my Alexas did the call, and here I don’t have the name of the device… Any idea? It would also be interesting to identify who is the user’s identity (as Alexa can recognize my voice vs my wife’s voice, …). Any idea on this?

Not sure if that’s possible unfortunately. That event object you’re looking at there is everything HA gets from Alexa when an Alexa command is called, if Alexa doesn’t provide it then nothing can be done. Since the object appears to not be JSON serializable I’m not sure exactly what is in that “Event” object so that’s the only thing worth more investigation. If you can get some way to see the complete object you can see whether or not that data is in there.

Since I don’t have Alexa I can’t really investigate this at the moment. But for comparison, here’s what the google_assistant_command event looks like when I talk to my google home:

{
    "event_type": "google_assistant_command",
    "entity_id": "light.guest_room",
    "event": {
        "request_id": "14240774154451216342",
        "entity_id": "light.guest_room",
        "execution": {
            "command": "action.devices.commands.BrightnessAbsolute",
            "params": {
                "brightness": 20
            }
        },
        "source": "cloud"
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-12T14:02:16.586838+00:00",
    "context": {
        "id": "6c89d2fbb17c4f21c76728bb1fb27fbc",
        "parent_id": null,
        "user_id": "40a00d63ba7b4ec5804d8682198e1130"
    }
}

Looks like Google doesn’t provide info on which device I talked to. That doesn’t necessarily mean alexa doesn’t though as they are different integrations that just serve a similar purpose.

Note that I was able to get the JSON version of that Event object in Node RED. I actually don’t know how to do it from an HA automation if to_json doesn’t work. Can probably dump out instances of python classes in python script, maybe there’s one on the forum for debugging somewhere?