Sync WLED Sunrise lights to alarm using Tasker

Part of the Sunrise Lights project
Last Updated: November 26, 2024 at 8:53:57 PM

Introduction

The main reason why I wanted to set up WLED lights in my bedroom was to create a sunrise lamp. During the winter, when the mornings are dark, I find it incredibly difficult to motivate myself to get out of bed when I'm supposed to. And for good reason, my circadian rhythm is telling me that it isn't time to wake up yet! So in the absence of a real sun, I thought that perhaps a simulated one would work. After having set it up, I can tell you it works wonderfully. Not only am i able to wake up on time, but I actually feel like I got a full night's rest! One of my goals for this project was to have it be intelligent. WLED easily allows me to schedule the lights on a regular clock, however, I don't always wake up at the same time every day. The mornings when I'd probably need the lights the most are the ones where I have to wake up extra early to catch a train or a plane, or maybe get to a particularly early morning meeting. I also don't want to worry about waking up my wife when I'm not even there. Because of that I'd like my phone to automatically start the sunrise animation a bit before my alarm goes of every day.

Overview

This may seem like a challenging task, but with the right tools it is actually quite simple:

Tasker - Tasker is an app that will allow us to set up the automation portion of the lights. It can gather information about our phone (like when the next alarm is) and run certain actions on a schedule. NOTE: This is not a free app, however, it is also a one-time-purchase of just a couple of dollars. In a world of monthly subscriptions, this is basically free.

WLED JSON REST API - Apologies for the long string of acronyms, but this is how we'll remotely trigger the lights. By sending a simple post request with a small JSON body, we will be able to tell our controller to turn on and also set the appropriate color and animation.

Putting it All Together

There are probably a few ways to get this done, but this is the way that made the most sense to me. I tried to implement a solution that was easy to debug and also had as few moving parts as possible. I'm going to explain how to set this up manually yourself below, but all of my tasks are saved as xml here: https://github.com/jeffeth-donaldson/SunriseTasks. You can find out how to import them here: https://www.youtube.com/watch?v=YFQRunOw4PU.

Variables

Tasker is a bit like a programming language, and like any good programming language, you can use variables. We are going to need to set up one global variable for this project. That being, our %BEDROOM_LED_URL

This should be set like the following:

Screenshot_2024-11-24-11-35-23-46_78c564e752295d769e9a0ddd26e8199d.jpg

To find the local IP address of your controller, you can either look in the WLED App under network, or you can look at your router's network management page.

Start Sunrise Task

A Task in Tasker is the main way of doing anything with the app. Tasks represent some action that your phone can take. In our case, we need to send some requests to our %BEDROOM_LED_URL to turn on the lights. Create a new task under the Tasks menu and call it Start Sunrise. Once you've done that we will add one HTTP Request Action. You can add it by hitting the plus button on the edit task page and searching for it:

Screenshot_2024-11-24-11-41-10-25_78c564e752295d769e9a0ddd26e8199d.jpg

Once there, we will want to set the following parameters:

  • URL - We will set this to our global variable %BEDROOM_LED_URL
  • Headers - We want to add a Content-Type:application/json header. You can find it without having to type it in using the magnifying glass button
  • Body - set your body to the following: {"on":true, "seg":[{"id":0,"fx":104,"sx":30,"ix":110}], "pal":0}
  • Trust Any Certificate - true

A note on the Body, these are the parameters we are sending to the WLED JSON API. Some of the values may be different for you. For example, you may have multiple segments. If so you will need to update the "seg" array to match all the segments you want to use. Also, the "ix" parameter defines the length of your segment for the effect. My segment has 110 pixels, but yours may have more. Finally, the "sx" represents the duration in minutes of the animation. For the rest of this article, my animation will last 30 minutes and start 30 minutes before my alarm goes off. You can adjust this to suit your needs.

If you are following me exactly, your action should look like this:

Screenshot_2024-11-24-11-42-01-71_78c564e752295d769e9a0ddd26e8199d.jpg

You can test this task by running it manually and seeing if your LED strip reacts accordingly.

Turn Off LED Task

I wanted my LEDs to automatically turn off a bit after my alarm goes off and I've (theoretically) gotten ready for the day. To do that we are going to create a similar task to last time. We will have one action and it will just turn off the LEDs.

The only parameter this time that will be different is the Body, which should be set to this: {"on":false}

Make sure to test your task again.

Cycle Effect Task

One of the issues I ran into is that if the LED effect isn't changed between two wake-ups, then it will turn on at full brightness and not restart the animation. So for our sunrise job, we want to cycle the effect first just so we know that we are going to start the animation from the beginning. To do that we are going to create another task that is again, very similar to the first one. The body is the only thing that should be changing here, and here is what it should be set to: {"seg":[{"id":0,"fx":"~"}]}

Don't forget to update the segment count/ids if you are using more than one segment.

And finally, don't forget to test your task!

Queue Sunrise Task

This is the one that is going to tie everything together (and as such will be the most complicated). Here is a quick overview of the steps we will implement on our task:

  1. Find out when our next alarm is
  2. Set a variable to represent 30 minutes in ms
  3. Set %SUNRISE_LIGHT_TIME to our next alarm-30 minutes
  4. Set %time_to_wait to %SUNRISE_LIGHT_TIME - now
  5. Set %time_to_wait to 0 if it is less than 0
  6. If %time_to_wait is a reasonable amount of time:
  7. Perform the Cycle LED Effect Task
  8. Perform the Turn off LED Task
  9. Wait %time_to_wait
  10. Perform the Start Sunrise Task
  11. Wait 1 hour
  12. Perform the Turn off LED Task

Basically, we will figure out when 30 minutes before our next alarm is, and then wait until that time, upon which we will setup the LED strips and start our sunrise animation. After an hour we will turn the lights off and be done. I'll walk you through each step and tell you how to set it up.

Get Next Alarm

Our first task should be a Test Next Alarm task. This task gets some information about our next alarm and sets some local variables. There is no configuraiton for this task, and the variable we are interested in is %na_time_ms

Set 30 Minute Variable

Use a Variable Set action to set a variable called %thirty_minutes to 1000*60*30 to represent 30 minutes in ms.

Sunrise Time Variable

Use another Variable Set action to set a variable called %SUNRISE_LIGHT_TIME to %na_time_ms-%thirty_minutes which will be the timestamp exactly 30 minutes before your alarm will go off.

Time to Wait Variable

Again we are going to set another variable called %time_to_wait. We will set this to %SUNRISE_LIGHT_TIME-%TIMEMS where %TIMEMS is a variable that you can use at any point to represent the current number of milliseconds since epoch. As the name would imply, the resulting variable will be the number of milliseconds we need to wait before we start the sunrise.

Catch Error

If for whatever reason %SUNRISE_LIGHT_TIME is in the past, we will want to catch that and set it to 0. If later on we try to wait a negative amount of time then our job will fail. To do that we can do another Variable Set task, and have it set %time_to_wait to just 0. We can use an if clause on the action to only trigger if %time_to_wait is less than zero. This is what the variable set would look like:

Screenshot_2024-11-24-17-24-48-33_78c564e752295d769e9a0ddd26e8199d.jpg

If Statement

Now lets say that your next alarm isn't until the next day. You don't want tasker to be running a task for 24+ hours. To prevent this from happening what we want to do is start the sunrise only if we would wait a reasonable amount of time. I arbitrarily decided upon 4 hours, or 14400000 milliseconds. What we can do is create an If action and set our condition to %time_to_wait < 14400000. From here on out, the rest of the actions we do will be under this if statement.

Screenshot_2024-11-24-17-28-32-48_78c564e752295d769e9a0ddd26e8199d.jpg

Cycle Effect

Now, to make sure that when we start our sunrise it plays the whole animation, we will want to make sure that we are switching to the sunrise effect from another one. To ensure that, we can use a Perform Task action to run the task we created earlier. This action will have lots of parameters, but we don't need to use any of them, just make sure that you've selected the right action using the magnifying glass button.

Screenshot_2024-11-24-17-38-18-41_78c564e752295d769e9a0ddd26e8199d.jpg

Turn off LEDs

This is another Perform Task action similar to the previous one. This time, we'll want to run the Turn Off LED task to make sure the LEDs are off. I imagine they would be, otherwise you'd be sleeping with the lights on, but still, it's a good idea to cover your edge cases.

Wait

Now, we wait! We can use a Wait action to wait %time_to_wait milliseconds. To put in a variable, just press the little "remix" button to swap from constant to variable time.

Screenshot_2024-11-24-17-43-20-09_78c564e752295d769e9a0ddd26e8199d.jpg

Start Sunrise

After we've waited, we can start the sunrise task by doing a Perform Task action just like we've done before. This will start the animation.

Wait, Again

Now that it's started, we can theoretically be done, but I'd like the lights to turn off automatically too, so we are going to wait again. This time I'm just waiting one hour using a constant.

Turn off the Lights!

Alright, now that we've waited another hour, let's turn the lights off using the exact same Perform Task as before.

Final Results

After adding in all those steps, you should be looking at a task that looks like this:

Screenshot_2024-11-24-17-52-14-86_78c564e752295d769e9a0ddd26e8199d.jpg

You should be able to test this task manually just like the other ones we created earlier.

Scheduling the Task

Now that we have our task working, we need to schedule it so that it runs every morning automatically before we wake up. To do this we can use a profile. Profiles in tasker allow you to trigger tasks based on different conditions. For this project, we just need to run our Queue Sunrise task at a specific time every day. To create a profile, hit the plus button and then select the Time option. Here you will be presented with a form to select the duration of the profile. For our case, we can set both the From and To times to the same, to trigger once. I set mine to 4:30AM like so:

Screenshot_2024-11-26-13-28-50-73_78c564e752295d769e9a0ddd26e8199d.jpg

Once you back out of that menu you will be prompted to select the task that will be triggered by this profile. Select our Queue Sunrise task that we made in the last step. From here, we are finished! All that is left to do is test everything to make sure it gets triggered like we would expect. Here is what my profile looks like:

Screenshot_2024-11-26-13-32-54-89_78c564e752295d769e9a0ddd26e8199d.jpg

Final Thoughts

As mentioned in the introduction, I can't recommend this method for waking up enough. My wife and I both feel much more energized in the morning, and having the entire process be completely automated helps me to get about my day without any extra hassle. One final tip about debugging in tasker, if you select the monitoring tab in the context menu in the top right, you can turn on the Run Log which will log everything that tasker tries to do. This will help you diagnose any issues you have with your profiles/tasks. With that, I wish you good luck and success in your Tasker adventure!

Made with 😎 by Joshua McClung