Motion detected -> lights on. no motion within 10mins, turn lights off

i would love to have a light to turn on automatically whenever motion is detected. if no motion is detected within 10mins, turn light off. if motion is detected, then add another 10mins. is this even possible???

this is what i have in the house so far:
binary_sensor.motion
no motion, state= off
motion detected, state= on

light is connected to the Belkin WeMo switch, switch.light.
switch on, state= on
switch off, state= off

im using Home Assistant 0.60.0.

please help!
thanks

If your binary sensor is already a template binary sensor, you can try using the delay_off option, otherwise, just create another template binary sensor with delay_off based on your binary_sensor.motion. Automation can be as simple as: sensor on -> light on, sensor off -> light off.
Here is the example of delay_off:
template binary sensor - delay_off

1 Like

Here’s my automation using Xiaomi Aqara motion sensors and Hue lights:

# Motion detector - study lights
- alias: Study light on - motion
  id: 'study_motion'
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d0001e573dd
    to: 'on'
  condition:
    condition: state
    entity_id: group.all_devices
    state: 'home'
  action:
    service: light.turn_on
    entity_id: light.study

- alias: Study light off - no motion
  id: 'study_nomotion'
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d0001e573dd
    to: 'off'
    for:
      minutes: 5
  action:
    service: light.turn_off
    entity_id: light.study

My automation

automation kitchen_light_movement:
  alias: Turn on off kitchen lights when there is movement
  trigger:
    - platform: state
      entity_id: binary_sensor.Family_Room_Motion
      to: 'on'
    - platform: state
      entity_id: binary_sensor.Family_Room_Motion
      to: 'off'
      for:
        minutes: 10
  action:
    - service_template: 'light.turn_{{trigger.to_state.state}}'
      entity_id: light.kitchen_light
3 Likes

Probably that’s my luck, using the for in the state platform has not been reliable for me, or I just did not do it right. With to: ‘off’ as trigger, does that mean change state to ‘off’ or has a value ‘off’?
Also, with these different ‘wait’ methods, can they all execute the wait action even if HA restarts during the wait time?
Sorry for side-tracking.

If you don’t want to rely on the state, you can use the timer:

automation:
  - alias: Light on if motion
    trigger:
      - platform: state
        entity_id: binary_sensor.motion
        to: 'on'
    action:
      - service: timer.start
        entity_id: timer.timer_lamp
      - service: switch.turn_on
        entity_id: switch.light
  - alias: Turn off lights at end of timer
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.timer_lamp
    action:
      service: switch.turn_off
      entity_id: switch.light

timer:
  timer_lamp:
    duration: '00:10:00'

A to ‘off’ trigger mean that the state change from another value to ‘off’. It won’t trigger if it just stays in off state.

2 Likes

I use the timer method. The PIR detects motion, turns on the light, starts the timer. As long as their is motion at the PIR, the timer gets restarted. After (in my case 2 minutes) there is no motion, the lights shuts off. Works great.
Automation file

- alias: Turn on driveway floodlight when there is movement
  trigger:
    - platform: state
      entity_id: binary_sensor.drivewaypir
      to: 'on'
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
  action:
    - service: homeassistant.turn_on
      entity_id: script.timed_driveway

Script file

timed_driveway:
  alias: "Turn on floodlight and set timer"
  sequence:
    # Cancel ev. old timers
    - service: script.turn_off
      data:
         entity_id: script.driveway_off
    - service: switch.turn_on
      data:
        entity_id: switch.ge_unknown_type4952_id3033_switch_13_0
    # Set new timer
    - service: script.turn_on
      data:
        entity_id: script.driveway_off

driveway_off:
  alias: "Turn off floodlight after 2 minutes"
  sequence:
    - delay:
        minutes: 2
    - service: switch.turn_off
      data:
        entity_id: switch.ge_unknown_type4952_id3033_switch_13_0
2 Likes

That’s exactly not the timer, but the previous wait function, which could be replaced by:

automation:
  - alias: Turn on driveway floodlight when there is movement
    trigger:
      - platform: state
        entity_id: binary_sensor.drivewaypir
        to: 'on'
    condition:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
    action:
      - service: timer.start
        entity_id: timer.timer_driveway
      - service: switch.turn_on
        entity_id: switch.ge_unknown_type4952_id3033_switch_13_0
  - alias: Turn off floodlight after timer
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.timer_driveway
    action:
      service: switch.turn_off
      entity_id: switch.ge_unknown_type4952_id3033_switch_13_0

timer:
  timer_driveway:
    duration: '00:02:00'
2 Likes

Understood. I think your method is a little cleaner than mine, but I’ll probably leave mine as is. Hopefully the info was helpful to the original poster.

For simple ON/OFF it doesn’t make a big difference. It become useful when you want to pause/cancel/extend or display the remaining duration of a timer :slight_smile:

I often deactivate the turn off automation so that the light stay on. With script it’s not that convenient (the automation is shown in front end)

1 Like

how to mark manual power on so pir wont turn off the ligth ?

thank you soooo much everyone for the codes!
i cant wait to get home to try them all. will definitely let you know which works best for my situation.

1 Like

How do you perform manual power on? What if the sensor already turn on the light?
What you can do, is to turn off the “turn light off” automation.

But better would be to figure out a way to detect that someone is still present in the room.

1 Like

How did yo get you xiaomi to show as On / Off as mine show as Active & inactive ??

On/off are just the default states for my sensors. If yours are different you can just use

trigger:
    platform: state
    entity_id: your_sensor
    to: 'active'

yeah my defaults as active and inactive, i wanted to group them so i could say if group “active” then blah, but as a group it says unknown. argh well here’s mine as they currently stand

##########################################################################
################# Bathroom Motion Lights On ##################################
##########################################################################

- alias: "Motion Bathroom Lights On"
  trigger:
   platform: state
   entity_id: 'sensor.smartthings_xiaomi_bathroom_motion_sensor_detection'
   to: 'active'
  condition:
   condition: and
   conditions:
   - condition: state
     entity_id: light.bathroom
     state: 'off'
   - condition: or
     conditions:
      - condition: sun
        after: sunset
        after_offset: "-2:00:00"
      - condition: state
        entity_id: sun.sun
        state: below_horizon
      - condition: numeric_state
        entity_id: sensor.smartthings_xiaomi_bathroom_motion_sensor_lux
        below: 13
 action:
  - service: light.turn_on
    entity_id: light.bathroom

#########################################################################

- alias: "Motion Bathroom Lights Off"
  trigger:
   platform: state
   entity_id: 'sensor.smartthings_xiaomi_bathroom_motion_sensor_detection'
   to: 'inactive'
   for: 00:00:15
  condition:
   condition: state
   entity_id: light.bathroom
   state: 'on'
  action:
   service: light.turn_off
   entity_id: light.bathroomindent preformatted text by 4 spaces

this is something I’ve been meaning to find out… would you be willing to share the code for these turn on/off switches?
Thanks!
Marius

You mean to turn off the “turn off automation” from the frontend?

I just made a group that show the lamp controlled by the automation, and the two automation that handle turning ON and turning OFF:

office_light_viewing_group:
  control: hidden
  entities:
    - switch.table_lamp
    - switch.column_lamp
    - automation.turn_on_office_lights_when_there_is_movement
    - automation.turn_off_office_lights_at_end_of_timer

Here you can see the complete automation:

Click to see the complete automation and a screenshot of the frontend group
automation:
  - alias: Turn on office lights when there is movement
    trigger:
      - platform: mqtt
        topic: home/433toMQTT
        payload: 16026762
      - platform: mqtt
        topic: homesense/433toMQTT
        payload: 16026762
    condition:
      condition: and
      conditions:
        - condition: or
          conditions:
          - condition: sun
            after: sunset
            after_offset: "-0:30:00"
          - condition: sun
            before: sunrise
            before_offset: "0:30:00"
        - condition: or
          conditions:
          - condition: state
            entity_id: device_tracker.michaela
            state: 'home'
          - condition: state
            entity_id: device_tracker.me
            state: 'home'
    action:
      - service: timer.start
        entity_id: timer.timer_office
      - service: switch.turn_on
        entity_id: group.office_light
  - alias: Turn off office lights at end of timer
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.timer_office
    action:
      service: switch.turn_off
      entity_id: group.office_light

timer:
  timer_office:
    duration: '00:03:00'

And the customize part:

customize:
  automation.turn_on_office_lights_when_there_is_movement:
    friendly_name: Turn on automaticaly
    icon: mdi:lightbulb-on-outline
  automation.turn_off_office_lights_at_end_of_timer:
    friendly_name: Turn off automaticaly
    icon: mdi:lightbulb-outline

To achieve this entry:

And you can simply call this service, from another automation, to avoid having your light turning themselves off:

  - service: automation.turn_off
    entity_id: automation.turn_off_office_lights_at_end_of_timer
1 Like

a yes, the power of groups. So easy to forget that. thanks!

i just tried your method and it worked flawlessly! first i added this section of your code to my automations.yaml file

- alias: Light on if motion
  trigger:
    - platform: state
      entity_id: binary_sensor.motion
      to: 'on'
  action:
    - service: timer.start
      entity_id: timer.timer_lamp
    - service: switch.turn_on
      entity_id: switch.light
- alias: Turn off lights at end of timer
  trigger:
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.timer_lamp
  action:
    service: switch.turn_off
    entity_id: switch.light

then i added this section to my configuration.yaml

timer:
  timer_lamp:
    duration: '00:10:00'

restarted HA and it worked. amazing!
at first i was skeptical, because everything in my automations.yaml file starts out with - action
while yours started out with - alias
but as long as it works, it’s great!

many thanks!

1 Like