Github gist: Sleep trainer blueprint for Home-Assistant (github.com)
This is a blueprint to create a ‘sleep trainer’ for smaller children who can’t tell time yet. It allows you to set a time at which the configured light(s) will be turned green, indicating it is OK to get out of bed.
15 minutes before the light turns green it will turn purple, to indicate that it is almost time to get up.
Other than that a start time and stop time can be configured and the brightness for the light can be set.
The times to get up can be inserted using input_datetime entities. These can easily be configured in the front end if you want to change the time the light should turn green.
Blueprint code
Import this blueprint using the forum topic, or click the badge below if you are running HA 2021.3.1 or higher.
blueprint:
name: Sleep trainer light using time and RGB light
description: 'Creates a sleep trainer using an RGB(w) light to
indicate if it is time to get out of bed or not.
Usefull for smaller children who cant tell time yet.
At the start time the light is turned on red.
15 minutes before the wake up time the light is changed to purple-ish
At the configured wake up time the light is changed to green.
At the stop time the light is turned off again.
As target several options are available to choose from.
'
domain: automation
input:
enable_trainer_light:
name: Enable light
description: Enable the actions of the automation to set the ligtht's state
default: 'on'
selector:
entity:
domain: input_boolean
time_start:
name: Time to start
description: Time to turn on the light red before it's OK to get up
default: "05:30:00"
selector:
time:
time_stop:
name: Time to stop
description: Time to turn off the light
default: "09:00:00"
selector:
time:
time_monday:
name: Monday time
description: Time to get up on Mondays
default: "07:00:00"
selector:
entity:
domain: input_datetime
time_tuesday:
name: Tuesday time
description: Time to get up on Tuesdays
default: "07:00:00"
selector:
entity:
domain: input_datetime
time_wednesday:
name: Wednesday time
description: Time to get up on Wednesdays
default: "07:00:00"
selector:
entity:
domain: input_datetime
time_thursday:
name: Thursday time
description: Time to get up on Thursdays
default: "07:00:00"
selector:
entity:
domain: input_datetime
time_friday:
name: Friday time
description: Time to get up on Fridays
default: "07:00:00"
selector:
entity:
domain: input_datetime
time_saturday:
name: Saturday time
description: Time to get up on Saturdays
default: "07:00:00"
selector:
entity:
domain: input_datetime
time_sunday:
name: Sunday time
description: Time to get up on Sundays
default: "07:00:00"
selector:
entity:
domain: input_datetime
target_light:
name: Light
description: Light entity to use
selector:
target:
entity:
domain: light
brightness:
name: Brightness
description: Brightness of the light(s) when turning on
default: 10
selector:
number:
min: 0.0
max: 100.0
mode: slider
step: 1.0
unit_of_measurement: '%'
mode: queued
max_exceeded: silent
trigger:
- platform: time
at:
- !input time_start
- !input time_stop
- !input time_monday
- !input time_tuesday
- !input time_wednesday
- !input time_thursday
- !input time_friday
- !input time_saturday
- !input time_sunday
- platform: homeassistant
event: start
variables:
timestart: !input 'time_start'
timestop: !input 'time_stop'
timemonday: !input 'time_monday'
timetuesday: !input 'time_tuesday'
timewednesday: !input 'time_wednesday'
timethursday: !input 'time_thursday'
timefriday: !input 'time_friday'
timesaturday: !input 'time_saturday'
timesunday: !input 'time_saturday'
condition:
- condition: state
entity_id: !input enable_trainer_light
state: 'on'
action:
- choose:
- conditions:
- condition: time
after: !input time_stop
sequence:
- service: light.turn_off
target: !input target_light
data:
transition: 2
- conditions:
- condition: template
value_template: >
{% set cdate = now().strftime("%Y-%m-%d ") %}
{% set ctime = (now().timestamp() + 1) | int %}
{% if now().weekday() in (0,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timemonday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (1,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timetuesday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (2,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timewednesday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (3,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timethursday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (4,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timefriday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (5,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timesaturday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (6,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timesunday), '%Y-%m-%d %H:%M:%S')) | int %}
{% endif %}
{% set starttime = as_timestamp(strptime(cdate + timestart, '%Y-%m-%d %H:%M:%S')) | int %}
{{ starttime < ctime < wakeuptime }}
sequence:
- service: light.turn_on
target: !input target_light
data:
transition: 2
color_name: 'red'
brightness_pct: !input brightness
- wait_template: >
{% set cdate = now().strftime("%Y-%m-%d ") %}
{% if now().weekday() in (0,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timemonday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (1,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timetuesday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (2,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timewednesday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (3,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timethursday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (4,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timefriday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (5,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timesaturday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (6,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timesunday), '%Y-%m-%d %H:%M:%S')) | int %}
{% endif %}
{% set curTime = as_timestamp(strptime(cdate + states('sensor.time'), '%Y-%m-%d %H:%M')) | int %}
{{ curTime > (wakeuptime - (60 * 15)) }}
- service: light.turn_on
target: !input target_light
data:
transition: 2
rgb_color: [255,0,170]
brightness_pct: !input brightness
- conditions:
- condition: template
value_template: >
{% set cdate = now().strftime("%Y-%m-%d ") %}
{% set ctime = (now().timestamp() + 1) | int %}
{% if now().weekday() in (0,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timemonday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (1,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timetuesday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (2,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timewednesday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (3,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timethursday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (4,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timefriday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (5,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timesaturday), '%Y-%m-%d %H:%M:%S')) | int %}
{% elif now().weekday() in (6,) %}
{% set wakeuptime = as_timestamp(strptime(cdate + states(timesunday), '%Y-%m-%d %H:%M:%S')) | int %}
{% endif %}
{% set stoptime = as_timestamp(strptime(cdate + timestop, '%Y-%m-%d %H:%M:%S')) | int %}
{{ wakeuptime < ctime < stoptime }}
sequence:
- service: light.turn_on
target: !input target_light
data:
transition: 2
color_name: 'green'
brightness_pct: !input brightness
Changelog
- 2022-09-27: Remove deprecated
white_value
fromlight.turn_on
service calls - 2021-06-11: Update descriptions to indicate use of entity_id’s instead of direct input.
- 2021-03-20: Initial version