I took a different approach in the end. I created a custom Alexa skill and now I am saying things like:
Alexa, ask Homey (my skill name) to wake me up at 6:30
This triggers an AWS lambda that getâs the time from that, and sends it to the endpoint of my home-assistantâs api. Then, my home-assistant automations are taking over from there.
This is the interface I created in my dashboard. Itâs not all functional yet, and the code is a bit too messy to share for now Maybe the screenshot below can serve as inspiration.
I donât really recall all the steps I took, but Iâll add my lambda code below, and together with an Alexa Skill tutorial somewhere on the net, I think you can manage to achieve the same
import random
import requests
import json
import datetime
# TODO: build CancelAlarm intent
# TODO: build when is my alarm intent
# TODO: expand random sleep wish to audio files from friends
def lambda_handler(event, context):
alarm_time = event['request']['intent']['slots']['time']['value']
alarm_time = guarantee_four_digits(alarm_time)
# pass time along to Home Assistant
enable_hass_alarm(alarm_time)
send_automation_start_time(alarm_time)
return {
'version': '1.0',
'sessionAttributes': {},
'response': {
'outputSpeech': {
'type': 'SSML',
'ssml': "<speak>I will wake you up at <say-as interpret-as='time'>{}</say-as>. {} </speak>".format(alarm_time, random_sleep_wish())
},
'card': {
'type': 'Simple',
'title': "New alarm set",
'content': 'alarm time = {}'.format(alarm_time)
},
'shouldEndSession': 'True'
}
}
def guarantee_four_digits(alarm_time):
if len(alarm_time) == 2:
return '{}:00'.format(alarm_time)
else:
return alarm_time
def random_sleep_wish():
wishes = [
'Sleep well',
'Sweet dreams',
'Have a good night',
'Rest well',
'Enjoy your night',
'Enjoy your dreams'
]
selected = random.choice(wishes)
return selected
def enable_hass_alarm(alarm_time):
url = 'https://my-home-assistant.url/api/states/input_select.alarm_time'
headers = {'x-ha-access': 'my-api-password',
'content-type': 'application/json'}
data = {
'state': '{}'.format(alarm_time),
'attributes': {
"friendly_name": "Next alarm",
"options": [
"{}".format(alarm_time),
"Not set"
],
"icon": "mdi:alarm"
}
}
response = requests.post(url, headers=headers, data=json.dumps(data))
print('sent new alarm time to hass --> {}'.format(alarm_time))
def send_automation_start_time(alarm_time):
start_time = datetime.datetime.strptime(alarm_time, '%H:%M') - datetime.timedelta(minutes = 15)
start_time = start_time.time().strftime("%H:%M")
url = 'https://my-home-assistant.url/api/states/input_select.alarm_start_automation_time'
headers = {'x-ha-access': 'my-api-password',
'content-type': 'application/json'}
data = {
'state': '{}'.format(start_time),
'attributes': {
"friendly_name": "Automations start at this time",
"options": [
"{}".format(start_time)
]}
}
response = requests.post(url, headers=headers, data=json.dumps(data))
print('sent new automation start time to hass --> {}'.format(start_time))
Update:
Reading the code above again, I realise itâs importing the requests
module. The downside of importing third-party modules like this one, is that you cannot do this when editing the lambda in the online editor - you have to deploy it instead. This complicates things a bit, especially if youâre not familiair with AWS yet. Alternatively, you can send the requests with the good old urllib
module from python. The syntax isnât super straightforward, so I have put an example call below:
req = urllib.request.Request(your_api_endpoint, data=bytes(json.dumps(data), encoding='utf-8'))
req.add_header('Content-Type', 'application/json')
response = urllib.request.urlopen(req)