Skip to content

Instantly share code, notes, and snippets.

@akloeckner
Last active March 19, 2024 09:50
Show Gist options
  • Save akloeckner/6f5d3299b5661ef1830171db1957b1a0 to your computer and use it in GitHub Desktop.
Save akloeckner/6f5d3299b5661ef1830171db1957b1a0 to your computer and use it in GitHub Desktop.
Wake and sleep light (Home Assistant Automation Blueprint)
blueprint:
name: Wake and sleep light
description: |
If a light is turned on in a very dim state, do one of two actions:
a) raise its brightness in steps up to maximum brightness in a given time frame, or
b) set it to its minimum brightness.
Turning the light to its maximum brightness or off will stop the actions.
source_url: https://gist.github.com/akloeckner/6f5d3299b5661ef1830171db1957b1a0
domain: automation
input:
light_entity:
name: Light entity
description: The light to control
selector:
entity:
domain: light
wake_time_start:
name: Start of the wake-up time frame
description: The beginning of the time frame, where the automation should behave as a wake-up light
default: '07:00:00'
selector:
time: {}
wake_time_end:
name: End of the wake-up time frame
description: The end of the time frame, where the automation should behave as a wake-up light
default: '10:00:00'
selector:
time: {}
trigger_brightness:
name: Trigger brightness
description: The brightness below which this automation will trigger
default: 50
selector:
number:
min: 1.0
max: 255.0
step: 1.0
mode: slider
sleep_brightness:
name: Sleep brightness
description: The brightness so set when used as a sleep light
default: 25
selector:
number:
min: 1.0
max: 255.0
step: 1.0
mode: slider
max_brightness:
name: Maximum brightness
description: The ultimate brightness to turn the wake-up light to
default: 255
selector:
number:
min: 1.0
max: 255.0
step: 1.0
mode: slider
wake_duration:
name: Wake-up duration
description: The timespan in which to reach full brightness in minutes
default: 5
selector:
number:
min: 1.0
max: 30.0
step: 1.0
mode: slider
mode: single
max_exceeded: silent
variables:
light_entity: !input 'light_entity'
trigger_brightness: !input 'trigger_brightness'
max_brightness: !input 'max_brightness'
wake_duration: !input 'wake_duration'
step: 5
trigger:
- platform: state
entity_id: !input 'light_entity'
from: 'off'
to: 'on'
for: 5
condition:
- condition: template
value_template: "{{ is_state(light_entity, 'on') and state_attr(light_entity, 'brightness') < float(trigger_brightness) }}"
action:
- alias: "Set up runtime variables"
variables:
start_brightness: "{{ state_attr(light_entity, 'brightness') }}"
start_time: "{{ as_timestamp(now()) }}"
wake_gradient: "{{ (max_brightness-start_brightness) / float(wake_duration) / 60 }}"
wake_tick: "{{ float(step) / wake_gradient }}"
- alias: "Turn up brightness in the morning, else set minimum brightness"
choose:
conditions:
- condition: time
after: !input 'wake_time_start'
before: !input 'wake_time_end'
sequence:
- repeat:
while:
- "{{ is_state(light_entity, 'on') and state_attr(light_entity, 'brightness') < float(max_brightness) }}"
sequence:
- service: light.turn_on
entity_id: !input 'light_entity'
data_template:
#brightness: "{{ (state_attr(light_entity, 'brightness')+float(step)) | min(float(max_brightness)) }}"
brightness: "{{ start_brightness + (as_timestamp(now())-start_time) * wake_gradient }}"
- delay: "{{ wake_tick }}"
default:
- service: light.turn_on
entity_id: !input 'light_entity'
data:
brightness: !input 'sleep_brightness'
@akloeckner
Copy link
Author

akloeckner commented Oct 3, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment