Door Sensor - Turn on/off Light

Please keep me posted on this! This blueprint is perfect for some of my integrations (Specifically, when I open the back door it turns on the outside lights for the dog, lol) – It works great, but the only thing I could think that would make it even better is to have the ability to have it only work between sunset\sunrise.

Simply add to the conditions:

  condition:
     - "{{ is_state('sun.sun', 'below_horizon') }}"
2 Likes

Thanks so much. I’m new here

Yes, if you do want to know how? Let me know…

Great blueprint! Simple and effective.

I’ve modified it a bit to include:

  • An open delay
  • A close delay
  • Can choose multiple entities to toggle
  • Can select entities from multiple domains, including light, switch, cover and fan
blueprint:
  name: Door Sensor-activated Entities
  description: Turn entities on when a door is opened, off when a door is closed, with optional delays
  source_url: https://raw.githubusercontent.com/nwithan8/configs/main/home_assistant/blueprints/automations/toggle_with_door.yaml
  domain: automation
  input:
    door_entity:
      name: Door Sensor
      description: The door sensor that tracks if the door is opened or closed
      selector:
        entity:
          domain: binary_sensor
    target_entities:
      name: Target entity
      description: The targeted device(s) that will toggle when the door is opened or closed. Can be a `light`, `switch`, `cover` or `fan`.
      selector:
        target:
          entity:
            domain:
              - light
              - switch
              - fan
              - cover
    door_opened_wait:
      name: Open delay
      description: Time to wait after the door is opened to turn on the target entities
      default: 0
      selector:
        number:
          min: 0
          max: 3600
          unit_of_measurement: seconds
    door_closed_wait:
      name: Close delay
      description: Time to wait after the door is closed to turn off the target entities
      default: 0
      selector:
        number:
          min: 0
          max: 3600
          unit_of_measurement: seconds

mode: single
max_exceeded: silent

trigger:
  platform: state
  entity_id: !input door_entity

action:
  - if:
      - condition: state
        state: "off"
        entity_id: !input door_entity
    then:
      - delay: !input door_closed_wait
      - service: homeassistant.turn_off
        data: {}
        target: !input target_entities
    else:
      - delay: !input door_opened_wait
      - service: homeassistant.turn_on
        data: {}
        target: !input target_entities

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Example of configuration:

1 Like

I’d recommend that you make an explicit case for state = on to handle the turning on the light as there are other states that the sensor may take to include “unavailable” that will result in the light turning on when you don’t expect it. I’ve converted the iff…then…else to a choose with no default action. This will perform the same activity as the original and ignore any outlier states for the sensor.

action:
  - choose:
      - conditions:
          - condition: state
            entity_id: !input door_entity
            state: "off"
        sequence:
          - delay: !input door_closed_wait
          - service: homeassistant.turn_off
            data: {}
            target: !input target_entities

      - conditions:
          - condition: state
            entity_id: !input door_entity
            state: "on"
        sequence:
          - delay: !input door_opened_wait
          - service: homeassistant.turn_on
            data: {}
            target: !input target_entities
1 Like

Good idea!

this was really useful,

I’ve made some further tweaks to your tweaks…

Summary:

  • Allowed it to have multiple binary sensor entities to trigger from
  • door open on ANY of the entities (I’m using the front door contact sensor, my doorbell press, or human detection from camera)
  • door closed on ALL of the entities (so it doesn’t turn off when the door is open but the doorbell has stopped ringing)
  • added an input for sun elevation which operates as a condition for the door open actions (useful for turning the lights on at a preset amount before/after dawn/dusk).
  • set behaviour so the lights will always turn off, regardless of sun position

I’ve not tested it for long but finding it pretty versatile now.

blueprint:
  name: Door Sensor-activated Entities
  description:
    Turn entities on when a door is opened, off when a door is closed,
    with optional delays
  #source_url: https://github.com/nwithan8/configs/blob/4f4c7bf885475a0ac3365c9221a48efa199d205f/home_assistant/blueprints/automations/toggle_with_door.yaml I've tweaked from this
  domain: automation
  input:
    door_entity:
      name: Door Sensor
      description: The door sensor that tracks if the door is opened or closed
      selector:
        entity:
          domain:
            - binary_sensor
          multiple: true
    target_entities:
      name: Target entity
      description:
        The targeted device(s) that will toggle when the door is opened
        or closed. Can be a `light`, `switch`, `cover` or `fan`.
      selector:
        target:
          entity:
            - domain:
                - light
                - switch
                - fan
                - cover
    door_opened_wait:
      name: Open delay
      description: Time to wait after the door is opened to turn on the target entities
      default: 0
      selector:
        number:
          min: 0.0
          max: 3600.0
          unit_of_measurement: seconds
          mode: slider
          step: 1.0
    door_closed_wait:
      name: Close delay
      description: Time to wait after the door is closed to turn off the target entities
      default: 0
      selector:
        number:
          min: 0.0
          max: 3600.0
          unit_of_measurement: seconds
          mode: slider
          step: 1.0
    sun_elevation_threshold:
      name: Sun Elevation Threshold
      description: Threshold for the sun's elevation to trigger the automation
      default: 0
      selector:
        number:
          min: -90
          max: 90
          unit_of_measurement: degrees
          mode: slider
          step: 1
mode: single
max_exceeded: silent
trigger:
  platform: state
  entity_id: !input door_entity
action:
  - if:
      - condition: state
        state: "off"
        entity_id: !input door_entity
    then:
      - delay: !input door_closed_wait
      - condition: state
        state: "off"
        entity_id: !input door_entity
      - service: homeassistant.turn_off
        data: {}
        target: !input target_entities
    else:
      - condition: numeric_state
        entity_id: sun.sun
        attribute: elevation
        below: !input sun_elevation_threshold
      - delay: !input door_opened_wait
      - service: homeassistant.turn_on
        data: {}
        target: !input target_entities

So, it turns out I built an automation that’s more generic than I originally intended.

While this was supposed to be used to sync a room light with a door, it technically can control a light, cover or fan based on a binary sensor.

So, here’s me using this “door sensor” blueprint to turn on/off the backlight of my ESP32-S3-Box-3 when its voice assistant satellite wake word is triggered.

I find it’s much more practical to allow for a generic binary_sensor without a device_class specified, as well as allow for other entity types / domains aside from just lights.

This blueprint seems to work well for me in a number of situations, including my den closet door lighting control automation:

1 Like

i have this blueprint working with the hue outdoor floodlight, and it does turn then light on when the door is open and turns off again once door is closed, however i also had a light sensor next to the light so is it possible that the blueprint could check the current light level and only turn on if it is dark enough for the light to be needed?

1 Like