Home Assistant Lights Automations
21 August, 2020 - 7 min
I decided to start automating things around my flat, starting with the lights. It was always something I thought could be easily automated, tweaked, and optimized in a continuous process. The more you reflect on your habits at home, the more you can tweak the automations.
I found Home Assistant (HA), an open-source home automation software, can't remember exactly how, but after a bit of researched if seemed to be the appropriate tool for the job.
I use HA to control my living room, kitchen and bedroom lights. My long term plan is to automate as many things as I possible can, and also learn as much as possible along the way.
Right now this is the setup I have:
- Kitchen - motion/light/temperature sensor and two celling lights.
- Bedroom - motion/light/temperature sensor, bed presence sensor, bedside table light.
- Living room - One lamp light and one wall light.
Automations
With that setup I've created a set of automations. Here are the automations I currently have, and how I implemented them.
1. Kitchen
- Motion sensor triggers the lights to turn on for 30 seconds, or 2 minutes depending on if cooking mode is on (more on that in a moment). Additional movement during this period will reset this timer, therefore keeping the lights on for an additional 2 minutes. So far, the 2 minutes threshold works very well, the lights have never turned off while I was cooking.
- After 30 minutes of no movement, cooking mode is automatically turned off.
Turn on kitchen lights automatically with motion sensor
This configuration will set the kitchen motion sensor to turn on the kitchen's lights if the light sensor's level is bellow 30.
#/config/automations.yaml
- alias: Turn on kitchen lights automatically
trigger:
platform: state
entity_id: binary_sensor.hue_motion_sensor_2_motion
to: 'on'
condition:
condition: numeric_state
entity_id: sensor.hue_motion_sensor_2_light_level
below: 30
action:
service: light.turn_on
data:
entity_id:
- light.c1
- light.c2
Cooking mode
I manage the cooking mode using an MQTT topic. Publishing either True or False.
To configure an MQTT topic we first need to setup MQTT on HA. I use the Mosquitto Broker. I recommend you to follow this link to set it up. After you have MQTT setup, you can follow the steps bellow to understand how to configure a similar type of sensor.
The following configuration sets the MQTT topic home/kitchen/cooking, and names the sensor Cooking. The sensor is accessible in automations with the name sensor.cooking.
#/config/configuration.yaml
# see https://www.home-assistant.io/integrations/sensor.mqtt
sensor:
- platform: mqtt
state_topic: 'home/kitchen/cooking'
name: 'Cooking'
After the sensor is configured it's time to create an automation using it. In the following configuration, I create a trigger for the kitchen motion sensor. It turns off both kitchen lights after 30 seconds of no movement (off motion sensor state) or 120 seconds when the sensor.cooking is set to 'True'.
#/config/automations.yaml
- alias: Turn off kitchen lights automatically
trigger:
platform: state
entity_id: binary_sensor.hue_motion_sensor_2_motion
to: 'off'
for:
seconds: >
{{ '120' if is_state('sensor.cooking', 'True' ) else '30' }}
action:
service: light.turn_off
data:
entity_id:
- light.c1
- light.c2
The way I set the sensor to 'True' is by using the HA android application's Service Button widget. See the image below.
Fig. 1 - Service Button widget.
Turn cooking mode off automatically
This following configuration allows me to reset the cooking mode. After 30 minutes of no motion detected, it will publish 'False' to the MQTT's topic home/kitchen/cooking.
#/config/automations.yaml
- alias: Turn off cooking mode after 30 minutes of kitchen inactivity
trigger:
platform: state
entity_id: binary_sensor.hue_motion_sensor_2_motion
to: 'off'
for:
minutes:
30
action:
service: mqtt.publish
data_template:
topic: 'home/kitchen/cooking'
payload:
'False'
2. Bedroom
- Motion sensor triggers the bedside table light to turn on for 2 minutes if the light sensor detects low light. (Similar to kitchen lights.)
- Between 10pm and 6am, when the bed weight sensor detects I'm on the bed for 10 seconds, it will turn of all the lights in the flat (except in the bedroom).
Bed presence sensor turns off all lights
In order to manage the weight sensor's state I also use MQTT, just like in the cooking mode explained before.
The configuration (1) bellow, sets the topic home/bedroom/bed/raw to be used for the Bed Presence MQTT sensor. By setting the unit_of_measurement to raw it makes the topic's values being interpreted by HA as continuous.
The configuration (2) creates a boolean sensor from the value of the sensor.bed_presence. True if less than 7.3M, or False otherwise. This value comes from the tests I made on the weight sensors. When I'm on the bed the values drop to around 7.2M/7.1M. When I'm not it stays around 7.5M.
I'll create an article detailing the weight sensors setup process in the future. For now, you can check this or this articles, both great, which I followed to build my setup.
#/config/configurations.yaml
sensor:
# (1)
- platform: mqtt
state_topic: 'home/bedroom/bed/raw'
name: 'Bed presence'
unit_of_measurement: "raw"
# (2)
- platform: template
sensors:
vicente_in_bed:
friendly_name: "Vicente in Bed"
value_template: >
{{ states('sensor.bed_presence')|float < 7300000 }}
The following automation uses the sensor.vicente_in_bed to turn all the lights in the house off (except on the bedroom). When the sensor's state changes from 'False' to 'True', and stays 'True' for 10 seconds, between 9 pm and 6 am, the lights are turned off.
#/config/automations.yaml
- alias: Turn off living room lights when I go to bed
trigger:
platform: state
entity_id: sensor.vicente_in_bed
from: 'False'
to: 'True'
for:
seconds: 10
condition:
condition: time
after: '21:00:00'
before: '06:00:00'
action:
service: light.turn_off
data:
entity_id:
- light.tall_lamp
- light.wall
- light.c1
- light.c2
3. Living room
- Lights turn on 45 minutes before the sunset, if I'm home (more on this in a second).
- If I arrive home and it's after 30 minutes before the sunset and before midnight, the living room lights will turn on.
- When I shutdown my desktop computer, it will turn off all the lights after 1 minute (except the ones on my bedroom).
On the following configuration, you can see minor changes in relation with the ones I've shown before. But I decided to post them as they use different new things, like the sunset or device tracker state.
#/config/automations.yaml
# (1)
- alias: Turn on living room lights when I'm at home and it's dark
trigger:
platform: sun
event: sunset
offset: "-00:30:00"
condition:
condition: state
entity_id: device_tracker.galaxy_s10
state: 'home'
action:
service: light.turn_on
data:
entity_id:
- light.tall_lamp
# (2)
- alias: Turn living room lights ON when I arrive home
trigger:
platform: state
entity_id: device_tracker.galaxy_s10
from: 'not_home'
to: 'home'
for:
seconds: 5
condition:
condition: sun
after: sunset
after_offset: "-00:30:00"
action:
service: light.turn_on
data:
entity_id:
- light.tall_lamp
# (3)
- alias: Turn off living room lights when I shutdown my desktop
trigger:
platform: state
entity_id: sensor.desktop
from: 'ON'
to: 'OFF'
for:
seconds: 60
condition:
condition: time
after: '22:00:00'
before: '06:00:00'
action:
service: light.turn_off
data:
entity_id:
- light.tall_lamp
- light.wall
- light.c1
- light.c2
On the following configurations:
- The (1) is the MQTT topic used for the sensor.desktop.
- The (2) runs nmap to map the devices on my network, and creates virtual devices I can use to track when they are connected. The device_tracker.galaxy_s10 is created from nmap being run.
#/config/configurations.yaml
# (1)
sensor:
- platform: mqtt
state_topic: 'home/living_room/desktop'
name: 'Desktop'
# (2)
device_tracker:
platform: nmap_tracker
hosts: 192.168.0.101-104
interval_seconds: 15
home_interval: 10
General
- When I leave the house, all the lights are turned off.
Again, very similar to previous examples. Just checks of device_trackes.galaxy_s10 changes for home state to not_home for 5 minutes, and then turns off all the lights.
#/config/automations.yaml
- alias: Turn off all lights when I leave the house
trigger:
platform: state
entity_id: device_tracker.galaxy_s10
from: 'home'
to: 'not_home'
for:
minutes: 5
action:
service: light.turn_off
data:
entity_id:
- light.tall_lamp
- light.wall
- light.cb
- light.c1
- light.c2
I hope you got some introduction on light automations using Home Assistant. I will probably continue to change and adapt these automations for a long time. It's a continuous process where I keep a mindset of trying to improve the system. It's a fun game.
Thanks for reading,
Vicente