GIthub Gist: Home Assistant Blueprint: Wake-up light alarm with sunrise effect · GitHub
This is a blueprint for a wake-up light / alarm clock using a dimmable and color-temperature controllable light entity. It takes its alarm time from an existing timestamp source (such as the _next_alarm sensor from the Android companion app) or from a manual input. It will generate a sunrise effect starting from a configurable amount of minutes before the alarm time with a dim and warm-colored light slowly transitioning to the fullest brightness and coldest light temperature supported by the lamp at the alarm time. The sunrise effect can be stopped by simply turning off the light during the automation run. User-defined actions can be run at the start of the sunrise.
Prerequisites
You need to enable the ‘date_time_iso’ and ‘date’ sensor in your configuration.yaml and restart HA before using this blueprint in an automation. Configuration can be found here: Time & Date - Home Assistant
Timestamp Sensor
You can provide a timestamp sensor containing the date and time of the next alarm. By default a compatible source is the _next_alarm sensor provided by the Android companion app that reads your next configured smart phone alarm. You can in principle use other sources as long as they contain a valid timestamp in text form with both date and time information that can be parsed by the as_timestamp function. However the source (like the next_alarm sensor) needs to have an attribute device_class set to timestamp before it appears in the dropdown. However you can still manually input the sensor by its name.
Using a manual alarm time
By setting the timestamp sensor to ‘none’ you can use a manual alarm time. The alarm will go off everyday at that time unless you turn off the automation manually or define additional conditions (see below). Note that alarm times at midnight or shortly after will not work as expected. Manual alarm time requires ‘sensor.date’ to be enabled.
Custom Pre-sunrise Actions
As noted you can define custom actions to be run before the sunrise effect. I’m using this e.g. to disable sleep mode in adaptive_lighting.
Additional conditions (e.g. workday, presence)
You can set an additional entity that is checked before the sunrise is triggered. If set the entity needs to have state ‘on’ or ‘home’ otherwise the alarm is not triggered. You can use for example a person entity or a device_tracker to check if a certain person is home before the sunrise is triggered. Other options would be to use a workday sensor to check the day if the day is a workday: Workday - Home Assistant
Alternatively if you have a (bed) occupancy sensor you could use this as well.
Troubleshooting
- Please note that automations based on this blueprint can not be manually triggered. Either a timestamp sensor or a manual time needs to be defined. The automation will start the configured number of minutes (25 by default) BEFORE the alarm time.
- Make sure the machine running HA has the correct time set and you have the correct time zone configured in HA under Settings - General Settings.
- Check your HA logs for related problems, especially those referring to templates. Please post any potentially relevant errors along with your problem.
- If you are having issues please always include your blueprint configuration: use the three-dots menu and select Edit as YAML and include the contents of your configuration along with your post.
Using multiple lights for the sunrise
There are multiple ways to achieve this. If you have multiple lights of the same type you can just group them either using your native light integration (e.g. deconz) or you use HA’s high-level way of defining light groups documented here: Light Group - Home Assistant
In any case the result will be a light entity that controls the whole group as if it was a single light.
You can just use this new light entity in the blueprint and be done. All lights of the group will be synchronized when using the group entity however they can still be individually controlled using their normal light entities.
If you have different kinds of lights with different absolute brightness and / or color temperature range you can simply create one automation for each such light from this blueprint and define e.g. different maximum brightness or even different sunrise durations individually to your liking. Just refer to the same timestamp sensor so that they will end at the same time.
Blueprint Code
Click the badge to import this Blueprint: (needs Home Assistant Core 2021.3 or higher)
Or import this Blueprint by using the forum topic / Gist URL:
blueprint:
name: Wake-up light alarm with sunrise effect
description: 'A wake-up light alarm with a brightness and color temperature sunrise
effect. Note: Requires date_time_iso sensor in configuration, not manually executable!'
domain: automation
input:
light_entity:
name: Wake-up light entity
description: The light to control. Turning it off during the sunrise will keep
it off. Color temperature range is auto-detected.
selector:
entity:
domain: light
timestamp_sensor:
name: Alarm timestamp sensor
description: 'Sensor with timestamp of next alarm with device_class: timestamp
(set to ''none'' for manual alarm time)'
default: none
selector:
entity:
device_class: timestamp
manual_time:
name: Manual alarm time
description: Time to trigger alarm every day if timestamp sensor is not set.
Settings at or shortly after midnight will not work as expected!
default: '7:00:00'
selector:
time: {}
check_entity:
name: Additional entity to check before sunrise is triggered
description: If set, checks if entity is 'on' or 'home' before triggering. Use
e.g. a (workday) sensor, device_tracker or person entity.
default: none
selector:
entity: {}
sunrise_duration:
name: Sunrise duration
description: The sunrise will start the configured number of minutes before
the timestamp.
default: 25
selector:
number:
min: 5.0
max: 60.0
step: 5.0
unit_of_measurement: min
mode: slider
start_brightness:
name: Minimum brightness
description: The brightness to start with. Some lights ignore very low values
and may turn on with full brightness instead!
default: 1
selector:
number:
min: 1.0
max: 255.0
step: 1.0
mode: slider
end_brightness:
name: Maximum brightness
description: The brightness will be transitioned from the minimum to the configured
value.
default: 254
selector:
number:
min: 5.0
max: 255.0
step: 1.0
mode: slider
min_mired:
name: Minimum color temperature
description: 'The minimum color temperature to use. (0: lowest supported)'
default: 0
selector:
number:
min: 0.0
max: 500.0
step: 5.0
mode: slider
unit_of_measurement: mired
pre_sunrise_actions:
name: Pre-sunrise actions
description: Optional actions to run before sunrise starts.
default: []
selector:
action: {}
post_sunrise_actions:
name: Post-sunrise actions
description: Optional actions to run after sunrise ends (around the alarm time).
default: []
selector:
action: {}
source_url: https://gist.github.com/sbyx/96c43b13b90ae1c35b872313ba1d2d2d
variables:
light_entity: !input 'light_entity'
sensor: !input 'timestamp_sensor'
sunrise_duration: !input 'sunrise_duration'
start_brightness: !input 'start_brightness'
end_brightness: !input 'end_brightness'
range_brightness: '{{float(end_brightness)-float(start_brightness)}}'
manual_time: !input 'manual_time'
seconds: '{{float(sunrise_duration) * 60}}'
min_mired: !input 'min_mired'
start_mired: '{{state_attr(light_entity, ''max_mireds'')}}'
end_mired: '{{[state_attr(light_entity, ''min_mireds'')|int(0), min_mired|int(0)]|max}}'
tick_time: '{{float(seconds) / float(range_brightness)}}'
check_entity: !input 'check_entity'
trigger:
- platform: time_pattern
minutes: '*'
condition: []
action:
- wait_template: '{{sensor == ''none'' or as_timestamp(states(sensor), None) != None}}'
- wait_template: '{{0 < as_timestamp(states(sensor) if sensor != ''none'' else states(''sensor.date'')
~ '' '' ~ manual_time) - as_timestamp(states(''sensor.date_time_iso'')) <= float(seconds)
and states(check_entity) in [''unknown'', ''on'', ''home'']}}'
- choose: []
default: !input 'pre_sunrise_actions'
- condition: template
value_template: '{{sensor == ''none'' or as_timestamp(states(sensor), None) != None}}'
- condition: template
value_template: '{{0 < as_timestamp(states(sensor) if sensor != ''none'' else states(''sensor.date'')
~ '' '' ~ manual_time) - as_timestamp(now()) <= float(seconds) and states(check_entity)
in [''unknown'', ''on'', ''home'']}}'
- choose:
- conditions:
- '{{state_attr(light_entity, ''min_mireds'') != None}}'
sequence:
- service: light.turn_on
data:
brightness: '{{start_brightness}}'
color_temp: '{{start_mired}}'
entity_id: !input 'light_entity'
default:
- service: light.turn_on
data:
brightness: '{{start_brightness}}'
entity_id: !input 'light_entity'
- repeat:
while:
- '{{sensor == ''none'' or as_timestamp(states(sensor), None) != None}}'
- '{{0 < as_timestamp(states(sensor) if sensor != ''none'' else states(''sensor.date'')
~ '' '' ~ manual_time) - as_timestamp(now()) <= float(seconds)}}'
sequence:
- delay: '{{tick_time}}'
- choose:
- conditions:
- '{{0 < state_attr(light_entity, ''brightness'') | int(0) < end_brightness |
int}}'
- '{{sensor == ''none'' or as_timestamp(states(sensor), None) != None}}'
- '{{0 < as_timestamp(states(sensor) if sensor != ''none'' else states(''sensor.date'')
~ '' '' ~ manual_time) - as_timestamp(now()) <= float(seconds)}}'
sequence:
- choose:
- conditions:
- '{{state_attr(light_entity, ''min_mireds'') != None}}'
sequence:
- service: light.turn_on
data:
brightness: '{{(float(end_brightness) - (float(range_brightness) *
(as_timestamp(states(sensor) if sensor != ''none'' else states(''sensor.date'')
~ '' '' ~ manual_time) - as_timestamp(now())) / float(seconds)))
| int(0)}}'
color_temp: '{{(float(end_mired) + (float(start_mired) - float(end_mired))
* ((as_timestamp(states(sensor) if sensor != ''none'' else states(''sensor.date'')
~ '' '' ~ manual_time) - as_timestamp(now())) / float(seconds)))
| int(0)}}'
entity_id: !input 'light_entity'
default:
- service: light.turn_on
data:
brightness: '{{(float(end_brightness) - (float(range_brightness) * (as_timestamp(states(sensor)
if sensor != ''none'' else states(''sensor.date'') ~ '' '' ~ manual_time)
- as_timestamp(now())) / float(seconds))) | int(0)}}'
entity_id: !input 'light_entity'
- choose: []
default: !input 'post_sunrise_actions'
mode: single
max_exceeded: silent
Explanation
In the variables section we are first reading the max_mireds and min_mireds from the light entity to define the color temperature range we can use for the sunrise effect.
The trigger is actually not the real trigger as you may guess. It will simply start the automation every minute in case it is not running and will waiting for the real trigger in the wait_template of the first action. This seems necessary since the computed blueprint variables are not available in the actual trigger section already. Note that we are using the date_time_iso sensor here instead of the now() function to cause reevaluation of the template at least every minute.
We are running pre-sunrise actions inside a choose block with an always-true condition since I haven’t found a better way to include them in the flow yet. If you have one, please let me know!
Thanks to swiftlyfalling for providing a better solution!
We turn on the light unconditionally only the first time with minmum brightness and warmest color temperature. Afterwards we always check if someone didn’t turn off the light in the meantime and in that case we just skip updating the light. We are however not leaving the automation since that would cause it to restart turning the light on again.
Now in the main loop every 5 seconds we recalculate what should be the current brightness and temperature of the light based on how close we are to the alarm time and simply update the lamp.
Changelog
- 2021-11-08: Work around breaking changes in template functions in HA 2022.1
- 2021-01-13: Test whether light was manually set above end brightness.
- 2021-01-12: Add custom post-sunrise actions
- 2021-01-10: Add additional sensor to check before sunrise
- 2021-01-08: Fix minimum color temperature detection
- 2021-01-06: Add optional minimum brightness and color temperature.
- 2021-01-01: Work-around template issue in HA to fix manual time.
- 2020-12-19: Use absolute brightness steps instead of percent and remove transitions again.
- 2020-12-18: Fix custom-conditions triggering light at random times.
- 2020-12-17: Added support for manual alarm time
- 2020-12-16: Added support for non-color-temperature lights. Added default for pre-sunrise-actions (thanks to swiftlyfalling).
- 2020-12-15 (15.00 UTC): Added default values. Dynamically determine tick intervals based on duration and maximum brightness instead of using fixed 5 second interval. Added transitions between intervals for lights that support it.
- 2020-12-15: Added maximum brightness input.
- 2020-12-14: Avoid logging errors when template sensor is not available or does not contain a valid timestamp temporarily.
- 2020-12-13: Initial version