Help with automating Courtesy Light

Hi.
I’ve searched many times but I haven’t find an asnwer to my question: how configure an automation to turn on a light when a motion is detected only at nighttime, and turn light off after x time if no more motion is detected? and if the light was turned on by wall switch, no turn off even if no motions are detected?

This is the real case: I enter a room along the night and the light must be turned on automatically. Until I’m in the room, don’t want to be leaved in the dark. If I leave the room, the light must be turned off after n minutes. If i enter the room and I use the wall switch immediately (no motion detected yet) the light must be leaved on, only the wall switch or the MQTT queue (HA frontend) can turn off the light - basically, the motion sensor is ignored; at daytime, the motion sensor is ignored.

Is it possible? can you help me? I’ll use a PIR o the IP camera motion to detect the movements (sensor), and a modified Sonoff switch (WiFi based Relay switch) that handles wall switch and MQTT messages to manage the light and to publish the state to the queue (MQTT switch)

Thanks if someone should explain me how to implement this.

1 Like

I think that this thread is the granddaddy on this subject:

But here’s what I do. It doesn’t cover your part about not turning off the auto-switching if a physical switch was used. It depends on motion sensors to keep track of whether someone’s in the room. This wouldn’t work great in a room where you sit and watch TV (no motion), but works good in my garage. I trigger if one of these things happens: door from house to garage opens, either garage door opens (door to outside), security alarm in in “exit delay” (we’re most often going to enter the garage next after arming alarm) or motion is sensed.

My motion sensors (Monoprice) send an ON when motion is sensed, then an OFF when motion stops. I set a delay via a script on the motion’s OFF to allow for a grace period where I might not have moved enough to keep the motion sensor engaged.

Hope you find this helpful.




Automation:
  - alias: Garage Doors Trigger Lights
    trigger:
        # garage doors (to outside)
      - platform: state
        entity_id: 
          - binary_sensor.scott_garage_door_sensor_5_0
          - binary_sensor.holly_garage_door_sensor_15_0
        # this is the door from house to garage
      - platform: state
        entity_id: binary_sensor.garage_door
        to: 'on' 
        # security alarm set
      - platform: state
        entity_id: sensor.home_alarm_keypad
        state: 'Exit Delay In Progress'
    action:
      #turn on the light no matter what
      - service: homeassistant.turn_on
        entity_id: switch.garage_ceiling_switch_29_0
      # only continue if the motion is off since we'll use the motion OFF as its own trigger
      - condition: state
        entity_id: binary_sensor.vision_zp3102_eu_pir_motion_sensor_sensor_6_0 
        state: 'off' 
      ## Cancel and restart the timer each time a trigger happens
      - service: script.turn_off
        entity_id: script.timed_lamp_garage
      - service: script.timed_lamp_garage

  - alias: Motion ON in Garage Trigger Lights
    trigger:
      platform: state
      entity_id: binary_sensor.vision_zp3102_eu_pir_motion_sensor_sensor_6_0
      to: 'on'
      from: 'off'
    action:
    # if we get motion, turn on the light and cancel an existing timer
    # Remember: this motion sensor triggers ON when motion starts and OFF when motion ends
      - service: homeassistant.turn_on
        entity_id: switch.garage_ceiling_switch_29_0
      - service: script.turn_off
        entity_id: script.timer_off_garage
        
  - alias: Motion OFF in Garage Trigger Countdown Script
    trigger:
      platform: state
      entity_id: binary_sensor.vision_zp3102_eu_pir_motion_sensor_sensor_6_0
      to: 'off'
    action:
      # light should already be on, but turn it on anyway
      - service: homeassistant.turn_on
        entity_id: switch.garage_ceiling_switch_29_0
        # you can't turn on a script that's already running (it just raises an error), so turn it off then back on
      - service: script.turn_off
        entity_id: script.timer_off_garage
      - service: script.timer_off_garage
      
script:
  ## keep garage ceiling lights on while motion ##
  timed_lamp_garage:
    alias: "Turn garage ceiling on and set timer"
    sequence:
      # Cancel ev. old timers
      - service: script.turn_off
        entity_id: script.timer_off_garage
      - service: homeassistant.turn_on
        entity_id: switch.garage_ceiling_switch_29_0
      # Set new timer
      - service: script.timer_off_garage
        
  timer_off_garage:
    alias: "Turn garage ceiling off after 5 minutes"
    sequence:
      - delay:
          minutes: 5
      - service: switch.turn_off
        entity_id: switch.garage_ceiling_switch_29_0      
2 Likes

You’ll want to add time or sunset conditions to the trigger to make this only happen at night.

Thank you very much, I think I finally understand something more about HA configuration.