🌇 Activating Lights At Sunset

Tags: #<Tag:0x00007f325f9c8130> #<Tag:0x00007f325f9c8040> #<Tag:0x00007f326af93ed8> #<Tag:0x00007f326af93e10>

GitHub Gist: https://gist.github.com/CyanAutomation/1b8bafd033f73e3c24e42e8f381ff906

A simple blueprint to turn lights on at sunset. The power of home automation is that automation can (and should) be dynamic. As the seasons change, so should how our homes are lit. With this blueprint, we can select the lights to come on as the sun goes down - as well as the brightness they should activate with.

Brightness

The brightness is set and passed as a percentage. If you select multiple lights as part of the automation, please note that they will all turn on with the same selected brightness.

Elevation Offset

Included is 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. To approximate Golden Hour - set the Elevation Offset to 1.

Prerequisites

None

Explanation

This automation uses the state of sun.sun - which is part of an integration native (default) to Home Assistant, unless you’ve explicitly disabled it. Throughout the day the angle (aka elevation) of the sun changes, relative to the horizon. We know that the elevation angle is near zero at sunrise and sunset, so when it goes from a positive value to zero, then this is the time of sunset, which is the trigger for this automation. This will change according the home/default location you’ve set in Home Assistant.

download

Blueprint

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.

blueprint:
  name: Lights On At Sunset
  description: Turn on the following lights at sunset
  domain: automation
  input:
    # Create a variable for identifying the light to act upon
    target_light:
      name: Lights
      description: This is the light (or lights) that will be activated at sunset
      # Use a selector, to pick the light(s)
      selector:
        target:
          entity:
            domain: light
    # Create a variable for capturing the desired brightness of the activated lights
    target_brightness:
      name: Brightness
      description: Brightness of the light(s) when they're activated
      default: 50
      # Use a selector, to set the brightness
      selector:
        number:
          min: 5
          max: 100
          mode: slider
          step: 5
          unit_of_measurement: "%"
    # Create a variable for capturing the desired offset for shifting the trigger
    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. To approximate Golden Hour - set the Elevation Offset to 1.
      default: 0.0
      # Use a selector, to set the time shift or offset
      selector:
        number:
          min: -3.0
          max: 3.0
          mode: slider
          step: 1.0

# Prevent the automation from running concurrently
mode: single

# Define the variables used in the action section
variables:
  target_brightness: !input target_brightness
  target_light: !input target_light

# Define the trigger for the automation
trigger:
  # Using the state of the sun to act as the trigger
  platform: numeric_state
  entity_id: sun.sun
  attribute: elevation
  # Can be a positive or negative number
  below: !input elevation_shift
  
# Add a condition to ensure that this only triggers near sunset, in the evening
condition:
  # Check that it is after sunrise
  condition: sun
  after: sunrise
  # Add a buffer to be sure
  after_offset: "01:00:00"  

# This section will take action on the lights, and turn them on
action:
  # A very simple structure of turning on the selected light(s)
  - service: light.turn_on
    target: !input target_light
    # Set and pass the desired brightness
    data_template:
      brightness_pct: "{{ target_brightness | int }}"

FAQ

Q: What value should I set to trigger this for the Golden Hour, or just before Sunset?
A: Set the Elevation Offset to 1. This is because positive values bring the automation start time forward, whilst negative values delay the start time

Q: Will this also trigger lights off/on at sunrise?
A: No, this is just for turning light on at sunset

Q: I would like to control/activate Switches, instead of Light entities. How do I do this?
A: User Knuspel created a version of this that focuses on switches. To import his version as a Blueprint, Click Here.

Changelog

  • 2020-12-30: Initial version
  • 2021-02-21: Changed sun trigger from Horizon Position to Sun Elevation, which allowed incorporating an elevation angle offset to bring the trigger time forward or backward. Thanks to svh1985 and pepo for the great feedback and ideas.
  • 2021-02-24: Added an “after sunrise” condition, as per awakefie’s suggestion - to ensure that automation does not fire in the morning.
  • 2021-02-25: Reduced the scale of values available for the elevation offset, as realistically - users will not need to have a notable offset value. As per suggestion from svh1985, I’ve put some guidance text in the Blueprint, for setting the trigger near Golden Hour.
  • 2021-03-04: Added FAQs in, as well as drawing attention to Knuspel’s fork, which swaps lights for switches.

First Time Working With Blueprints?

How To Import Blueprints

Since Home Assistant version 2021.3 - you can simply click the link below to import this Blueprint:
Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

3 Likes

Looks great thanks!

I have some suggestions to improve:

  • Possibility to activate a scene
  • Condition to only turn on when one or more persons are at home
  • Maybe even add the Golden Hour in there to make it perfect. At most places, below_horizon is too late/dark to turn on the lights

:smile:

1 Like

At first I also had sunset (or an offset from sunset, but at 59N I noticed early in the fall it was still pretty light when the lights turned on and around mid winter it was already to dark. I changed it to 3 degrees below the horizon and that works very well for me.

...
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state_attr(''sun.sun'', ''elevation'') }}'
    below: -3.0
...
1 Like

it works very good!

1 Like

Great feedback - I’ve included the idea of an Elevation Offset, to allow users to trigger the lighting earlier or later. A value of 1 in the offset would approximate the Golden Hour in most geographies. Hope this helps!

Really appreciated the feedback - what a great idea! It was a little work changing the automation trigger from horizon position to elevation angle, but got it working. The version herein is now updated with you suggestion.

1 Like

I implemented this the other day. It seems that the automation turns the lights on at sunset, but then it turns them on again at or before sunrise. Today it turned them back on at 4:11:32 AM.

Sun.Sun shows that the sun was below the horizon at 5:42:11 PM and that it was above the horizon at 6:36:06 AM

Timezone and such on the server all seem to be correct.

Any ideas would be welcome.

Sorry to be the pedant here but sunset is actually at - 0.833 degrees
Check out Wikipedia

Thanks for the note - and good catch! I recently changed the logic of the automation, and in some cases sunrise was tripping it up. I’ve now updated the code/automation - are you able to re-add and test?

You are completely right. I was aiming for a simple automation, that had some flexibility for the general user. If the blueprint allowed you to enter exact elevation degrees - would it be useful to you, and would you use it?

Sorry but I don’t use blueprints at all.
I have no problems writing templates for anything I need, which generally extends well beyond the capabilities of any blueprint.
I only dropped by to correct your assumption on sunrise/sunset

Will test it and let you know.

A

1 Like

Last night the lights turned on at 11:36;20 PM. They did not turn on again this AM.

Thanks Aaron Wakefield, great to hear that they are now only triggering in the evening - when they are supposed to! Is sunset really that late where you are, being almost midnight? Or have you set the Elevation Offset to a negative value, so the trigger is postponed to later on?

lol, no sunset it around 7PM… So I am not sure what is going on. I had the offset set to -1. I am in Chicago.

That is very strange. Just a reminder (and I often have to check) negative values delay triggering the automation, whilst positive values bring it forward. I would suggest changing from -1 to +1 to see how that affects when they trigger the next time. If they then trigger too early, then zero is likely the right value for you.

I will set it to zero and let you know what we get. I have a feeling something odd is going on here.

1 Like

what i need to change in yamal for activated switch instead of light ??? thanks

Hi - great question! A user named Knuspel created a version of this for switches. You can Click Here, to import his version - if you are on the latest version of Home Assistant. Or manually import the following GitHub link into your Blueprint dashboard: https://gist.github.com/Knuspel/2aff78a59bae9bec0633adbee0e866d4

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

thanks very much.