Tutorial - Advanced automatic light control with flows

Hi guys,

I thought it was finally time to share my flows that I use to automatically control my lights. The setup I have is a bit more advanced than the suggestions I have seen on this forum, which is the reason for this tutorial. Some features of my setup:

  • Automatically turn on and off lights based on motion (doh!)
  • Only turn lights on when it’s dark
  • Configuration of various scenes (e.g. Normal scene, Movie scene, Dinner scene, Romantic scene etc)
  • Memory ability that will remember the brightness of a light that you have manually set (either through a pulse switch, the homey app or any other app).
  • Configurable duration of the countdown, which makes it easy to dynamically configure the ‘light on time’ of a room.

I will be showing you how I set this up in my bedroom, but I also use this in all the other rooms of my apartment.

Let’s get started!

Prerequisites

In order to follow along with this tutorial, you’ll need to following things:

  • A sensor that is capable of measuring motion and lux (light intensity). For example, a NEO Coolcam
  • A light that can be controlled by Homey
  • The countdown app by Ralf van Dooren
  • Make sure all the apps are installed that are required to control your sensor and lights
  • (Optional) The simple log app by Niels de Klerk to log which flows are executed. This is optional, but I like this app because it gives me an overview of everything that is happening in my apartment and makes it easy to debug any issues I come across.

Flow naming convention

Before we dive into all the cool stuff, let me explain how I setup and name my flows. I divide my flows into two categories: events (ev) and actions (ac). I also prefix my flows with a code that identifies the room the flow is used in (BR for bedroom, LR for living room etc). For example, the flow that turns the lights on in my bedroom is called ‘BR_ac_LightsOn’. The reason for this is abstraction and re-usability. You can image you’d want to turn the lights on from multiple flows. Instead of turning on each individual light in every single flow, you would just call the flow ‘BR_ac_LightsOn’. Another benefit is that you can add these ‘action flows’ to your favourites so that you can quickly execute them.

Dark / Light detection

Let’s start with the first component of our setup: detecting whether it’s light or dark in our room. Obviously we only want to turn our lights on when it’s dark and not during the day (except on those miserable rainy days). Dark / Light detection can be used for many other use cases as well. For example, whenever I open the curtains in my bedroom on a weekend-day, Homey detects it’s light, marks me as awake and automatically starts my weekend playlist on my Sonos.

Variables

Create the following variables in the Homey app (more > logic). Obviously you can use your own names:

  • Bedroom_DarkLux (number). Configure this number to the threshold value (in lux) of dark/light. For example, I have set this value to 20, meaning every value below 20 is dark and every value above 20 is light. This value can differ depending on your room and the position of your sensor.
  • Bedroom_DarkLight (text). This is the variable that will keep track of whether your room is ‘dark’ or ‘light’. Set this to either ‘dark’ or ‘light’, since we will use flows to automatically set this value.

Flows

Create the following flows to enable dark / light detection:

  1. BR_ev_LumChanged_Dark. This flow checks if the luminance is below our configured threshold and then sets the light state to ‘dark’. Note how I verify whether the variable isn’t already set to dark in order to prevent this flow from firing over and over again.

  1. BR_ev_LumChanged_Light. This flow checks if the luminance is above our configured threshold and then sets the light state to ‘light’. Note how I check whether one of my lights is turned off, before I tell Homey that the room is actually ‘light’. This is because we don’t want to tell Homey that the room is light when we have a light turned on.

Memory function for the brightness of our lights

Imagine you are doing something in your room that requires a lot of light. Whether it is hanging up new curtains or doing some overdue cable management behind your TV. I find it really annoying that Homey dims the lights back to its pre-configured value when I have put it all the way up in order to see what I am actually doing. This is why I have come up with a memory function that remembers the dim level of a light, so that value can be used to turn the light on based on motion. The idea is that you store the dim level of the light in a variable and use that variable to dim the light whenever you want to turn that light on.

Variables

Go ahead and create the following variables in the Homey app (more > logic):

  • BR_DimLevel_BedLeft (number). This variable is used to store the dim level of the light ‘Bed left’.
  • BR_DimLevel_BedRight (number).
  • … repeat for every light in your room

Flows

  1. BR_ev_DimBedLeftChanged. This flow triggers when the brightness of your light has changed and stores the value in the appropriate variable. It doesn’t matter whether you use Homey, a physical switch or the native app (hue, lifx, ikea etc) to dim the light.

  1. BR_ev_DimBedRightChanged.

  1. …repeat for all the lights in your room.

Flow actions to turn the lights on and off

We need some ‘action flows’ that allow us to turn our lights on and off before we can link our lights to our motion sensor.

Flows

  1. BR_ac_LightsOn. This flow dims our lights to our configured variable. I guess there is a bug in ‘flow sharing’ (note the NaN%s). Make sure to use the variables (BR_DimLevel_BedLeft and Right) we created in the previous section. Make sure to include all the lights in your room.

  1. BR_ac_Lights_off. This flow turns our lights off.

Setting up our motion sensor flows

We can finally start with the fun stuff, now that all our building blocks are in place. Before we create our variables and flows, let’s head over to the Countdown app to create our timer:

  • Go to the countdown app in the Homey app
  • Click configure
  • Add a timer with the name ‘Bedroom’

The idea of using the timer to control our lights is as follows. Our sensor will send us two events: a motion on and a motion off event. Motion on is sent whenever the sensors detects motion. Motion off is sent whenever no motion has been detected for a certain period by the sensor. To make sure the lights do not turn off too soon, we start a timer and only turn the lights off when that timer hits 0.

Variables

  • Bedroom_LightTime (number). The amount of seconds the timer will run before turning off the lights. Set it to 60 for now or adjust to your liking.

Flows

  1. BR_ev_Motion_on_dark. This flow will turn the lights on when motion has been detected and when it is dark. Note that I also check that nobody is sleeping, since this flow is used in my bedroom. You can omit this check if you are using this flow in any other room.

  1. BR_ev_Motion_off.. This flow starts our timer with our pre-configured timeout when no motion has been detected for a while.

  1. BR_ev_timer_0. This flow turns our lights off when our timer hits zero. Note how we are using our ‘action flows’ we defined in the previous section.

Configuring scenes

Now that everything is setup we can start defining our scenes. I use scenes to set my lights to a specific ‘mood’ that’s appropriate for that time. In my bedroom I use a ‘normal scene’ for ordinary use, a ‘movie scene’ for when I am watching Netflix and a ‘romantic scene’ for when I… well you can fill in the blank :wink: . We will be using ‘Virtual switches’ to define our scenes. A virtual switch is a virtual (non physical) device that can be added to Homey. It will show up as a device under the ‘devices tab’, just like any physical devices do. This feature is still marked as experimental (although I have never had any issues with it), so you need to head over to more > settings > experiments to enable ‘Virtual devices’ in the Homey app. After enabling this, go to devices > + > Homey > Virtual Switch to add a switch. I have added the following switches:

  • Normale scene
  • Movie scene
  • Candle scene (aka my romantic scene)

Add as many (or as few) as you’d like. Note that you can completely skip this section if you only need one ‘scene’ in your room.

Flows

It’s extremely easy to implement scenes in our setup. Define a flow for every scene you have added and make sure to turn off the switches for all the other scenes. Also note the ‘Else’ part where I do not turn on my lights, but just set the brightness levels of my variables whenever a scene is activated when it’s light in the room. This will make sure then whenever it gets dark, the appropriate dim levels are used when turning on the lights. Also note that I dynamically increase the light time whenever I enable the romantic scene and change it back when either the normal scene or movie scene are enabled.

  1. BR_sc_LightsNormal. This flow activates the normal scene.

  1. BR_sc_LightsMovie. This flow activates the movie scene.

  1. BR_sc_LightsCandle. This flow activates the romantic scene.

Inspiration for using scenes in other flows

Now that we have setup our scenes, we can use them in other flows to control our lights. I will show you how I use a chromecast in order to automatically turn on the movie scene. You can of course use anything you like to trigger the scenes we have just created.

Flows

  1. BR_ev_NetflixStarted. This flow triggers when Netflix is started on my chromecast. Note how easy the setup of this flow is because of our modular setup.

  1. BR_ev_NetflixStopped. This flow triggers when Netflix is stopped on my chromecast.

‘Reset flows’

Whenever I have manually set the brightness of my lights, I eventually want them to automatically return to their default values, so that the next day, when it gets dark, the lights turn to my ‘normal scene’ again. The way I do this is by using, what I call, ‘reset flows’. The idea is to fire a flow at a specific time to restore your home back to its default state. For example, I fire my ‘reset flow’ at 2pm and also mark me and my girlfriend as ‘awake’ (in case we forget to use our flic buttons), clear my log, reset my light times etc.

Flows

  1. Home_ev_ResetDaily14h

Alright, that’s all folks! I hope you find this tutorial useful or hopefully you can at least draw some inspiration from it to automate your own home. Feel free to post any messages if you have any questions regarding this tutorial.

Cheers,
Marvin

62 Likes

Nice work @Marvin_Schenkel! thank you

1 Like

Great! :+1:

Very nice !

Thank you Marvin!
That is a nice, refreshing and polished inspiration
:clap::clap::clap:

1 Like

Nice work!

I want to translate this in the French community when its okay for you? Can help much people…

Sven.

EDIT : DONE

3 Likes

It is definitely okay to translate it to french :). Glad you like it and thanks for taking the time to translate it to French.

1 Like

Nice tutorial! But I’m curious why you use virtual switches right in the flows and not virtual buttons with a variable? Now when you set a scene, you have to switch off all other scenes (virtual switches) in that flow.

For example:

  • You create 3 virtual buttons: normal, movie, candle
  • You create 1 variable: scene status
  • Then you create 3 flows. The only thing that those flows do is setting the variable ‘scene status’ to ‘normal’, ‘movie’ or ‘candle’.
  • Then you have you have 3 flows to set the lights. These flows are triggered by the variable that is changed. Like this for the ‘normal scene’: 'when logic ‘scene status’ is changed, and ‘scene status’ = ‘normal’ than dim lights to…

Now you never have to switch off the other scenes, becaus the variable ‘scene status’ can only have 1 vallue.

1 Like

That’s another way to do it and was actually similar to my previous implementation. However, I prefer switches over buttons because I can see which scene is active by looking at the devices, since the inactive scenes are grayed out. This is not the case when using buttons. It also makes it more intuitive when displayed in a dashboard in my opinion.

2 Likes

Yeah that’s a good reason to do it like that. And I also found out that triggering on a variable isn’t working good: Triggeren op veranderen variabel 'logica' naar bepaalde waarde werkt niet goed (Dutch)…

Nice tutorial!

One question:
Why do you check for dark in your action flow BR_ac_LightsOn? The check for dark is already done in movement flow BR_ev_Motion_on_dark. Or keep the check and rename the on motion event to a general event, not just during dark.

regards from one who tries to understand :wink:

Dag-Inge

That’s actually a very good question. One of them is redundant and can be removed. I would remove the one from BR_ac_LightsOn. This way, you can still add this flow to your favourites, allowing you to quickly turn all the lights on whenever you want to :).

Thank you.

One more:
When you change scenes, the lights turns on even though no one is in the room. Right? I’ve added a check for activity in the room to prevent this. Am I missing something here?

Regards,
Dag-Inge

1 Like

Yes you are correct. I have ‘solved’ this by not turning on the lights when it’s between 13:59 and 14:01 (i know, hacky). I like your solution better!

1 Like

In the flow “lights on”, I get an error “token out of range”, when try do dim lights from the variable I made. However, if the lights are already on it seems to work. What am I doing wrong?

dim value is in percentage, so 0.00 - 1.00, so if your own made variable is using numerical 0 - 100 then it will not work.

Yep, that’s it. Thank you! :slight_smile:

Works fine with Hue lights, but looks like turning off Fibaro dimmer sets dim lvl to 0…

Yup that is the downside of z-wave (not just fibaro), the dim level == on/off state, 0 is off, anything higher is on, there is no different command/thing/information for just the on/off state

Okay, did not know that. So I guess I have to used fixed value on those…