How to set this particular door light automation using timer features

Hi All, need your help to setup door automation using timer features to off all light which I can’t figure it out.

Lets put sensor name as below

  1. binary_sensor.door_sensor_opening
  2. timer.30 (30secs)
  3. binary_sensor.motion_sensor
  4. script.off_all_light

Automation that need to run as below:
when door open, there will be 30sec timer running, if no motion detect after end of 30sec timer then run “script.off_all_light”. But if motion detected during 30sec then cancel the timer.

I not sure if I need to put this “But if motion detected during 30sec then cancel the timer” because it might made the automation never run or you guy have any other method for this matter?

I have automation in kitchen that use timer helper. Maybe this can help you out with your automation.

alias: Kitchen light
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.kitchen_motion_sensor_occupancy
    from: "off"
    to: "on"
    id: Motion detected
  - platform: state
    entity_id:
      - binary_sensor.kitchen_motion_sensor_occupancy
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 0
    id: Motion is off
    from: "on"
  - platform: state
    entity_id:
      - timer.kitchen_light_helper
    from: active
    to: idle
    id: Helper
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Motion detected
          - type: is_illuminance
            condition: device
            device_id: 4ffbaa70f931ad2540d3055c4212e7dc
            entity_id: sensor.kitchen_motion_sensor_illuminance_lux
            domain: sensor
            below: 50
        sequence:
          - service: timer.cancel
            data: {}
            target:
              entity_id: timer.kitchen_light_helper
          - service: light.turn_on
            data: {}
            target:
              device_id: c121281597ab02ba2bde7c486d102be1
      - conditions:
          - condition: trigger
            id: Motion is off
        sequence:
          - service: timer.start
            data: {}
            target:
              entity_id: timer.kitchen_light_helper
      - conditions:
          - condition: trigger
            id: Helper
        sequence:
          - service: light.turn_off
            data: {}
            target:
              device_id: c121281597ab02ba2bde7c486d102be1
mode: single
max_exceeded: silent
1 Like

This implements what you’ve asked for, assuming the door remains open for 30s. Note that I didn’t use your timer: see rule 8. Triggers after the door has been open for 30s, checks if there’s been no motion in that 30s.

trigger:
  - platform: state
    entity_id: binary_sensor.door_sensor_opening
    to: 'on'    # whichever state means "open"
    for: "00:00:30"
condition:
  - condition: state
    entity_id: binary_sensor.motion_sensor
    state: 'off'
    for: "00:00:30"
action:
  - service: script.turn_on
    target:
      entity_id: script.off_all_light

If there has been motion, the light will not turn off — but that is what you asked for. If you want something different, please explain in a structured way.

In particular, how do you open the door without triggering the motion sensor?

1 Like

sorry @Troon didn’t notice the rule, I should explain the result that I need.
here is result that I need,
Once I open the door, I would like to off all light provided no one in the house trigger the motion sensor within 30secs. But if someone is in the house & moving within 30secs, then the light won’t off. Problem now is if someone walk through the motion sensor & within 30sec open the door, the automation might not run as the motion sensor required 30sec to release from “detect to clear”. Unless I increase the timing to higher?

Thanks @ddaniel , I will look into it.

Well you can try using timer helper. In my scenario I use time helper to turn off light in kitchen. This is working far more better than using state for function for a few reasons.
You can change timer duration without changing automation.
Motion sensors have default value how long will they consider that motion is on, usually 90 seconds. You can change this is z2m config if using zigbee.
Timer will run only if no motion is detected. If motion sensor stop detecting motion it will start timer. But if motion is detected while timer is running, than cancel timer and start it again when motion is not detected.

yes, I do use timer on some of my automation, it work great.

Two automations for simplicity? I’ve not called your timer timer.30 as numeric entity IDs are a pain.

alias: Door timer manager
trigger:
  - platform: state
    entity_id: binary_sensor.door_sensor_opening
    to: 'on'
    id: "start"
  - platform: state
    entity_id: binary_sensor.motion_sensor
    to: 'on'
    id: "cancel"
action:
  - service: timer.{{ trigger.id }}
    target:
      entity_id: timer.door_timer

alias: Lights off
trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.door_timer
action:
  - service: script.turn_on
    target:
      entity_id: script.off_all_light

@Troon that automation look interesting, will try it & let you know the result.

@Troon look like the automation doesn’t always work, it will work for 1st time. Once timer start, 2nd time trigger on motion won’t cancel the timer. By the way, my binary sensor is the group of motion sensor that will on once one the motion sensor is on.

By the way, possible to combine both automation into one automation as it is consider under one control.

alias: OFF ALL light 
description: ""
trigger:
  - type: opened
    platform: device
    device_id: xx
    entity_id: binary_sensor.door_sensor_opening
    domain: binary_sensor
    id: door open
  - platform: event
    event_type: timer.finished
    id: ALL light OFF timer END
    event_data:
      entity_id: timer.off_all_light_timer
  - platform: state
    entity_id:
      - binary_sensor.motion_sensor
    to: "off"
    id: Confirm ALL sensor OFF
condition: []
action:
  - if:
      - condition: trigger
        id: door open
    then:
      - service: timer.start
        data: {}
        target:
          entity_id: timer.off_all_light_timer
  - if:
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.motion_sensor
            state: "off"
          - condition: trigger
            id: ALL light OFF timer END
    then:
      - service: script.off_all_light
        data: {}
      
mode: restart

I using different method as above although this one I need to set the timer time higher but still not perfect as might accidently off all light.