Turn on lights on and keep them on when motion detection

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…

:slight_smile: I guess you notice that the thread is 6 years old and last comment 4 years old. In the meanwhile, home assistant evolved and you can now use the GUI to build such easy automation .
BTW - even more complex automations are easy to setup in the GUI

Starting i s never easy, the GUI is great these days, my coding is iffy so I like to use the GUI. That said your automation would be easiest as you make two automation’s, the first with the trigger of motion setting the light on, the second one is switching it off if condition is that there is no movement anymore and after some time.
That is it, easily done in the GUI.
Sure you can add all kind of things like between hours, or if the sun is below a certain inclination or…

Good luck, there are numerous great examples to be found on the forum.

1 Like