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.
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:
- Add a parameter to your script of
source
. Modify the script so that it simply exits whensource
is blank. Alexa canât provide parameters to scripts so it will always exit when alexa calls it. - Update any places within HA (UI, automations, etc.) where you call that script to now call it with
source = 'HA'
- 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 withsource = 'Alexa'
- 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!
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?