One of the simplest home automations is the Motion-Controlled Light (MCL):
- Auto-on: The light is turned on when motion is detected.
- Auto-off: The light is turned off when no motion is detected for several minutes.
The only entities required are:
- a
light
orswitch
- a
binary_sensor
that detects motion
However, in practice, the first deficiency one experiences is the light might turn off while the room is still occupied. You might be sitting quietly and, after a few minutes of no detected motion, the light automatically turns off (leaving you in a dark room and unimpressed).
To prevent this undesirable behavior you can use additional methods of presence detection or by simply commanding the system to (temporarily) disable the MCL feature (i.e. manual override). The question is: “How do you make the manual override command convenient?”
An input_boolean
can be used to enable/disable the MCL feature. However, in practice, it may be inconvenient to access the Lovelace UI to turn off the input_boolean
(even if you have your phone handy). Perhaps the most convenient would be to tell your favorite voice assistant something like “Disable automatic living room light.” This would require creating a custom skill to enable/disable the input_boolean
(and is something I plan to try in the future).
An alternative method is to use the light switch itself. If the light is controlled by a physical switch (such as a wall-mounted switch), it can be used to turn the input_boolean
on or off. It works like this:
- When you enter the room, and the light is automatically turned on, turn the light switch off then back on. This quick toggling action serves to turn off the
input_boolean
. - To re-enable the MCL feature, simply repeat the action: turn the light switch off then back on. This will serve to turn on the
input_boolean
. - To summarize, the action of turning the light switch off then back on serves to toggle the state of the
input_boolean
.
NOTE:
Some switches can transmit events when they are double-tapped (and the event can be used to enable/disable the MCL feature). The approach I’m describing assumes your switch either doesn’t have this capability or you are already using it for another purpose.
Lastly, an automation is used to turn the input_boolean
back on after it has been off for an hour. This ensures the MCL feature is restored in case you disabled it but forgot to re-enable it.
What is required
light
orswitch
entitybinary_sensor
that detects motioninput_boolean
to enable/disable the MCL featuretimer
to help implement the manual override feature- two automations to implement the MCL feature
- two automations to implement the manual override feature
The following examples use:
light.kitchen
binary_sensor.kitchen
You will need to create a 3-second timer
and an input_boolean
.
timer:
kitchen_mcl:
duration: '00:00:03'
input_boolean:
kitchen_mcl:
name: Motion-Controlled Kitchen Light
initial: true
The first two automations simply create a Motion-Controlled kitchen light. The light is turned on by motion and turned off after several minutes of no motion. If you turn off the input_boolean
, the MCL feature is disabled.
# -------------------------------------------------------------------
# Automations for a Motion-Controlled kitchen light
# Turn on light when binary_sensor detects motion
# only if light is currently off and input_boolean is on
- alias: 'Kitchen light Auto-on'
trigger:
platform: state
entity_id: binary_sensor.kitchen
from: 'off'
to: 'on'
condition:
- condition: state
entity_id: light.kitchen
state: 'off'
- condition: template
value_template: "{{ is_state('input_boolean.kitchen_mcl', 'on') }}"
action:
service: light.turn_on
entity_id: light.kitchen
# Turn off light when binary_sensor detects no motion for 3 minutes
# only if input_boolean is on
- alias: 'Kitchen light Auto-off'
trigger:
platform: state
entity_id: binary_sensor.kitchen
to: 'off'
for: '00:03:00'
condition:
- condition: template
value_template: "{{ is_state('input_boolean.kitchen_mcl', 'on') }}"
action:
service: light.turn_off
entity_id: light.kitchen
The next two automations implement the manual override feature (i.e. ability to toggle the input_boolean
by turning the light switch off and then back on).
# -------------------------------------------------------------------
# Automations to allow for manually disabling the Motion-Controlled Light feature
# by simply turning the light switch off then back on within 3 seconds
# Start timer when light is turned off
- alias: 'Kitchen light turned off'
trigger:
platform: state
entity_id: light.kitchen
from: 'on'
to: 'off'
action:
service: timer.start
entity_id: timer.kitchen_mcl
# Toggle input_boolean when light is turned on
# only if timer is active
- alias: 'Kitchen light turned on'
trigger:
platform: state
entity_id: light.kitchen
from: 'off'
to: 'on'
condition:
- condition: template
value_template: "{{ is_state('timer.kitchen_mcl', 'active') }}"
action:
service: input_boolean.toggle
entity_id: input_boolean.kitchen_mcl
There is an optional automation to re-enable the MCL feature after it has been off for an hour.
# -------------------------------------------------------------------
# OPTIONAL automation to re-enable the input_boolean after 1 hour
- alias: 'Kitchen MCL Re-Enable'
trigger:
platform: state
entity_id: input_boolean.kitchen_mcl
to: 'off'
for: '01:00:00'
action:
service: input_boolean.turn_on
entity_id: input_boolean.kitchen_mcl
Yet another optional automation announces the state of the MCL feature.
# -------------------------------------------------------------------
# OPTIONAL automation to announce enabling/disabling of the input_boolean
- alias: 'Kitchen MCL Announce State'
trigger:
platform: state
entity_id: input_boolean.kitchen_mcl
action:
service: tts.amazon_polly_say
data_template:
entity_id: media_player.kitchen
message: >
{% set mode = 'automatic' if trigger.to_state.state == 'on' else 'manual' %}
<speak> Kitchen light is now in {{mode}} mode. </speak>
One useful enhancement is to constrain the MCL feature to work only between sunset and sunrise. You can do this by adding conditions to the first two automations.
NOTE:
If you would prefer to have the manual override feature used exclusively for disabling MCL, and not to toggle MCL’s state, then make the following change. In
automation.kitchen_light_turned_on
replace:
input_boolean.toggle
with:
input_boolean.turn_off
This will prevent the light switch from being used to re-enable MCL. You will have to use
input_boolean
directly to re-enable MCL or wait an hour for the auto-re-enable automation to do it for you.