Forked from sbyx/wake-up-light-alarm-with-sunrise-effect.yaml
Created
April 15, 2021 14:29
-
-
Save nakato/c7b28f19233482fd04f4f72d3cd0f0c2 to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Wake-up light alarm with sunrise effect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, min_mired|int]|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}}' | |
- wait_template: >- | |
{%- set now_ts = utcnow() | as_timestamp | int -%} | |
{%- set sensor_ts = (states(sensor) | as_timestamp | int) or 0 -%} | |
{%- if sensor_ts != 0 -%} | |
{%- set trigger_ts = sensor_ts -%} | |
{%- else -%} | |
{%- set tdargs = manual_time.split(":") -%} | |
{%- set H = tdargs[0] | int -%} | |
{%- set M = tdargs[1] | int -%} | |
{%- set S = tdargs[2] | int -%} | |
{%- set trigger_ts = ( now().date() + timedelta(H, M, S) ) | as_timestamp | int -%} | |
{%- endif -%} | |
{{- 0 < trigger_ts - now_ts <= 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}}' | |
- condition: template | |
value_template: >- | |
{%- set now_ts = utcnow() | as_timestamp | int -%} | |
{%- set sensor_ts = (states(sensor) | as_timestamp | int) or 0 -%} | |
{%- if sensor_ts != 0 -%} | |
{%- set trigger_ts = sensor_ts -%} | |
{%- else -%} | |
{%- set tdargs = manual_time.split(":") -%} | |
{%- set H = tdargs[0] | int -%} | |
{%- set M = tdargs[1] | int -%} | |
{%- set S = tdargs[2] | int -%} | |
{%- set trigger_ts = ( now().date() + timedelta(H, M, S) ) | as_timestamp | int -%} | |
{%- endif -%} | |
{{- 0 < trigger_ts - now_ts <= 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}}' | |
- >- | |
{%- set now_ts = utcnow() | as_timestamp | int -%} | |
{%- set sensor_ts = (states(sensor) | as_timestamp | int) or 0 -%} | |
{%- if sensor_ts != 0 -%} | |
{%- set trigger_ts = sensor_ts -%} | |
{%- else -%} | |
{%- set tdargs = manual_time.split(":") -%} | |
{%- set H = tdargs[0] | int -%} | |
{%- set M = tdargs[1] | int -%} | |
{%- set S = tdargs[2] | int -%} | |
{%- set trigger_ts = ( now().date() + timedelta(H, M, S) ) | as_timestamp | int -%} | |
{%- endif -%} | |
{{- 0 < trigger_ts - now_ts <= seconds and states(check_entity) in ["unknown", "on", "home"] }} | |
sequence: | |
- delay: '{{tick_time}}' | |
- choose: | |
- conditions: | |
- '{{0 < state_attr(light_entity, "brightness") | int < end_brightness | | |
int}}' | |
- '{{sensor == "none" or as_timestamp(states(sensor)) != None}}' | |
- >- | |
{%- set now_ts = utcnow() | as_timestamp | int -%} | |
{%- set sensor_ts = (states(sensor) | as_timestamp | int) or 0 -%} | |
{%- if sensor_ts != 0 -%} | |
{%- set trigger_ts = sensor_ts -%} | |
{%- else -%} | |
{%- set tdargs = manual_time.split(":") -%} | |
{%- set H = tdargs[0] | int -%} | |
{%- set M = tdargs[1] | int -%} | |
{%- set S = tdargs[2] | int -%} | |
{%- set trigger_ts = ( now().date() + timedelta(H, M, S) ) | as_timestamp | int -%} | |
{%- endif -%} | |
{{ 0 < trigger_ts - now_ts <= seconds }} | |
sequence: | |
- choose: | |
- conditions: | |
- '{{state_attr(light_entity, "min_mireds") != None}}' | |
sequence: | |
- service: light.turn_on | |
data: | |
brightness: >- | |
{%- set now_ts = utcnow() | as_timestamp | int -%} | |
{%- set sensor_ts = (states(sensor) | as_timestamp | int) or 0 -%} | |
{%- if sensor_ts != 0 -%} | |
{%- set trigger_ts = sensor_ts -%} | |
{%- else -%} | |
{%- set tdargs = manual_time.split(":") -%} | |
{%- set H = tdargs[0] | int -%} | |
{%- set M = tdargs[1] | int -%} | |
{%- set S = tdargs[2] | int -%} | |
{%- set trigger_ts = ( now().date() + timedelta(H, M, S) ) | as_timestamp | int -%} | |
{%- endif -%} | |
{{ (float(end_brightness) - (float(range_brightness) * (trigger_ts - now_ts) / float(seconds))) | int }} | |
color_temp: >- | |
{%- set now_ts = utcnow() | as_timestamp | int -%} | |
{%- set sensor_ts = (states(sensor) | as_timestamp | int) or 0 -%} | |
{%- if sensor_ts != 0 -%} | |
{%- set trigger_ts = sensor_ts -%} | |
{%- else -%} | |
{%- set tdargs = manual_time.split(":") -%} | |
{%- set H = tdargs[0] | int -%} | |
{%- set M = tdargs[1] | int -%} | |
{%- set S = tdargs[2] | int -%} | |
{%- set trigger_ts = ( now().date() + timedelta(H, M, S) ) | as_timestamp | int -%} | |
{%- endif -%} | |
{{(float(end_mired) + (float(start_mired) - float(end_mired)) * ((trigger_ts - now_ts) / float(seconds))) | int}} | |
entity_id: !input 'light_entity' | |
default: | |
- service: light.turn_on | |
data: | |
brightness: >- | |
{%- set now_ts = utcnow() | as_timestamp | int -%} | |
{%- set sensor_ts = (states(sensor) | as_timestamp | int) or 0 -%} | |
{%- if sensor_ts != 0 -%} | |
{%- set trigger_ts = sensor_ts -%} | |
{%- else -%} | |
{%- set tdargs = manual_time.split(":") -%} | |
{%- set H = tdargs[0] | int -%} | |
{%- set M = tdargs[1] | int -%} | |
{%- set S = tdargs[2] | int -%} | |
{%- set trigger_ts = ( now().date() + timedelta(H, M, S) ) | as_timestamp | int -%} | |
{%- endif -%} | |
{{ (float(end_brightness) - (float(range_brightness) * (trigger_ts - now_ts) / float(seconds))) | int }} | |
entity_id: !input 'light_entity' | |
- choose: [] | |
default: !input 'post_sunrise_actions' | |
mode: single | |
max_exceeded: silent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment