Scrape Alexa Alarm from Website

Hi,

any idea how to best scrape the set Alarm from the Alexa?
As far as i know this is not available as an API.

However, you can see your Alarms on your Alexa Website:
AlexaAlarm
Is it possible to scrape these values? I would like to use it to put some additional automations to my morning routine :slight_smile:

1 Like

Not sure how to scrape them but you can do an http post via IFTTT when your Alexa alarm goes off. I use this to flash lights wherever I am in the house when my timer goes off. Just use ifttt to turn on a boolean and use that as an automation trigger?

I would like it to trigger my Hue Wake up Lights. So i need to know when the Alarm goes off… So the lights can turn on earlier

you can use IFTTT:

IFTTT as Trigger
Trigger if Alarm gets off

That only works when the Alarm gets off. I need to know about 15min earlier

I am trying to achieve the same, did you by any chance already found a way how to achieve this? I was thinking about using an AWS lambda to scrape the alarm time every 15 minutes, but it will get more complicated because the alexa url with the alarm times is protected by a login.

Another route I thought of is to create a custom alexa command that will set an alarm. Whenever I would say something like ‘Alexa, let Home Assistant wake me up at xx:yy’ ; it would send the desired alarm time to Home-Assistant and then Home-Assistant will let an alarm go of with a custom automation.

I haven’t got the time yet to try either of those solutions, but maybe the principles above can help you out in the mean time.

Didnt follow up on it… but now im interested again… either via Alexa or Google Assistant. Amazing that all these assistants are so “closed” for their main features & Smart Home… Did you get any further in you endeavour?

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 :relaxed: 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 :slight_smile:

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)

Hi, i was able to find a part solution to scrape the alexa alarms directly.
Partly, because it only works with 1 alarm currently and not multiple, but should be an easy fix.

Just posted how i did that here: