This is what I use for doors with a 2 minute timer (This can be done in different ways, but the behavior is how you describe):
These next 3 peices of yaml will turn on a light for 2 minutes after a door is opened. Each time the door opens, it extends the timer by 2 minutes. So it will only shut the lights off after 2 minutes since the last open.
automation1:
- alias: Foyer light trigger on main door
trigger:
- platform: state
entity_id: sensor.main_door_hindge
to: 'open'
condition:
- condition: state
entity_id: sun.sun
state: "below_horizon"
action:
- service: homeassistant.turn_on
entity_id: script.foyer_door_is_open
Door opening script
alias: Foyer Door is Open
sequence:
# Cancel ev. old timers
- service: script.turn_off
data:
entity_id: script.foyer_light_timer
- service: switch.turn_on
data:
entity_id: switch.foyer_s_switch_7_0
# Set new timer
- service: script.turn_on
data:
entity_id: script.foyer_light_timer
timer script
alias: Foyer Light Timer
sequence:
- delay:
minutes: 2
- service: switch.turn_off
data:
entity_id: switch.foyer_s_switch_7_0
This second automation is similar for motion detection:
automations to turn on lights depending on time:
- alias: Hall Motion Detected During Night
trigger:
- platform: state
entity_id: binary_sensor.hallway_ms_sensor_32_0
to: 'on'
condition:
- condition: time
after: '23:00:00'
before: '07:00:00'
action:
- service: homeassistant.turn_on
entity_id: script.hall_motion_night
- alias: Hall Motion Detected During Day
trigger:
- platform: state
entity_id: binary_sensor.hallway_ms_sensor_32_0
to: 'on'
condition:
- condition: time
after: '07:00:00'
before: '23:00:00'
action:
- service: homeassistant.turn_on
entity_id: script.hall_motion_day
motion timer script
alias: Hall Light Timer
sequence:
- delay:
minutes: 5
- service: light.turn_off
data:
entity_id: light.hall_d_level_10_0
day time lights for motion
alias: Hall Motion Day
sequence:
# Cancel ev. old timers
- service: script.turn_off
data:
entity_id: script.hall_light_timer
- service: light.turn_on
data:
entity_id: light.hall_d_level_10_0
brightness: 255
# Set new timer
- service: script.turn_on
data:
entity_id: script.hall_light_timer
night time motion script
alias: Hall Motion Night
sequence:
# Cancel ev. old timers
- service: script.turn_off
data:
entity_id: script.hall_light_timer
- service: light.turn_on
data:
entity_id: light.hall_d_level_10_0
brightness: 10
#brightness: "{{ 255 * 0.15 | round(0) | int }}"
# Set new timer
- service: script.turn_on
data:
entity_id: script.hall_light_timer
These can be simplified if you want to, otherwise you can use them and just change the entity id’s.