Door Sensor - Turn on/off Light

Hi there!

I’ve tried your blueprint, and it looks like exactly what I need, and makes it a lot easier to create a door automation than doing it manually, which is great, thanks! :+1:

I’m a bit new to Home Assistant though, so forgive me if what I’m about to write is dumb.

I tried using the timeout, and found out that the following scenario does not work as expected:

  • Use a short delay to experiment (say 3 seconds)
  • open the door (light turns on as expected)
  • close the door (light has not turned off yet, waiting for the delay to expire)
  • start counting seconds
  • before the delay expires, open the door again (at this point, light is still on)
  • when the delay expires, even though the door is open, lights turn off.

I would expect the lights to stay on, until I close the door again and the wait time expires.

I believe setting the automation to mode: restart would fix it: if the trigger happens while the automation is running, then restart it (ie. turn the light on again then wait for the closed trigger again).

(Here’s documentation on Automation Modes.)


Update: it also looks like you can use the “wait for trigger” action’s for parameter to set up the wait time, so you don’t have to add another action - delay: !input door_closed_wait.
Here’s how I modified your blueprint to fit my needs:

blueprint:
  name: Door Sensor Light
  description: Turn a light when a door is opened
  domain: automation
  input:
    doorsensor_entity:
      name: Door Sensor
      selector:
        entity:
          domain: binary_sensor
    light_target:
      name: Light
      selector:
        target:
          entity:
            domain: light
    door_closed_wait:
      name: Wait time
      description: Time to leave the light on after door is closed
      default: 120
      selector:
        number:
          min: 0
          max: 3600
          unit_of_measurement: seconds

mode: restart

trigger:
  platform: state
  entity_id: !input doorsensor_entity
  from: "off"
  to: "on"
condition: []

action:
  - service: light.turn_on
    target: !input light_target
  - wait_for_trigger:
      platform: state
      entity_id: !input doorsensor_entity
      from: "on"
      to: "off"
      for: !input door_closed_wait
  - service: light.turn_off
    target: !input light_target

Hello, I am sorry for the late Reply. I am a bit on and off on the Community.

Great work modifying the blueprint to fit your needs! We are all new at some point. This is actually My first or second blueprint, learning from existing ones

1 Like

recommend “restart” instead of “single”

1 Like

Try this, by default it does not work with switches.

Is it possible to implement the brightness of the light?

I have it like this now:

This one i want brightness 50%

alias: New Automation
description: ''
condition:
  - condition: time
    after: '20:15'
    before: '23:15'
use_blueprint:
  path: bergstrom/door-sensor-turn-on-off-light.yaml
  input:
    doorsensor_entity: binary_sensor.keuken
    light_target:
      area_id: keuken
    door_closed_wait: 60

This one i want brightness 25%

alias: New Automation
description: ''
condition:
  - condition: time
    after: '23:15'
    before: '05:15'
use_blueprint:
  path: bergstrom/door-sensor-turn-on-off-light.yaml
  input:
    doorsensor_entity: binary_sensor.keuken
    light_target:
      area_id: keuken
    door_closed_wait: 60

Is it possible to use condition sun AND time? I would like to have “after sunset” “until 5 o’clock”

After a bit of research i wonder if this might work:

condition:
  condition: or
  conditions:
    - condition: state
      entiti_id: sun.sun
      state: 'below horizon'
    - condition: time
      before: "05:00:00"

I created my own automation that will turn on the lights when the front door opens after sunset. I also added a rule so that it doesn’t do it for 30 seconds after the lights are turned off (ie. when turning off the lights before leaving the house)

trigger:
  - platform: state
    entity_id: binary_sensor.z_wave_door_sensor
    from: 'off'
    to: 'on'
condition:
  - condition: sun
    after: sunset
  - condition: state
    entity_id: light.living_room_lights
    state: 'off'
    for:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
action:
  - service: light.turn_on
    target:
      entity_id: light.living_room_lights
    data:
      brightness_pct: 100
mode: single

Hy, i tried the following butt all didn’t work:

condition:
  condition: or
  conditions:
    - condition: state
      entiti_id: sun.sun
      state: 'below horizon'
    - condition: time
      before: "05:00:00"
condition:
  condition: or
  conditions:
    - condition: state
      entiti_id: sun.sun
      state: 'below horizon'
    - condition: time
      after: "00:00:00"
      before: "05:00:00"
condition:
  condition: and
  conditions:
    - condition: state
      entiti_id: sun.sun
      state: 'below horizon'
    - condition: time
      before: "05:00:00"

Can somebody pls help find out how to manage this?
I want it like until “two hours before sunset”

Try using the Sun condition and the before sunset offset:

condition:
  - condition: sun
    before: sunset
    before_offset: '02:00:00'

Must be more like:

condition:
  - condition: sun
    after: sunset
    before: sunrise
    before_offset: '02:00:00'

Right?

I will try this! Thx!

Play around with it and see how it works. I was just going off your last comment, since you said “two hours before sunset”

Sorry i meant before sunrise. :blush:

Can you help me with formating?

This:

condition:
  - condition: sun
    after: sunset
    before: sunrise
    before_offset: '02:00:00'

or this:

condition:
  - condition: sun
    after: sunset
 - condition: sun
    before: sunrise
    before_offset: '02:00:00'
1 Like

Hy, unfortunately i couldn’t make it work.
i tried.

condition:
  - condition: sun
    after: sunset
    before: sunrise
    before_offset: '02:00:00'
condition:
  - condition: sun
    after: sunset
    before: sunrise
    before_offset: '"02:00:00"
condition:
  - condition: sun
    after: sunset
    before: sunrise
    before_offset: '02:00:00'
condition:
  condition: and
  conditions:
    - condition: sun
      after: sunset
    - before: sunrise
      before_offset: "02:00:00"
condition:
  condition: and
  conditions:
    - condition: sun
      after: sunset
    - condition: sun
      before: sunrise
      before_offset: "-02:00:00"

Can somebody pls help me with the correct way to do this. Im frustrated.

Maybe post your whole automation and explain what you’re trying to do. There could be an issue somewhere else in it that’s preventing it from working.

I want to switch on the light on the balkony and leave it on for 5-6 Mins by opening a door.
All that works great unless i put in that special condition.
My goal is that the light just gets switched on between sunset and 5 o’clock in the morning.
Switching the light on on condition sun - below horizon works too.

Its just something about that “between sunset and 5 o’clock” i guess.

Here’s the entire code.

blueprint:
  name: Door Sensor-activated Light
  description: Turn on a light when door is opened.
  domain: automation
  input:
    doorsensor_entity:
      name: Door Sensor
      selector:
        entity:
          domain: binary_sensor
    switch_target:
      name: Schalter
      selector:
        target:
          entity:
            domain: switch
    door_closed_wait:
      name: Wait time
      description: Time to leave the light on after door is closed
      default: 360
      selector:
        number:
          min: 0.0
          max: 1000.0
          unit_of_measurement: seconds
          step: 10.0
          mode: slider
  source_url: https://community.home-assistant.io/t/door-sensor-turn-on-off-light/255657
mode: single
max_exceeded: silent
trigger:
  platform: state
  entity_id: !input 'doorsensor_entity'
  from: 'off'
  to: 'on'
  
condition:
  condition: and
  conditions:
    - condition: sun
      after: sunset
    - condition: sun
      before: sunrise
      before_offset: "-02:00:00"
    
action:
- service: switch.turn_on
  target: !input 'switch_target'
- wait_for_trigger:
    platform: state
    entity_id: !input 'doorsensor_entity'
    from: 'on'
    to: 'off'
- delay: !input 'door_closed_wait'
- service: switch.turn_off
  target: !input 'switch_target'

Ahh okay I see what you’re trying to do now. I’m not good enough with the coding to do that. But right now, your code is not telling it to turn off the light. The condition is just saying turn the light on if it’s between these times only.

My solution would be to create a second automation to turn the lights off. So a new trigger for when it’s 5AM or 2 hours before sunrise with the action being to turn the lights off.

Hy, thats funny because it works as it should without the condition.

It turnes on by opening the door and stays on for the configured time after closing the door.

But without the condition it does so all the day.

It would work with the condition “sun below horizon” but thats not precisely what i want.

Any other hints?

Thx and greetings Max

I have another idea, maybe the sun in the wait_for_trigger instead of in the condition?

action:
  - wait_for_trigger:
      - platform: sun
        event: sunrise
        offset: '-02:00:00'
  - service: switch.turn_off
    target: !input 'switch_target'

Anyone have an idea?

I’m currently working on an extend to this blueprint (I will probably do some more changes, but feel free to try) that will enable open and closed actions. With those actions you can customize what the door sensor will do not only for turning a light on, with it you can also like turn a power plug or something like that. Or use conditions as actions for timed events.

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