Turn on lights on and keep them on when motion detection

I also found out that switching off lights with one automation that waits for sensor state to be off for xx minutes does not work well: it seems multiple off timers start, each time the motion sensor state changes to off, and the lights start to turn off randomly until all timers have ended. I didn’t notice it at first because the delay time was only 30 seconds, but it gets annoying with a 30 minute delay time.

So, back to using a timer script with the automation to trigger it…

You might want to look into using Appdaemon, It looks difficult but it is not to hard to get going. It solves all these problems! :slight_smile:

hello, I’m new to Home Assistant. I have made an esp8266 MQTT motion sensor. when motion is detected it will be sending MQTT message “Motion Detected” to HA… what I want to do he when HA received Motion detected message I want the light to turn on and keep it on for 3 mins then turn it back off. I have made an automation to turn on the light when motion is detected. someone, please help me can I make it to auto turn off after 3 mins?

You basically just need to scroll up in this thread, and you will find plenty of examples where lights are turned on triggered by motion detected or sun setting, and then turned off after a set amount of time.

Hello @Ashfaaa
This is the way I’m doing it, thanks to @Pilot, which I’m happy with…

- alias: Light off after 90 sec when motion off
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor
      to: 'off'
      for:
        minutes: 1
        seconds: 30
  action:
    - service_template: light.turn_off
      entity_id: group.kitchen
1 Like

Hi @Ashfaaa, someone had the same (arduino sketch) problem.

You can use the automation with timed scripts, or use another sketch.

1 Like

you have any better sketch.? im kinda new to all this

Sorry, no. But everything you need is the linked thread.

1 Like

I know this is an old post but just to let you know I found your example was exactly what I was looking for. Thanks

2 Likes

Hi,
Find below my config sample which allow to turn on the light on motion is on and turn it off after 30s:

a) The script

script_entrance_night_light:
  alias: Night Light at Entrance
  sequence:
  - data:
      brightness_pct: 5
      entity_id: light.yeelight_white_xxxxxx
    service: light.turn_on
  - delay: '30'
  - data:
      entity_id: light.yeelight_white_xxxxxx
    service: light.turn_off

b) Automation:

- alias: Entrance Night Light On
  id: '1516061021603'
  trigger:
  - entity_id: binary_sensor.motion_sensor_xxxx
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  - condition: state
    entity_id: light.yeelight_white_xxxxx
    state: 'off'
  action:
  - data:
      entity_id: script.script_entrance_night_light
    service: script.turn_on

What is the question?

Hi,

Not sure if it helps, but my lights only become motion activated at 9pm. I just wrote an automation to turn my motion automations on at 9 and off at 7am when they become normal lights. Iv hardcoded these times but you could use a input_number to set the time they cone off/on.

I use something similar for my heating:

Let me know if you’d like to see my code

You can control several entities using a comma:

action:
service: homeassistant.turn_on
entity_id: script.timed_lamp, script.second_lamp

Why the script? Can’t you simply use this?:

- alias: Motion Detection - Bathroom Light on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015ab008
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.bathroom
    
- alias: Motion Detection - Bathroom Light off
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015ab008
    to: 'off'
    for: '00:05:00'
  action:
    service: light.turn_off
    entity_id: light.bathroom
1 Like

That’s smart and would help.

Yes please i would like to see the code

Can confirm this works perfectly :+1:

Could you please share your code.

Thanks.

automation motion on at 10:
  alias: Motion - on at 10pm
  initial_state: True
  hide_entity: False
  trigger:
    platform: time
    at: '22:00:00' 
  action:
    service: automation.turn_on
    entity_id: automation.motion_1_on__lights_on,automation.motion_1_off__lights_off

automation motion off at 11:
  alias: Motion - off at  7am
  initial_state: True
  hide_entity: False
  trigger:
    platform: time
    at: '07:00:00' 
  action:
    service: automation.turn_off
    entity_id: automation.motion_1_on__lights_on,automation.motion_1_off__lights_off

I followed someone else’s idea to try and the motion sensor tries to keep turning off unless there is motion, I also have built in time as I want the lights to be dimmer at certain periods. Here’s my automation around it:

- alias: Motion Home Hallway
  trigger:
    - platform: state
      entity_id: binary_sensor.neo_coolcam_battery_powered_pir_sensor_hallway
      from: 'off'
      to: 'on'
  condition:
    - condition: state
      entity_id: light.hallway
      state: 'off'
    - condition: or
      conditions:
        - condition: state
          entity_id: input_select.chris_status_dropdown
          state: 'Home'
        - condition: state
          entity_id: input_select.val_status_dropdown
          state: 'Home'
    - condition: or
      conditions:
        - condition: sun
          after: sunset
          after_offset: "-1:00:00"
        - condition: sun
          before: sunrise
          before_offset: "1:00:00"
  action:
    - service: scene.turn_on
      data_template:
        entity_id: >
          {% if now().hour >= 20 or now().hour <= 7 %}
            scene.hallway_nightlight
          {% else %}
            scene.hallway_bright
          {% endif %}
- alias: Lights Hallway Off
  trigger:
    - platform: state
      entity_id: binary_sensor.neo_coolcam_battery_powered_pir_sensor_hallway
      to: 'off'
      for:
        minutes: 1
  action:
    - service: homeassistant.turn_off
      entity_id: light.hallway
2 Likes

This should be sooooo simple, yet Homeassistant makes it so difficult, writing code and code and everyone tells you something different… specially when there are a lot of different plataforms out there that do the same thing in 3 simple steps…