Outdoor Night Lighting Control with Migration Overrides

This blueprint handles turning outdoor lights on at dusk and off at dawn.

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

Features

  • Turn lights on at sunset and off at sunrise
  • Set offsets for sunset/sunrise (for example, turn lights on a half-hour after sunset and off a half-hour before sunrise)
  • Specify a fixed time for turning lights on or off (vs. using sunset/sunrise)
  • Turn lights off in the middle of the night during spring and fall bird migration periods
  • Ability to set all parameters vs. using defaults (e.g., different migration date periods, etc.)

Inputs

  • One or more devices to control.
    • The blueprint allows selecting devices from either the Light or Switch domains.
    • The actions used are homeassistant.turn_on and homeassistant.turn_off.
  • Type of trigger for turning on lights:
    • Sunset (default)
    • Fixed Time
  • Type of trigger for turning off lights:
    • Sunrise (default)
    • Fixed Time
  • Optional offsets for sunrise and sunset in format HH:MM:SS which can be negative
  • Migration override switch
    If set, the lights are turned off during critical night time hours and following inputs apply:
    • Override lights off time (defaults: 11PM)
    • Override lights on time (defaults: 6AM)
    • Migration periods start and end:
      • Spring period start month and day (default: March 1)
      • Spring period end month and day (default: June 15)
      • Fall period start month and day (default: August 15)
      • Fall period end month and day (default: November 30)

The date ranges and override times are based on the Lights Out Texas program. The ability to override date ranges is provided so you can adjust based on your own local circumstances or to target the defined critical peak periods vs. the full migration periods.

For more information on Lights Out Texas, click here.

NOTE: Existing code doesn’t check that the end of the migration override time frame is before sunrise (adjusted by offset). If the turn-on time at the end of the migration override occurs after local sunrise/offset, it could result in the lights being on all day. A check for this is on my list of potential enhancements.

Github Gist: Outdoor Night Lighting Control with Migration Overrides

Change Log

  • 2024-08-30: Initial release
  • 2024-09-03: Streamlined migration season template code

Source Code

blueprint:
  name: Outdoor Night Lighting Control with Migration Overrides
  description: >
    Control outdoor lights with options for on at sunset and off at sunrise (with  optional offsets), or at fixed times, and bird migration overrides.


    - By default, lights are turned on at sunset and turned off at sunrise. If an offset is specified (positive or negative), that is applied to either the  sunset or sunrise time.

    - As an alternative to using sunset or sunrise, lights can be turned on and/or off at a fixed time.

    - During the specified date ranges for spring and fall bird migration periods, lights are turned off at 11pm and turned on at 6am. The date ranges and the override off/on times can be changed. In addition, the override feature can be turned off.

    
    The date ranges and override times are based on the **Lights Out Texas** program.  The ability to override date ranges is provided so you can adjust based on your  own local circumstances. 

    
    For more information on **Lights Out Texas**,  [click here.](https://texanbynature.org/projects/lights-out-texas/)
    
    
    Release date: 2024-09-03
  domain: automation
  source_url: https://gist.github.com/NateEaton/261ff8d0a88dd74263b980c0725d91f6
  # Which devices to control
  input:
    lights:
      name: Outdoor Lights
      description: Select one or more outdoor lights to control. Device domain options include lights and switches.
      selector:
        target:
          entity:
          - domain:
            - light
            - switch
    # How to turn lights on 
    lights_on_trigger_section:
      name: Turn On at Sunset or Fixed Time
      icon: mdi:weather-sunset
      description: Options for using sunset or fixed time as a trigger to turn lights on
      collapsed: true
      input:
        lights_on_trigger_type:
          name: Lights On Trigger Type
          description: Choose whether to use sunset or a fixed time to turn lights on
          default: sunset
          selector:
            select:
              options:
                - label: Sunset
                  value: sunset
                - label: Fixed Time
                  value: fixed_time
        offset_sunset:
          name: Offset Before/After Sunset
          description: 'Time offset before or after sunset to turn lights on (hh:mm:ss, can be negative)'
          default: 00:00:00
          selector:
            text:
              multiple: false
              multiline: false
        fixed_on_time:
          name: Fixed Time to Turn Lights On
          description: 'Fixed time to turn lights on (format: hh:mm:ss)'
          default: '20:00:00'
          selector:
            time: {}
    # How to turn lights off
    lights_off_trigger_section:
      name: Turn Off at Sunrise or Fixed Time
      icon: mdi:weather-sunset-up
      description: Options for using sunrise or fixed time as a trigger to turn lights off
      collapsed: true
      input:
        lights_off_trigger_type:
          name: Lights Off Trigger Type
          description: Choose whether to use sunrise or a fixed time to turn lights off
          default: sunrise
          selector:
            select:
              options:
                - label: Sunrise
                  value: sunrise
                - label: Fixed Time
                  value: fixed_time
        offset_sunrise:
          name: Offset Before/After Sunrise
          description: 'Time offset before or after sunrise to turn lights off (hh:mm:ss, can be negative)'
          default: 00:00:00
          selector:
            text:
              multiple: false
              multiline: false
        fixed_off_time:
          name: Fixed Time to Turn Lights Off
          description: 'Fixed time to turn lights off (format: hh:mm:ss)'
          default: '20:00:00'
          selector:
            time: {}
    # Whether and how to override lights during migration periods 
    migration_override_section:
      name: Migration Override
      icon: mdi:bird
      description: Options for migration overrides
      collapsed: true
      input:
        migration_override:
          name: Migration Override
          description: Enable or disable migration override
          default: true
          selector:
            boolean: {}
        migration_override_off_time:
          name: Migration Override Lights Off Time
          description: Time to turn lights off during migration
          default: '23:00:00'
          selector:
            time: {}
        migration_override_on_time:
          name: Migration Override Lights On Time
          description: >
            Time to turn lights back on during migration.
            CAUTION: if sunrise is before 6am then this could result in leaving lights on all day (i.e., the trigger associated with this happening after the trigger to turn lights on at runrise.) 
            Consider setting this to just before earliest sunrise time in your area (also accounting for any offset you've set).
          default: 06:00:00
          selector:
            time: {}
        spring_migration_start_month:
          name: Spring Migration Start Month
          description: Start month for spring migration
          default: March
          selector:
            select:
              options:
              - January
              - February
              - March
              - April
              - May
              - June
              - July
              - August
              - September
              - October
              - November
              - December
              multiple: false
              custom_value: false
              sort: false
        spring_migration_start_day:
          name: Spring Migration Start Day
          description: Start day for spring migration
          default: 1
          selector:
            number:
              min: 1.0
              max: 31.0
              mode: box
              step: 1.0
        spring_migration_end_month:
          name: Spring Migration End Month
          description: End month for spring migration
          default: June
          selector:
            select:
              options:
              - January
              - February
              - March
              - April
              - May
              - June
              - July
              - August
              - September
              - October
              - November
              - December
              multiple: false
              custom_value: false
              sort: false
        spring_migration_end_day:
          name: Spring Migration End Day
          description: End day for spring migration
          default: 15
          selector:
            number:
              min: 1.0
              max: 31.0
              mode: box
              step: 1.0
        fall_migration_start_month:
          name: Fall Migration Start Month
          description: Start month for fall migration
          default: August
          selector:
            select:
              options:
              - January
              - February
              - March
              - April
              - May
              - June
              - July
              - August
              - September
              - October
              - November
              - December
              multiple: false
              custom_value: false
              sort: false
        fall_migration_start_day:
          name: Fall Migration Start Day
          description: Start day for fall migration
          default: 15
          selector:
            number:
              min: 1.0
              max: 31.0
              mode: box
              step: 1.0
        fall_migration_end_month:
          name: Fall Migration End Month
          description: End month for fall migration
          default: November
          selector:
            select:
              options:
              - January
              - February
              - March
              - April
              - May
              - June
              - July
              - August
              - September
              - October
              - November
              - December
              multiple: false
              custom_value: false
              sort: false
        fall_migration_end_day:
          name: Fall Migration End Day
          description: End day for fall migration
          default: 30
          selector:
            number:
              min: 1.0
              max: 31.0
              mode: box
              step: 1.0

trigger:
# Lights on triggers
- platform: sun
  event: sunset
  offset: !input offset_sunset
  id: sunset
- platform: time
  at: !input fixed_on_time
  id: fixed_on_time
# Light off triggers
- platform: sun
  event: sunrise
  offset: !input offset_sunrise
  id: sunrise
- platform: time
  at: !input fixed_off_time
  id: fixed_off_time
# Migration override triggers
- platform: time
  at: !input migration_override_off_time
  id: migration_off_time
- platform: time
  at: !input migration_override_on_time
  id: migration_on_time

variables:
  # Needed to use inputs in template logic
  lights_on_trigger_type: !input lights_on_trigger_type
  lights_off_trigger_type: !input lights_off_trigger_type
  migration_override: !input migration_override
  migration_override_on_time: !input migration_override_on_time
  offset_sunrise: !input offset_sunrise
  spring_migration_start_month: !input spring_migration_start_month
  spring_migration_start_day: !input spring_migration_start_day
  spring_migration_end_month: !input spring_migration_end_month
  spring_migration_end_day: !input spring_migration_end_day
  fall_migration_start_month: !input fall_migration_start_month
  fall_migration_start_day: !input fall_migration_start_day
  fall_migration_end_month: !input fall_migration_end_month
  fall_migration_end_day: !input fall_migration_end_day
  in_migration_season: >-
    {% set month_mapping = {
      "January": 1, "February": 2, "March": 3, "April": 4, "May": 5, "June": 6,
      "July": 7, "August": 8, "September": 9, "October": 10, "November": 11, "December": 12
      }
    %} 
    {% set n = now() %}
    {% set spring_start = n.replace(month=month_mapping[spring_migration_start_month],
      day=spring_migration_start_day) %} 
    {% set spring_end = n.replace(month=month_mapping[spring_migration_end_month],
      day=spring_migration_end_day) %} 
    {% set fall_start = n.replace(month=month_mapping[fall_migration_start_month],
      day=fall_migration_start_day) %} 
    {% set fall_end = n.replace(month=month_mapping[fall_migration_end_month],
      day=fall_migration_end_day) %} 
    {% if (spring_start <= n <= spring_end) or (fall_start <= n <= fall_end) %}
      true
    {% else %}
      false
    {% endif %}  

action:
- choose:
  - conditions:
    # Turn on lights if sunset trigger set
    - condition: trigger
      id: sunset
    - condition: template
      value_template: '{{ lights_on_trigger_type == "sunset" }}'
    sequence:
    - action: homeassistant.turn_on
      target: !input lights
  - conditions:
    # Turn on lights if fixed time trigger set
    - condition: trigger
      id: fixed_on_time
    - condition: template
      value_template: '{{ lights_on_trigger_type == "fixed_time" }}'
    sequence:
    - action: homeassistant.turn_on
      target: !input lights
  - conditions:
    # Turn off lights if sunrise trigger set
    - condition: trigger
      id: sunrise
    - condition: template
      value_template: '{{ lights_off_trigger_type == "sunrise" }}'
    sequence:
    - action: homeassistant.turn_off
      target: !input lights
  - conditions:
    # Turn off lights if fixed time trigger set
    - condition: trigger
      id: fixed_off_time
    - condition: template
      value_template: '{{ lights_off_trigger_type == "fixed_time" }}'
    sequence:
    - action: homeassistant.turn_off
      target: !input lights
  - conditions:
    # Turn lights off during migration season
    - condition: trigger
      id: migration_off_time
    - condition: template
      value_template: '{{ in_migration_season }}'
    sequence:
    - if:
      - condition: template
        # Only execute if override is enabled during migration season
        value_template: '{{ migration_override }}'
      then:
      - action: homeassistant.turn_off
        target: !input lights
  - conditions:
    # Turn lights back on during migration season
    - condition: trigger
      id: migration_on_time
    - condition: template
      value_template: '{{ in_migration_season }}'
    sequence:
    - if:
      - condition: template
        # Only execute if override is enabled during migration season
        # Future enhancement idea: only execute if migration_override_on_time is earlier
        # than sunrise (adjusted with offset) or the specified fixed off time. 
        value_template: '{{ migration_override }}'
      then:
      - action: homeassistant.turn_on
        target: !input lights
mode: single