Hello everyone,
I would like to share with you all my latest script for home assistant. This script is designed to handle the lighting in a room based on presence, brightness, natural light based on the sun and scenes. It will automatically turn on the lights when it detects low brightness and someone is in the room. It also takes into account the natural light based on the sun and adjusts the lights accordingly. Moreover, you can set up multiple scenes to fit different scenarios and moods.
I hope you will find it useful and Iâm eager to hear your feedback.
Best regards,
Full Automation Lights
Introduction
Are you tired of getting up from your comfortable bed to check if you turned off the living room light? Are you annoyed by finding the bathroom light on all night? Are you disturbed by bright hallway lights in the middle of the night when you have to use the bathroom? Donât worry! With this magic (but not too much) script, you can automate all of this in no time!
This AppDaemon-based script works with Home Assistant. Itâs designed to automatically control your home lighting based on different criteria such as brightness and sun elevation. No need to worry about commands, your home will take care of everything! And itâs open-source, so you can improve it as you like or adapt it to your needs.
Donât hesitate any longer and install it to live in a perfectly lit home! And if you encounter any problems, donât hesitate to consult our documentation, itâs there for that!
Features
- Room Occupancy: The script uses occupancy sensors to detect the presence of people in a room and adjusts the lighting accordingly. It is possible to set brightness limits and hysteresis for each room. There is also an option to turn off lights automatically if the natural light in the room is high.
- Natural Light: The script uses the position of the sun to automatically adjust the brightness and color temperature of the lights based on the sunâs elevation. It is possible to set different natural light modes for each room.
- Scenes: The script allows for automatically triggering scenes based on the state of a specific trigger, such as the turning on of a TV. It is possible to set different scenes for each room.
- Configuration for multiple rooms: it is possible to configure multiple rooms with different options for lighting automation and scenes. There is a configuration for the living room and another for the kitchen in the provided example.
- Advanced Configuration: The script offers great configuration flexibility for experienced users, allowing for setting parameters such as brightness limits, natural light modes, scenes, and occupancy sensors for each room in the house.
Prerequisites
- Home Assistant: A functional installation of Home Assistant is required to use this script.
- AppDaemon: AppDaemon is an add-on for Home Assistant that allows you to run Python scripts in the background. AppDaemon must be installed and configured to use this script.
Installation
Once you have Home Assistant and AppDaemon running, you can install this application by following these steps:
- Download the FullAutomationLights.py file and place it in the âappsâ directory of AppDaemon.
- Add the following configuration to your apps.yaml file. This file is usually located in the âappsâ directory of AppDaemon.
Configuration
Example of minimum configuration to turn on and off the light âlight.living_roomâ in the room âliving_roomâ when a motion is detected with the sensor âbinary_sensor.living_room_motionâ. âlight.living_roomâ is turned on with a simple âturn_onâ without any parameters :
FullAutomationLights:
module: FullAutomationLights
class: FullAutomationLights
rooms:
living_room:
occupancy_entity: binary_sensor.living_room_motion
lights_entity: light.living_room
Example of a configuration with the natural light feature. Here, ânatural_lightingâ is set to turn on the feature. By default, a natural light âmodeâ named âdefaultâ is created. In the âliving_roomâ room, the âlight.living_roomâ is linked to this mode :
FullAutomationLights:
module: FullAutomationLights
class: FullAutomationLights
natural_lighting:
rooms:
living_room:
occupancy_entity: binary_sensor.living_room_motion
lights_entity: light.living_room
natural_lighting:
- name: default
lights_entity: light.living_room
The following advanced configuration sets the transitions between scenes and natural lighting with a transition time of 5 seconds for scenes and 30 seconds for natural lighting.
Natural lighting is configured with different parameters such as the entity associated with the sun, the limits of the elevation angle for brightness and temperature in Kelvin, as well as the ânl1â and ânl2â modes defined with their own range of brightness and temperature in Kelvin.
Then, the configuration defines the rooms âliving_roomâ and âkitchenâ. For each room, it defines the entities associated with presence, luminance, luminance limit, and lights. It also defines the natural lighting modes and the scenes associated with these rooms. For example, for âliving_roomâ, it defines the scenes associated with ambient lighting for TV and TV on. In both rooms, different light groups are associated with the two natural lighting configurations ânl1â and ânl2â.
âboost_brightness_pctâ is used in the ânatural_lightingâ settings of âliving_roomâ. It represents the boost percentage of brightness for a specific light. The ânl1â mode has a brightness boost of -30%. This means that the brightness will be reduced by 30% for the lights specified in the âlights_entityâ (light.living_room_group_1).
âhigh_luminance_off_lightâ is also set to âtrueâ in âliving_roomâ. This means that, unlike in âkitchenâ, if the luminance captured by âsensor.living_room_luminanceâ is above 50 + 5, the light will turn off. Since this feature is not used in âkitchenâ, âluminance_hysteresisâ is not defined and is therefore equal to its default value, which is 0.
FullAutomationLights:
module: FullAutomationLights
class: FullAutomationLights
debug: true
transitions:
scenes: 5
natural_lighting: 30
natural_lighting:
sun_entity: sun.sun
min_elevation_for_brightness: -20
max_elevation_for_brightness: 20
min_elevation_for_kelvin: 0
max_elevation_for_kelvin: 20
modes:
- name: nl1
max_brightness: 255
min_brightness: 100
max_kelvin: 5500
min_kelvin: 2000
- name: nl2
max_brightness: 255
min_brightness: 100
max_kelvin: 5500
min_kelvin: 2700
rooms:
living_room:
occupancy_entity: binary_sensor.living_room_motion
luminance_entity: sensor.living_room_luminance
luminance_limit: 50
luminance_hysteresis: 5
hight_luminance_off_light: true
lights_entity: light.living_room
natural_lighting:
- name: nl1
lights_entity: light.living_room_group_1
boost_brightness_pct: -30
- name: nl2
lights_entity: light.living_room_group_2
scenes:
- scene_entity: scene.living_room_ambilight
scene_trigger_entity: switch.android_tv_ambilight_hue
scene_trigger_value: "on"
- scene_entity: scene.living_room_tv
scene_trigger_entity: media_player.android_tv
scene_trigger_value: "on"
kitchen:
occupancy_entity: binary_sensor.kitchen_motion
luminance_entity: sensor.kitchen_illuminance
luminance_limit: 35
lights_entity: light.kitchen
natural_lighting:
- name: nl1
lights_entity: light.kitchen_spots
- name: nl2
lights_entity: light.table
The FullAutomationLights configuration includes the following settings :
-
module:
etclass:
These parameters tell AppDaemon where to find the FullAutomationLights.py file and which class to use to start the application. -
debug:
(Optional) [boolean] This option enables or disables the debugging messages from the application. It is recommended to set it to âtrueâ during the first installation to check that everything is working correctly. The debug function is chatty and slows down the script. For production, it is best to deactivate it. Default: false -
transitions:
(Optional) This section allows for defining custom transitions.-
init:
(Optional) [integer] duration of the transition in seconds when the script is initialized. Default: None -
occupancy:
(Optional) [integer] duration of the transition in seconds when changing state due to the âoccupancy_entityâ sensor. Default: None -
low_light:
(Optional) [integer] Duration of the transition in seconds when changing state due to the âluminance_entityâ sensor. Default: 15 -
scenes:
(Optional) [integer] duration of the transition in seconds when changing the scene. Default: 5 -
natural_lighting:
(Optionel) [integer] duration of the transition in seconds when changing the natural lighting. Default: 10 -
off:
(Optional) [integer] duration of the transition in seconds when turning off the lights. Default: 3
-
-
natural_lighting:
(Optionel) This section allows configuring natural light management. It includes several sub-parameters.-
sun_entity:
(Optional) [string] Sun entity from Home Assistant is used to determine the sun elevation. Default: sun.sun -
min_elevation_for_brightness:
etmax_elevation_for_brightness:
: (Optional) [integer] These parameters determine the range of sun elevation in degrees during which the brightness of the lights is adjusted. Default: -20 and 20 -
min_elevation_for_kelvin:
etmax_elevation_for_kelvin:
: (Optional) [integer] These parameters determine the range of sun elevation in degrees during which the color temperature of the lights is adjusted. Default: 0 and 20 -
modes:
(Optional) This section allows for defining natural light modes for different ranges of sun elevation. Each mode can have the following parameters:-
name:
The name of the mode. This name is used to link lights to a mode in the room configuration. -
max_brightness:
etmin_brightness:
(Optional) [integer] The brightness limits for this mode. Default: 255 and 100 -
max_kelvin:
etmin_kelvin:
(Optional) [integer] The color temperature limits for this mode. Default: 5500 and 2000
-
-
-
rooms:
This section allows specifying the parameters for each room in the house.-
name of the room:
-
occupancy_entity:
[string] The entity used to detect occupancy in the room. This can be a motion sensor, switch, etc. -
luminance_entity:
(Optional) [string] Entity used to measure the roomâs luminosity. -
luminance_limit:
(Optional) [integer] The brightness threshold below which the room lights will automatically turn on. Default: 10 -
luminance_hysteresis:
(Optional) [integer] Value used to define a range of luminance aroundluminance_limit
, within which the lights in the room will be automatically turned on or off. Default: 0 -
hight_luminance_off_light:
(Optional) [boolean] Value is used to define if the room lights should be automatically turned off in case of high luminosity (greater thanluminance_limit
+luminance_hysteresis
). Default: false -
lights_entity:
[string] Light entity used to control the lights in the room. It can be a group of lights or an individual light. -
natural_lighting:
(Optional) This section allows you to specify the natural lighting parameters for each room. You can specify different lighting modesname:
with different brightness and color temperature parameters. You can also specify individual light entities for each lighting mode.-
name:
(Optional) [string] Name of the natural lighting that is desired to be used. It is possible to define several different names based on the userâs needs, for example âprimaryâ and âsecondaryâ. -
lights_entity:
(Optionel) [string] Entities that will be controlled based on the sun elevation. -
boost_brightness_pct:
(Optionel) [integer] This setting allows you to increase or decrease the brightness of the lighting by percentage.
-
-
scenes:
(Optional) Scenes are triggered when an event is triggered. For example, specific lighting when the TV is turned on.-
scene_entity:
[string] Scene that we want to activate. This entity can be a scene or a script. -
scene_trigger_entity:
[string] The entity that triggers the activation of the scene defined inscene_entity
. This entity can be a switch, sensor, TV or any other item that can send a specific value. -
scene_trigger_value:
[] The value thatscene_trigger_entity
must take for the scene defined inscene_entity
to be activated. For example, ifscene_trigger_value
is set to âonâ, the scene will only be activated whenscene_trigger_entity
switches to the âonâ state.
-
-
-
It is possible to define multiple scenes for each room by adding multiple âscenesâ blocks in the configuration.
Example:
scenes:
- scene_entity: scene.ambilight
scene_trigger_entity: switch.android_tv_ambilight_hue
scene_trigger_value: "on"
- scene_entity: scene.tv
scene_trigger_entity: media_player.android_tv
scene_trigger_value: "on"
It means:
If the switch âandroid_tv_ambilight_hueâ is turned on, the scene âsalon_ambilightâ is triggered.
If the media player âandroid_tvâ is turned on, the scene âsalon_tvâ is triggered.
Note that the scenes are in a priority order. If both âtriggersâ are active, âscene.salon_ambilightâ will be triggered.
Order of operations
- Presence in the room?
- Yes
- Low luminance?
- Yes
- Scene configured and active?
- Yes
- Launch the active scene
- No
- Natural lighting configured?
- Yes
- Turn on light based on natural light and adjust while it is on
- No
- Simply turn on the light
- No
- Turn off if high_luminance_off_light = true
- No
- Turn off
Future improvement possibilities
- Clean up of the code and improvement of performance
Thank you
- Thank you to @jlpouffier for introducing me to Appdaemon through his YouTube channel (https://www.youtube.com/@HorizonDomotique).
- Thank you to ChatGPT for their valuable assistance in writing the documentation and translating it into English.
- Thank you to my partner for proofreading this documentation.