GitHub Gist: Home Assistant Blueprint for Activating Lights At Sunset · GitHub
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.
Blueprint
Click the badge to import this Blueprint: (needs Home Assistant Core 2021.3 or higher)
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?
Since Home Assistant version 2021.3 - you can simply click the link below to import this Blueprint: