Simple Motion Light with Sun Condition

Turn on a light when motion is detected during dark hours, with configurable offsets for sunset and sunrise.

Features

  • Turn on lights when motion is detected.
  • Only activates during dark hours (between sunset and sunrise).
  • Configurable offset before sunset — start allowing lights X minutes before sunset. Based on sun.sun entity attribute next_setting.
  • Configurable offset after sunrise — stop allowing lights X minutes after sunrise Based on sun.sun entity attribute next_rising.
  • Configurable wait time after motion stops before turning off
  • Uses duration selector for intuitive time input.

Blueprint

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

blueprint:
  name: Motion-activated light with sun condition
  description: Turn on a light when motion is detected during dark hours.
  domain: automation
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          filter:
            - 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:
        minutes: 2
      selector:
        duration:
    before_sunset:
      name: Before sunset
      description: How long before sunset to start allowing lights. Based on sun.sun entity attribute "next_setting".
      default:
        minutes: 60
      selector:
        duration:
    after_sunrise:
      name: After sunrise
      description: How long after sunrise to stop allowing lights. Based on sun.sun entity attribute "next_rising".
      default:
        minutes: 45
      selector:
        duration:

mode: restart
max_exceeded: silent

variables:
  # Expose inputs for use in templates (inputs can't be used directly in templates)
  before_sunset_input: !input before_sunset
  after_sunrise_input: !input after_sunrise

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

conditions:
  # Allow lights when: after (sunset - offset) OR before (sunrise + offset)
  # 
  # Why today_at(): sun.sun next_rising/next_setting jump to next day after 
  # the event passes, so we extract just the time and apply it to today
  #
  # Why as_local(): converts UTC sun times to local timezone before extracting time
  - condition: or
    conditions:
      - condition: template
        value_template: >
          {% set sun_setting_time = today_at(as_local(as_datetime(state_attr('sun.sun', 'next_setting'))).strftime('%H:%M:%S')) %}
          {{ now() >= sun_setting_time - timedelta(hours=before_sunset_input.hours | default(0), minutes=before_sunset_input.minutes | default(0), seconds=before_sunset_input.seconds | default(0)) }}
      - condition: template
        value_template: >
          {% set sun_rising_time = today_at(as_local(as_datetime(state_attr('sun.sun', 'next_rising'))).strftime('%H:%M:%S')) %}
          {{ now() <= sun_rising_time + timedelta(hours=after_sunrise_input.hours | default(0), minutes=after_sunrise_input.minutes | default(0), seconds=after_sunrise_input.seconds | default(0)) }}

actions:
  - action: light.turn_on
    target: !input light_target
  - wait_for_trigger:
      - trigger: state
        entity_id: !input motion_entity
        from: "on"
        to: "off"
  - delay: !input no_motion_wait
  - action: light.turn_off
    target: !input light_target

Credits

Based on the original Motion Light with Sun Condition blueprint by @Marcos_Felipe — thank you for the initial concept! The original blueprint has a bug where sun offset values are ignored.

1 Like

Update: Please re-import the blueprint

Fixed two bugs:

  1. Next day jumpsun.sun attributes next_rising/next_setting jump to the next day after the event passes, causing conditions to always be true. Now extracts just the time portion using today_at().
  2. Timezone conversion — Sun times were compared incorrectly across timezones. Now properly converts to local time using as_local().

To update:

  1. Settings → Automations & Scenes → Blueprints → three dots on this blueprint → Re-import blueprint
  2. Developer Tools → YAML → Reload Automations