The Perfect Wake-Up-Light Automation With OpenHAB

Johannes Schildgen
(Smart)²Home
Published in
5 min readAug 28, 2022

A wake-up light is a routine that slowly dims up the light in the bedroom just before the alarm clock rings. The following chart illustrates a wake-up light that starts 20 minutes before the alarm at 7:00 in the morning.

Wake-up light increases before the alarm clock rings at 7 o’clock.

The goal is to prepare the body for softly waking up and having a better start in the day.

You can buy alarm clocks with a built-in wake-up light functionality, but you can also do it yourself, for example, with OpenHAB. And in my opinion, that’s a much better solution!

What makes a wake-up light perfect?

  • The wake-up light starts automatically without reconfiguring it every day; this means it needs access to your regular alarm clock (e.g., your Android phone or Amazon Echo).
  • The wake-up light is not a dedicated light, but it controls all regular lights in the bedroom: ceiling lights, floor lights, …
  • The color of the light should be cold white.
  • Other actions can start as soon as the wake-up light procedure is complete, e.g., opening the roller shutters, starting the coffee maker, or heating the bathroom.

Prerequisites

You need:

  • an OpenHAB 3 server,
  • smart lights (e.g. Philips Hue, Homematic, Shelly, …),
  • optional: Amazon Echo, Android Phone, Smart Rollershutters (Homematic, Shelly, …), …

How to Access The Alarm-Clock Time?

We want to create an item in OpenHAB that stores a DateTime value of when the next alarm will ring. Depending on which alarm clock you are using, there are multiple options or binding. We show now two solutions, one for Android phones, and one for Amazon Echo devices. Alternatively, you can also manually set the alarm directly in the rule, or create an OpenHAB schedule, or create a DateTime item and the ability to set the alarm manually in the BasicUI or so.

Accessing the Android Alarm Time

If you are using an Android phone as your alarm clock, it’s very easy. First, create an item in OpenHAB, call it AlarmClock:

It’s important to select the correct datatype DateTime. This item will hold the value of the next alarm clock. When no alarm is set, it will be NULL.

Next, in the OpenHAB app for Android, go to settings and then “Send device information to server”. There, you can tap on “Alarm time” and specify the name of the Item: AlarmClock.

If you have multiple Android devices, you can also create multiple items, using a prefix, e.g. PeterAlarmClock. Specify this prefix in the Android app and name the item in OpenHAB accordingly.

That’s it. Your item will now automatically be set to the next alarm time by your mobile phone.

Accessing the Amazon Echo Alarm Time

When you are using Alexa devices as your alarm clock (Echo Show, Echo Dot, …), you can install the Amazon Echo Control binding in OpenHAB. Next, you need to set up the binding and add your Echo device:

  1. Add a new Thing in OpenHAB: “Amazon Account”
  2. Open http://<OpenHAB IP Address>:8080/amazonechocontrol/ in your browser
  3. You will be forwarded to Amazon to log in
  4. In OpenHAB, search for new things. Your Echo devices should be automatically discovered now. Add them.

In your Echo device Thing, you see a long list of available channels. To see the “Next Alarm” channel, you need to click “Show advanced”:

Create a new item of type DateTime for the channel Next Alarm, e.g. EchoBedroom_NextAlarm.

Wake-Up Light in OpenHAB

The central element of our wake-up-light approach is an items that we call “WakeUp”. Create that item and choose the data type Number. The meaning of that number is as follows:

  • WakeUp = -1 => Wake-up-light procedure is not active
  • WakeUP = 0 => Wake-up-light procedure just started
  • WakeUP > 0 => The number of seconds it is already running

This means, we need two rules:

  1. WHEN: 20 minutes before the alarm rings
    THEN: set WakeUp = 0
  2. WHEN: every 5 seconds
    IF: WakeUp >= 0
    THEN: dim up the light and increase WakeUp += 5; when finished, then reset it to WakeUp = -1;

And these are the full two rules now:

Rule 1: Check the Alarm and Activate WakeUp

var OFFSET = 20;var alarm = Date.parse(items.getItem("EchoBedroom_NextAlarm").state);
var now = new Date();
if (now >= (alarm-1000*OFFSET*60) && now < (alarm-1000*(OFFSET-1)*60)) {
items.getItem("WakeUp").sendCommand(0);
}

Mind that for this and the other rule, the new improved avaScript syntax (ECMA 262 Edition 11) is used.

Explanation of this rule:
As it is not possible to trigger the rule 20 minutes before the alarm rings, we need to check every minute: Will the alarm ring in 20 minutes? If yes, we set WakeUp = 0 to trigger the start of the wake-up procedure.

Rule 2: WakeUp Procedure

var DURATION = 20 * 60;   // 20 minutesvar seconds = parseInt(items.getItem("WakeUp").state);
items.getItem("WakeUp").sendCommand(seconds+5);
if(seconds == 0) { // Initialization
items.getItem("Bedroom_color_temperature").sendCommand(18);
}
var level = 0;
if(seconds/DURATION <= 0.5) { // First half, dim from 0 to 10 percent
level = Math.ceil(10 * 2 * (seconds/DURATION));
} else if(seconds/DURATION <= 1) { // Second half, dim from 10 to 100 percent
level = 10 + Math.ceil(90 * 2 * (seconds/DURATION - 0.5));
} else { // Finished
level = 100;
items.getItem("WakeUp").sendCommand(-1);
// Open rollershutter a bit
items.getItem("RollershutterBedroom_1_LEVEL").sendCommand(80);
}
if(items.getItem("Bedroom_Brightness").state != level) {
items.getItem("Bedroom_Brightness").sendCommand(level);
}

Explanation of this rule:
Firstly, the WakeUp varible is increased by 5 so that the next time the rule triggers (in 5 seconds) it will be increased. So, the variable WakeUp always shows how long the procedure is running.

Bedroom_color_temperature is set to a cold white.

For the first half (minute 0 to 10), the light is increased very slowly. From 0% to 10% brightness. In the second half (minute 10 to 20) the light increases faster (from 10% to 100%). See also the chart at the beginning of this article.

When the procedure is finished, the rollershutters are opened.

--

--