Adaptive Day Cycle

Home automation has a bad reputation. You spend all that time setting your system up and in the unlikely scenario that you end up with something functional, you can’t even enjoy it. Because spending time with the automation defeats the purpose. It’s supposed to be automatic, right?

But in the end it doesn’t really matter (pun intended). Home Automation is as any other hobby, and the tinkering is part of the fun. But it does affect the people around you, and you don’t want to spend your time telling your family to not touch the light switches, so you better try to make something that actually works decently.

Home Assistant is an open source software for home automation. It allows all those different lights, sensors, cameras, and other devices that you got from a variety of manufacturers to work together as a cohesive whole, build your own automations and script into it.

You can run Home Assistant on a variety of hardware, such as a Raspberry Pi, a cheap single board computer. I personally run it on my own home server, but that is a topic for another time. Because Home Assistant has another important feature, at least for me. It allows you to code your own modules.

Allow me to set the stage.

Lighting is one of those things that is easy to start with, since smart lights are so accessible. But it’s also one of those things that is pretty difficult to make right. Using your smart speaker to tell the kitchen lights to turn on is not smart. It’s not automation. It’s just replacing a physical switch for an audio one. And we already did that in the 80s, with the clapping lights.

I did however want a lighting system that is more or less automatic. Dimming the lights when it’s time to watch a movie. Setting the mood for Xbox (yes, I still think they stand a chance. Optimist here). Slowly turning off the lights when it’s time to sleep. But none of that is any good if we can’t get the basics going, the correct lights at the right time of day.

But how the hell do you know what time of day it is. When is it morning? Evening? Night? You can check the sunset, but what if it’s cloudy and raining? What about in the middle of summer when daylight lingers way past sunset? Timers are too blunt. And boring. Sunrise and sunset too imprecise.

My goal was to set permanent scenes for the different times of the day. Dawn, morning, afternoon, dusk and evening. So I started building my first ever custom integration for Home Assistant.

Dawn was fairly easy. It’s gonna start at 5, because any earlier than that, I’m not gonna be up. Morning was more complicated. I want the sun to have risen, so I can turn most of the lights off. Afternoon start around midday or solar noon. Dusk starts about 2 hours before sunset and evening starts at sunset. Night would be at midnight, since we will be in bed then.

This is all based on sun cycles, but that is not enough. So, I added weather. Cloud cover would for example shift morning so it starts later, but would shift dusk and evening to start earlier. Rain even more so.

This worked pretty well, unless the weather was sunny. Since I was building this in May, the sun actually lingers pretty long after the sunset, so I needed another way to adjust?

I added a shift of ±45 minutes, where the maximum, 45, is at midsummer and the minium, -45 is at midwinter, and all numbers in between during the year. I now had a working adjustment, but I had one problem. I only worked for me.

Even though I’m building this for my own personal enjoyment, I want it to be possible for someone on the other side of the world to use it. What happens if someone lives in Australia, where summer and winter seasons are swapped? Or a country closer to the equator.

So, I brought in latitude. Latitude goes from 90 in the northern hemisphere to -90 degrees in the southern. So, if we normalize this to a value between -1 and 1 and multiply by the value we got before, we get a seasonal adjustment that shifts with the location of the user. At the equator, the adjustment would be 0 and in the northern and southern hemispheres it would be much greater.

And that is how I totally over-engineered this.

This is of course not the end of it. Lighting is exceptionally fun, and there are many more layers to this, which I will tell you about in an upcoming post. But you can get pretty far just knowing what time of day it is.

And if you want to try it out, the project is available on GitHub. If you want to install it you can either add it manually to your custom_integrations folder or use HACS to install it as a custom repository.