Turn lights on/off with motion - but only if not already on?

Hi there,

I want to make this setup:

  • A motion sensor should turn my lights on (and off when motion is gone)
  • But ONLY if the lights were off, it should turn in on and then later off

Its easy to create an automation for turning it on if its not already on - but how should I make sure its only turned off, if it was OFF initially when motion occured ?

So when kids go up at night (lights are off) and they turn on and then off after they go back to bed - but if its at 8pm, and lights are probably on already, it should not turn on at motion and expecially NOT turn OFF when motion is gone.

Any ideas ?

Cheers from Denmark,
Tony

Hi,

In my case a have and script, that when motion is detected, light turn ON, and after few minutes (i define these minutes in the script), then the light turn OFF.

This is the script:

timed_lamp:
  alias: 'Switch ON light'
  sequence:
    - service: script.turn_off
      data:
         entity_id: script.timer_off
    - service: light.turn_on
      data:
        entity_id: light.yeelight_white_f0xxxxxxxxx
        brightness: 64
    # Set new timer
    - service: script.turn_on
      data:
        entity_id: script.timer_off

timer_off:
  alias: 'Switch off light after 3 minutes'
  sequence:
    - delay:
        minutes: 3
    - service: light.turn_off
      data:
        entity_id: light.yeelight_white_f0xxxxxxxxxxx

I found this script in forum.

Then in automation, you must have something similar to this:

alias: Switch ON Corridor light
hide_entity: False
initial_state: 'off'
trigger:
  platform: state
  entity_id: binary_sensor.motion_sensor_xxxxxxxxxxx
  to: 'on'
action:
  service: homeassistant.turn_on
  entity_id: script.timed_lamp

And if you want, you can add a condition about the status of light.

Thanks, but what happens in this script if lights are already ON when it triggers ?

I mean, if my wife is sitting in the kitchen, lights are on and I pass the motion sensor, it then after 3 minutes turns OFF the light.

Then I think my “wife acceptance factor” is going down the drain :wink: …

So, in the automation, you should add a condition, before actiave the script. In this condition, you must see the state of the light.

alias: Luz_Pasillo_Noche_Mov_Pasillo
hide_entity: False
initial_state: 'off'
trigger:
  platform: state
  entity_id: binary_sensor.motion_sensor_158d00010ecfa8
  to: 'on'
condition:
  condition: state
      entity_id: 'light_status'
      state: 'off'  
action:
  service: homeassistant.turn_on
  entity_id: script.timed_lamp

You can see in documentation about automation, the differents option about triggers, conditions and actions

https://www.home-assistant.io/docs/automation/

conditions

I use an input boolean to track whether the light was turned on by the automation.

3 Likes

yes, I thought of using an (invisible) boolean.

But I think the example with a timer to shut off the light will work too - as it only initiates if the light was off (and its nighttime and so forth) …

Thanks guys!

@Tinkerer Does your light work automatically ONLY if it has been turned off by user?

Meaning that if manually turned on it will never turn off automatically?

I take it you didn’t read it :wink:

  1. Turns on the light if the motion sensor detects motion, and the light is off. Turns on the input boolean.
  2. Turns off the light if there’s been no motion for 5 minutes, and the input boolean is on.
  3. Turns off the input boolean when the light is turned off

So yes. If turned manually on it won’t turn off automatically

That’s basicall my solution, too. I have three input_boolean switches to control my (exteriour) lights:

  • one switch is turned on and off by sunset and sunrise indicating “darkness”
  • a second switch is turned on at sunset and turned off at 11pm. That’s my “permanent lights” phase
  • a third switch is turned on by motion and turned off 5mins after last motion appeared. This is my “movement” trigger.

Exteriour lights are turned on if “darkness” is on AND either “permanent lights” OR “movement” (or both) are on.

Another idea is to use wait. Create an automation that turns on a light only when light is off and wait till no movement to shut it off.

1 Like