Scripting My Morning Wake-Up Alarm and Lights with Android and Kasa

TLDR: This post covers how (and why) I setup an Android automation flow that automatically turns on lights in my room whenever my morning alarm goes off, regardless of which alarm app I use!

Intro

I have something to admit. I’m one of those people that has struggled with my sleep schedule my entire life. It constantly shifts, and when I force myself to wake up at a certain time with an alarm clock, both my mind and body fight against the process. Don’t get me wrong (especially potential employers 😀) – I can always force myself to get up when I need to, but it has not been easy.

As such, it should be no surprise that I also have tried many different types of alarms, and have settled on “challenge” alarm apps. These are alarm apps that force you to complete certain tasks before the alarm will shut off, such as scanning a barcode, solving math problems, or shaking your phone.

Can I Get Some Light in Here?

One of the challenges that my alarm clock app (Sleep as Android) has is called “Let there be light”; in order to complete the task, you must expose your phone to a high level of light for a minimum amount of time (adjustable).

I sleep with blackout curtains because I’m light-sensitive, and this is great for falling asleep, but makes it too easy to sleep-in late in the day. Thus, this challenge has been great for forcing me to bring some light into my pitch-black environment in the mornings.

…but, I was wondering; can I take this a step further? Can I have my lights automatically turn on whenever my alarm goes off?

Automating Power Outlets with Android

I already had a TP-Link Kasa Wi-Fi Plug that I wasn’t doing much with, so I set out upon figuring out how to automate it based on my Android alarm settings.

The Kasa software does support built-in scheduling, but you have to manually enter times for it to turn off and on; I wanted an automatic solution that would hook into the system-level Android alarm and would work regardless of what alarm app I was using, and no matter how many times I changed my alarm time.

Google Apps Script Wrapper

My first step was to create a “wrapper” script / API around the unofficial Kasa API. I wanted to do this, because using the Kasa API requires a multi-step token exchange, and I wanted to be able to control my switch with a single network request (which I knew would make the automation piece easier down the line).

I used Google Apps Script for this, since it is a free and very easy to use JavaScript serverless environment; similar to AWS Lambda, but without the complicated pricing or web console. Its ease of use makes it great for one-off hobby projects like this one.

I won’t go too much into the details of the script here, but the basics are that it:

  • Caches my credentials and token to avoid extra API requests
  • Refreshes the auth token when it expires
  • Simplifies using the Kasa API by wrapping and formatting requests for it
  • Published as a “web app”, so I have an endpoint I can POST to

I’ve posted my script here, as a Github gist.

Integrating with Android via Automagic

Since my Google Apps Script gives me a single endpoint I can turn my lights on with a single request, all I need to automate my flow in Android is an app that can A) listen for the alarm trigger, and B) make a POST request with a payload. Luckily, I already have a go-to automation app that meets both those requirements. Meet Automagic.

The Automagic flow is super simple:

  1. Trigger = Next Alarm
    • Fires when alarm goes off
  2. Make sure I’m at home (connected to home WiFi SSID)
    • If false, stop flow
  3. Fire POST request to my Google Apps Script endpoint
    • JSON payload says to turn my bedroom light on
  4. Capture and log response from script, just in case something failed

And here is what that flow look like in the Automagic UI:

Automagic flow for turning on light based on alarm clock going off trigger

The actual POST request that triggers the light turning on is very simple as well (thanks to the Google Apps Script wrapper). Here is an example:

{
     "pass": "REDACTED",
     "deviceId": "REDACTED",
     "action": "on"
}

Android Internals – Intent Broadcasts

Because I was curious about how Automagic might have built that trigger behind-the-scenes, and how you might approach this with other Android apps, I did some digging into Android Intents. The TLDR of an Android Intent, is that it is a request and/or message that is broadcasted from one app to another (or the general system). The Android OS itself broadcasts many of these messages, which makes it easy to automate things like this.

For example, if I wanted to have my light come on 10 minutes before my alarm goes off, I could use something like this:

  1. Listen for android.app.action.NEXT_ALARM_CLOCK_CHANGED
  2. Use getNextAlarmClock()) to determine the time of the next alarm, then subtract 10 minutes
  3. Use the calculated time to set a trigger for the POST action

You can read more about Intents here: Docs – Intents and Intent Filters

No-Code Options

I thought it important to note that this kind of automation is very close to being a prime candidate for a “no-code” approach, where WYSIWYG tools are used to build the automation as opposed to coding.

Most of what I put together could probably be replicated with a combination of some of the following: – IFTTT (one of the few official integrations for Kasa) – Zapier – … etc.

Leave a Reply

Your email address will not be published. Required fields are marked *