A Light Control Pattern Using Home Assistant

I’ve been using Home Assistant as our home automation platform for quite a while now. The aspect of it that we experience most is probably the light control. Our apartment is equipped with IKEA Trådfri lights which are connected to the network gateway. Besides being cheap, one of the main advantages of the system is that the functionality works without internet access and integrates nicely with Home Assistant.

Over the last two years, I’ve tried to constantly improve the automations that control the lights with the goal of avoiding user interactions as much as possible by trying to guess what lighting is required at every point in time. I’m now at a point where I’m happy with the way things work and I’d like to share my setup and the thoughts behind it.


The automations below require some infrastructure components to provide the necessary data:

  1. The individual lights are set up and groups like group.livingroom_lights were created.

  2. A PIR sensor is installed in every room and connected to Home Assistant using a Java implementation of the Homie Convention (homie-java). Whenever motion is detected, a binary sensor such as binary_sensor.livingroom_motion will become active for a few seconds and afterwards return to inactivity.

  3. A user controllable setting for each room is defined to allow to override the automatic behavior. The name is input_select.livingroom_light_mode and possible values are auto, on, and off.

  4. Another setting that is being used is the bed time mode represented by input_boolean.bedtime. This is used to indicated that people are sleeping and thus the light in the bedroom will not turn on automatically and lights in other rooms will only run at minimum brightness.

  5. An ambient light sensor is placed at one of the windows and is constantly monitoring the brightness outside. Based on that value, some template sensors periodically compute a target brightness ranging from 0% to 100% for each room such as sensor.target_brightness_livingroom.

  6. A simple python script observes the currently active WiFi devices and updates a numeric sensor sensor.people_present with the number of people that are currently present in the apartment.


Turning the lights on and off through the switch

These automations are the least complex. Whenever a user sets the light mode to off or on, we immediately react and set them to off or maximum respectively:

Turning the lights on dynamically

This automation handles the activation of the lights when something moves in the room. It is only triggered if the lights are off and the mode is set to auto. When it is triggered, the automation doesn’t set the brightness to maximum but instead to the computed target brightness using a data template.

Turning the lights off dynamically

Conversely, lights are also turned off when no motion is detected for a certain amount of time. It turned out to be useful to have two different durations after which this is triggered:

  1. If we detect no motion for 20min, we simply turn off the lights.

  2. If we detect no motion for 2min, we also turn off the lights but only if the lights were on for less than 5 minutes.

This differentiation is used to separate people sitting in the room for a while from someone just quickly grabbing something. The automation is only active during the day and when the lights are set to auto.

Turning the lights off at night

At night, we don’t need this differentiation. Lights are simply turned off after 30s of no activity. This also only activates if the mode is set to auto to allow overrides at night as well.

Turning the lights off when only one person is home

If we know for sure that only one person is home, we can turn the lights off almost immediately if motion was detected in another room. Therefore, this automation triggers through any motion in another room after 10s of inactivity in the living room. Here, it is important that the people counter is working correctly and to have an override for when guests are at home. My override simply increases the count of people by one.

Periodically adjusting brightness

The final automation adjusts the brightness when the light mode is set to auto and only when the lights are on already. The automation is triggered every minuted and additionally when the state of the TV changes since this has an impact on the computed brightness in my case.

Initially, I had implemented this using the sun component or based on the weather report but only the dedicated sensor gave sufficient results.

The target brightness itself is computed by these templates. The sensor provides readings in lx and the formulas here compute a target light brightness for rooms with windows and rooms without windows. For the living room in particular, the value is modified and limited to 20% if the TV is running.

Conclusion and Next Steps

I hope this gave a good overview of the different automations and their interactions. While I am happy with the current setup, there are a few things that I’d like to improve in the future:

  1. Currently, there seems to be no way to define these automations at one location and then reuse them for different rooms. Theoretically, this could all be baked into the same automation with a lot of if and else but this would definitely degrade the readability.

  2. I was not able to use the Trådfri PIR sensors for this since they don’t expose their data but rather just directly control the lights. It should be obvious that many of the automations here wouldn’t be possible then. I have hopes, though, that IKEA eventually changes this.