Lights on w/ Motion Detection but only at night

I was curious if anyone had a configuration example of an automation when the lights (in the kitchen) for example should turn on only when it is either a) dark in the room based on lumens or b) after 5pm but before 8am.

I’m currently using this automation blueprint:


- id: "164295323987081"

  alias: Kitchen Lights

  description: ""

  use_blueprint:

    path: homeassistant/motion_light.yaml

    input:

      motion_entity: binary_sensor.kitchen_sensor_home_security_motion_detection

      light_target:

        device_id: 1db61fb43141736855c2a45c0dfe542c

      no_motion_wait: 120
1 Like

Trigger.

Trigger 1: motion
Trigger 2: no motion for x mins

Condition: motion sensor light level below x value

Action:

Choose:
Option 1: condition - trigger1
Action - turn on light

Option 2: condition - trigger2
Action - turn off light

This one uses the Sun integration and a lumen sensor as a trigger, but you could easily make those conditions and have your trigger on motion:

- alias: Front Lights On
  initial_state: true
  trigger:
  - platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: -1.0
  - platform: numeric_state
    entity_id: sensor.front_verandah_lux
    below: 200
  condition:
  - condition: time
    after: '17:00:00'
    before: '22:00:00'
  - condition: state
    entity_id: light.front_garden_lights
    state: 'off'
  action:
  - service: light.turn_on
    entity_id: light.front_garden_lights

Thank you. I wonder if there would be a way to include the time – for example: 5PM and 8am only have them come on during that time based on motion activity.

maybe this

1 Like
alias: 'Light: Kitchen'
description: ''
trigger:
  - type: opened
    platform: device
    device_id: cab210b115269b495bde32740deebaca
    entity_id: binary_sensor.kitchen_door_contact
    domain: binary_sensor
    id: kitchen_door
  - type: motion
    platform: device
    device_id: 45a10883d530fe0171a2af8f2f25fe5a
    entity_id: binary_sensor.kitchen_motion_home_security_motion_detection
    domain: binary_sensor
    id: kitchen_motion
  - platform: device
    type: turned_on
    device_id: 798c0acc10864eb1de3c2366900171c2
    entity_id: switch.kitchen_light
    domain: switch
    id: light_on
    for:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - platform: event
    event_type: timer.finished
    id: timer_finished
    event_data:
      entity_id: timer.kitchen_light
  - platform: device
    type: turned_off
    device_id: 798c0acc10864eb1de3c2366900171c2
    entity_id: switch.kitchen_light
    domain: switch
    id: light_off
condition: []
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id: kitchen_door
              - condition: trigger
                id: kitchen_motion
          - condition: or
            conditions:
              - condition: state
                entity_id: input_boolean.lighting_night_mode
                state: 'on'
              - condition: numeric_state
                entity_id: sensor.sma_lux
                below: '1200'
        sequence:
          - type: turn_on
            device_id: 798c0acc10864eb1de3c2366900171c2
            entity_id: switch.kitchen_light
            domain: switch
          - service: timer.cancel
            target:
              entity_id: timer.kitchen_light
          - service: timer.start
            data:
              duration: '00:15:00'
            target:
              entity_id: timer.kitchen_light
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: timer_finished
              - condition: device
                type: is_on
                device_id: 798c0acc10864eb1de3c2366900171c2
                entity_id: switch.kitchen_light
                domain: switch
        sequence:
          - type: turn_off
            device_id: 798c0acc10864eb1de3c2366900171c2
            entity_id: switch.kitchen_light
            domain: switch
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: light_on
              - condition: not
                conditions:
                  - condition: state
                    entity_id: timer.kitchen_light
                    state: active
        sequence:
          - service: timer.start
            data:
              duration: '00:19:00'
            target:
              entity_id: timer.kitchen_light
      - conditions:
          - condition: trigger
            id: light_off
        sequence:
          - service: timer.cancel
            target:
              entity_id: timer.kitchen_light
    default: []
mode: restart

Triggers:

  • Kitchen door opening.
  • Kitchen motion sensor detecting motion
  • Light turned on
  • Timer finished
  • Light off

Logic

  • If kitchen door opened or motion is detected then:
    • if the house is in night mode OR the outside LUX is below 1200 then:
      • turn the light on
      • cancel the timer
      • start the timer
  • If timer finished AND the light is on then:
    • turn light off
  • If light is turned on AND NOT timer active then:
    • start the timer (light was turned on manually, but it should still turn off automatically)
  • If the light is turned off then:
    • cancel the timer (no point in it running if the light is off)

Simply add an additional condition of time.

I truly appreciate the helpful suggestions. I’ll certainly take this into consideration.

Does anyone know if we can setup a ramp up time period or lights come on at 50%?

Thank you all for posting these suggestions. They are very useful for an it illiterate individuaql like myself.

brilliant.

In my fist toe in the water.
I managed to turn on and off the lights correctly.

Once this is proved to work flawlesly. I will develop this into a template and use it for all my lights and add an additional condition that during certain hours the lights only come on at 60 %. (what can i say i’m lazy)

BTW
What does the sun.sun trigger elevation below -1.0 do?

Once again thank you

This will trigger when the Sun angle with the horizon is below -1°. You could use “Sunset” instead, but as during the sunset usually still too bright, during twilight, and you dont need the lights yet and might wanna wait a little bit more until the Sun goes a bit more down the horizon, so you use a negative angle.

Things to remember with sun conditions:

  • after: sunset only works from sunset to midnight. After midnight it won’t trigger
  • before: sunrise only works from midnight to dawn
  • below_horizon is any elevation value less than zero
  • below: -1.0 leaves a bit of time for twilight. You could make it -5.0 or -10.0 - whatever suits your locality. Twilight is longer the further from the equator you are.
2 Likes

Many thank to all

I never would have understood this on my own

I was trying to insert 2 triggers 1) after sunset and 2) before sunrise

the automation would not work

again thank you for your help and education

i’ve used this blueprint : core/motion_light.yaml at a90ef3a5752d39b13ec57c7acee83cc36c0cd3bd · home-assistant/core · GitHub
but modified it to add the condition to only trigger it at night:

blueprint:
  name: Motion-activated Light
  description: Turn on a light when motion is detected.
  domain: automation
  source_url: https://github.com/home-assistant/core/blob/a90ef3a5752d39b13ec57c7acee83cc36c0cd3bd/homeassistant/components/automation/blueprints/motion_light.yaml
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    light_target:
      name: Light
      selector:
        target:
          entity:
            domain: light
    no_motion_wait:
      name: Wait time
      description: Time to leave the light on after last motion is detected.
      default: 120
      selector:
        number:
          min: 0
          max: 3600
          unit_of_measurement: seconds

# If motion is detected within the delay,
# we restart the script.
mode: restart
max_exceeded: silent

trigger:
  platform: state
  entity_id: !input motion_entity
  from: "off"
  to: "on"
condition:
  - condition: not
    conditions:
      - condition: sun
        after: sunrise
        after_offset: '+01:00:00'
        before: sunset
        before_offset: '-01:00:00'
action:
  - alias: "Turn on the light"
    service: light.turn_on
    target: !input light_target
  - alias: "Wait until there is no motion from device"
    wait_for_trigger:
      platform: state
      entity_id: !input motion_entity
      from: "on"
      to: "off"
  - alias: "Wait the number of seconds that has been set"
    delay: !input no_motion_wait
  - alias: "Turn off the light"
    service: light.turn_off
    target: !input light_target
4 Likes

Thanks, this is just perfect. I had to change this blueprint a couples of time before. But adding this condition works great for me. My whole house is full with motion sensors and its not necessary to light up the house when the sun is up :wink:

Update:
Was forgotten that the toilet was on the same blueprint as wel :smiley: So i deleted it from the blueprint and added this condition in the yaml ( because its not an GUI option ) of the automation which is using the blueprint and that worked perfect.