Automatically turn on a light when a door is opened

This is a really simple automation that turns on a light when a door is opened, and then turns it off again two minutes after the door is closed.

I use this automation to turn on a light in my attic space when the attic door is opened. The reason for the two-minute delay when the door is closed is to prevent the door blowing shut and then locking me in the dark attic with all those spiders. I also fade the lights in the attic out over 30 seconds, which is useful if I've not realised that the door has closed as I will see the lights slowly getting dimmer. This will give me enough time to get back to the door and open it to trigger the lights to turn on again.

You can easily adjust this automation to turn off the light immediately when you close the door if that makes more sense for your use case.

To make this automation work you'll need the following:

  1. A working Home Assistant installation

  2. Some Contact Sensors paired with Home Assistant

  3. Some Lights paired with Home Assistant

Trigger

This automation is triggered when the door contact sensor goes from closed to open. I use a state trigger type to check if the state of the sensor goes from off to on.

Trigger showing contact sensor changing state from off to on.

Trigger showing contact sensor changing state from off to on.

Contact Sensors are really confusing, their state is set to on when the door is opened. You can validate this yourself by opening one of the doors and then looking at that contact sensor in the developer tools.

Sonoff Contact Sensor for an open door showing on state, but contact is false.

Sonoff Contact Sensor for an open door showing on state, but contact is false.

Here is the trigger automation block in YAML:

platform: state
entity_id: binary_sensor.attic_door_contact_sensor_contact
from: 'off'
to: 'on'

Conditions

This automation has no conditions. We want the attic light to come on any time the door is opened, because it's always dark up there.

Action

This automation has multiple stages in it. First, it fades on the attic light over 1 second to 100% brightness using the light.turn_on service.

Automation Action showing a call to the light.turn_on service which turns on the attic lights.

It uses a wait for trigger to wait until the door contact sensor goes from on to off for two minutes. This means it is waiting until the attic door has gone from Open to Closed for at least two minutes.

Screenshot of Wait For Trigger in the action of the automation, waiting for the attic door contact sensor to register as closed for 2 minutes.

Once the door has been closed for two minutes, the automation will continue on to call the light.turn off service which fades the light out over 30 seconds.

Full Automation in YAML

alias: 'Light: Turn on Attic Light when door is opened'
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.attic_door_contact_sensor_contact
    from: 'off'
    to: 'on'
condition: []
action:
  - service: light.turn_on
    target:
      entity_id: light.attic_light
    data:
      brightness_pct: 100
      transition: 1
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.attic_door_contact_sensor_contact
        from: 'on'
        to: 'off'
        for:
          hours: 0
          minutes: 2
          seconds: 0
          milliseconds: 0
  - service: light.turn_off
    data:
      transition: 30
    target:
      entity_id: light.attic
mode: single
Previous
Previous

Get notified when your Washing Machine has finished it's wash cycle using Smart Switches and Contact Sensors

Next
Next

Get a push notification if a door or window is left open when you leave the house