Turn light on and off based on detected motion with sun condition

This based on Turn light on and off based on detected motion (by dondochaka)

This is a simple automation that turns a light on when there is motion, and turns it off when there is not.
but I added a condition so that it only trigger only in sunset, it is possible to configure the sunset and sunrise offset.

Get started

Click the badge to import this Blueprint: (needs Home Assistant Core 2021.3 or higher)

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Or import this Blueprint by using the forum topic URL:

blueprint:
  name: Motion-activated Light with sun condition
  description: Turn on a light when motion is detected and sunset.
  domain: automation
  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
          
    sunset_offset:
      name: Offset to turn on lights
      description: Define offset to sunset
      default: 60
      selector:
        number:
          min: 0
          max: 240
          unit_of_measurement: minutes
          
    sunrise_offset:
      name: Offset to turn off lights
      description: Define offset to sunrise
      default: 45
      selector:
        number:
          min: 0
          max: 240
          unit_of_measurement: minutes

# 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: or
    conditions:
    - condition: sun
      after: sunset
      after_offset: !input sunset_offset
    - condition: sun
      before: sunrise
      before_offset: !input sunrise_offset

action:
  - service: light.turn_on
    target: !input light_target
  - wait_for_trigger:
      platform: state
      entity_id: !input motion_entity
      from: "on"
      to: "off"
  - delay: !input no_motion_wait
  - service: light.turn_off
    target: !input light_target
10 Likes

Good Automation.

Can you also add contact sensor condition there?

I’ll like the lights to go ON/OFF when any of exterior door is open to trigger the outdoor light as well at sunset

3 Likes

But did you know how write next condition to check that light is off ? But when is ON automation not run.

Good Automation!

Can you add some brightness to turning on the light? Or can you tell me how to do it myself?

Thanks in advance and keep up the good work

Do not understand, if the light is on why to turn on?

1 Like

change

- service: light.turn_on
    target: !input light_target

to:

service: light.turn_on
target: !input light_target
data:
  brightness: 50

No. When light is on manual automation not run, but when light is off (no turn manual) automation based on motion and elevation sun run…
But for this we must add condition…

1 Like

The Blueprint worked like a charm, thanks for sharing!

hey thanks for this , how can i see my devices that are switches ? i only see lights, but all my lights are via fibaro swithes, thanks.

I don’t know if it’s possible to allow 2 types of devices in blueprints. you can customize the switches as lights.
customizing-devices

and the other way around ?
i changed the domain to switch in the script and see the switches now, but what else to change ? domain: switch

I didn’t find a way to keep 2 domains, you can use my blueprint as a base for a switch or keep your lights as lights and switches, see here: https://www.home-assistant.io/integrations/light.switch/

via the integrations light.switch i can see the devices as entities and selectable , so i will work like that. many thanks for the info. edit: not working for me , i use fibaro double switch, when added this one , all of them go unavailable …with single swicthes it works. Changed the domain, all the lights that i need are on a switch , so that is a better solution for me.

Does this have a manual override of the motion detector? I want this in the bedroom but I do not want the light turning on when I move during my sleep.

Thank you for the blueprint. I’m new to HA from smartthings I’ve been wanting sun setting forever with smartthings. I do however have a question. Is there a way to add a time based stop? EX I like the motion in the evenings but after 10pm when everyone is asleep I don’t need the animals running through the motion rooms turning on and off lights all night? thanks for the help

Hi, thanks for this, however I can’t get it to trigger correctly. I’m trying to do a basic ‘Turn off lights after 5 seconds, when there is no motion detected’. I have set the ‘Wait time’ to ‘5 seconds’, but the light still stays on.

Blueprint:

alias: Turn of WC Light when no motion
description: ''
use_blueprint:
  path: >-
    Marcos_Felipe/turn-light-on-and-off-based-on-detected-motion-with-sun-condition.yaml
  input:
    motion_entity: binary_sensor.wc_motion_sensor
    light_target:
      entity_id: light.wc_light
    sunrise_offset: 5
    sunset_offset: 2
    no_motion_wait: 5

Help would be appreciated, thanks.

the parameter to turn off after movement depends on the time your sensor changes the sensor status to off, mine for example takes 2 minutes.

Is there any reason the offsets are not allowed to be less than 0? I want my automation to be able to trigger 30 minutes before sunset, but the min_value for sunset_offset is 0.

Hi,

I use this variant:

blueprint:
  name: Motion-activated Light On at Sunset
  description: Turn on a light when motion is detected On At Sunset.
  domain: automation
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion

   elevation_shift:
      name: Elevation Shift
      description: Using an elevation offset (height of sun relative to the horizon) to shift the sunset trigger, either earlier or later. Positive values bring the automation start time forward, whilst negative values delay the start time.
      default: 0.0
      # Use a selector, to set the time shift or offset
      selector:
        number:
          min: -10.0
          max: 10.0
          mode: slider
          step: 1.0

    target_light:
      name: Lights
      description: This is the light (or lights) that will be activated at sunset
      selector:
        target:
          entity:
            domain: light

    target_brightness:
      name: Brightness
      description: Brightness of the light(s) when they're activated
      default: 50
      selector:
        number:
          min: 5.0
          max: 100.0
          mode: slider
          step: 5.0
          unit_of_measurement: '%'

    target_color_temp:
      name: Temperature
      description: Termperature of the light(s) when they're activated
      default: 280
      selector:
        number:
          min: 153
          max: 454
          mode: slider
          step: 1

    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

variables:
  target_brightness: !input 'target_brightness'
  target_color_temp: !input 'target_color_temp'

trigger:
  platform: state
  entity_id: !input motion_entity
  from: "off"
  to: "on"

condition:
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: !input elevation_shift

action:
  - service: light.turn_on
    target: !input target_light
    data_template:
      brightness_pct: '{{ target_brightness | int }}'
      color_temp: '{{ target_color_temp | int }}'
  - wait_for_trigger:
      platform: state
      entity_id: !input motion_entity
      from: "on"
      to: "off"
  - delay: !input no_motion_wait
  - service: light.turn_off
    target: !input target_light
2 Likes

Hi,

thank you for the blueprint.

But the specification in minutes for the values sunrise_offset and sunset_offset didn´t work for me.
I had to specify both in the following format:

sunrise_offset: '02:00:00'
sunset_offset: '01:00:00'