Skip to content

Instantly share code, notes, and snippets.

@sbyx
Last active October 10, 2024 06:49
Show Gist options
  • Save sbyx/96c43b13b90ae1c35b872313ba1d2d2d to your computer and use it in GitHub Desktop.
Save sbyx/96c43b13b90ae1c35b872313ba1d2d2d to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Wake-up light alarm with sunrise effect
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
@jLynx
Copy link

jLynx commented Jul 10, 2021

I am getting this error:

Stopped because an error was encountered at July 10, 2021, 5:30:00 PM (runtime: 0.01 seconds)

In 'template' condition: TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'

image

@jrsecor
Copy link

jrsecor commented Nov 5, 2021

I am getting this error:

Stopped because an error was encountered at July 10, 2021, 5:30:00 PM (runtime: 0.01 seconds)

In 'template' condition: TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'

image

Did you figure out this error, I have the same happening

@jeffremps
Copy link

jeffremps commented Nov 17, 2021

@jrsecor
I have been struggling with the same issue. I finally read the documentation again and realized I had missed a key step. This script requires a few sensors to be added to the configuration.
https://community.home-assistant.io/t/wake-up-light-alarm-with-sunrise-effect/255193#prerequisites-1

Once I added these, and restarted HA, the blueprint started to work.

sensor:
  - platform: time_date
    display_options:
      - 'date'
      - 'date_time_iso'

@B-Kramer
Copy link

New error with 2012.12.7
2021-12-29 15:40:00 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'as_timestamp' got invalid input 'unavailable' when rendering 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']}}' but no default was specified. Currently 'as_timestamp' will return 'None', however this template will fail to render in Home Assistant core 2022.1

@djmitho
Copy link

djmitho commented Mar 30, 2022

##improvement:
Is it possible to add xy color transition before the color temperature adjustment?
In example, with a RGBW bulb, at first red to orange color and second warmer to white temperature color.
As RGB leds gets lower brightness than W led, as i was testing, may it be red 1% brightness to orange 100% brightness. And then 1% brightness warmer temperature to 100% of coldest temperature.
What do you think?
Great work ;)

@Sangoku
Copy link

Sangoku commented Aug 18, 2022

This template breaks randomly and starts to use the default value to turn randomly on.

This template part is failing when there is no next alarm set and the default value is used.

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']}}

This should not happen:
there is no alarm set for the sensor and i dont want to be woken up every day at fixed time!
image

@rosetown-kelly
Copy link

@sbyx , I am getting the following error when using this blueprint:

Stopped because an error was encountered at September 21, 2022 at 9:40:00 PM (runtime: 0.05 seconds)

In 'template' condition: ValueError: Template error: as_timestamp got invalid input 'unknown 21:45:00' when rendering 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']}}' but no default was specified

Any chance you'd be able to help?

@deathbybandaid
Copy link

deathbybandaid commented Oct 21, 2022

I'm new to HA, and transitioning from a Hue hub, and this blueprint makes this possible (with caveats)!

  1. I'm using some helpers for setting the time, but the blueprint can't use the helpers. I had to work around this in a weird way
    https://community.home-assistant.io/t/wake-up-light-alarm-with-sunrise-effect/255193/305

  2. Any chance this could take multiple Additional entity to check before sunrise is triggered ? This would allow much more flexibility with whether or not it should run.

  3. Any chance this could be modified to have a manual_sunrise_duration, with an if/then for the input from a number helper?

I'd like to be able to make my dashboard easier for my wife to mentally digest.

Right now I have

image

But I'd like her to be able to adjust the duration from here, as well as I'd have the ability to cleanly do some time math to indicate what time the lights will start based on the duration.

@Erfa
Copy link

Erfa commented Jan 26, 2023

This is great, but doesn't work well with snooze for me. If I have a sunrise time of 30 minutes, snoozing 5 minutes causes weird things to happen, i.e. it tries to turn on the brightness again. Would it be possible to have it ignore alarms that are too soon? I don't need it to act on any alarm that is less than the sunrise length into the future. I've tried to figure out how it works and implement it myself, but I don't understand all the script magic that is happening...

@ALotOfMooh
Copy link

Being able to use rgb lights by starting with red at the beginning would be great!!

@mverburg
Copy link

mverburg commented Feb 6, 2023

Hi @sbyx, thanks for sharing this automation. As a newbie at home assistent I was wondering if it where possible to run this automation for more than one alarm.

It so happens that we have more than one person in home in dire need of getting all possible assistance waking up. I've tried to run two instances of this blueprint but since the mode is single the execution seems to stop.

The simple solution would be to change the mode to parallel but I'm not sure if that would cause problems executing this blueprint.

Any thoughts?

/MaartenV

@mverburg
Copy link

mverburg commented Feb 8, 2023

It so happens that we have more than one person in home in dire need of getting all possible assistance waking up. I've tried to run two instances of this blueprint but since the mode is single the execution seems to stop.

So, taking some time to actually read the documentation I understand that this is probably not the root cause of my problem. I'll do some experimenting this weekend to resolve the problem I had (or thought I had).

@Micha-Btz
Copy link

@Erfa Did you find a soluion for the snooze?

@mverburg
Copy link

This is great, but doesn't work well with snooze for me. If I have a sunrise time of 30 minutes, snoozing 5 minutes causes weird things to happen, i.e. it tries to turn on the brightness again. Would it be possible to have it ignore alarms that are too soon? I don't need it to act on any alarm that is less than the sunrise length into the future. I've tried to figure out how it works and implement it myself, but I don't understand all the script magic that is happening...

I also came across this issue and I think it's kind of logical. When you snooze your device stops the current alarm and sets a new one 5 minutes later (snooze time). Then when the blueprint triggers the next minute it detects the new alarm and sets the light to the brightness value appropriate for 5 minutes before the alarm will go off.

I've solved it by adding a post-sunrise action in the form of a 25 minute delay. That way, when the alarm goes off the first time, the automation will keep running and because the automation will only restart if the current execution is finished. So if you snooze more than 5 times (5 snoozes of 5 minutes each) you will probably see the stuttering light again.

@hwinkel
Copy link

hwinkel commented Oct 4, 2023

in regards to use RGB Lights with Red, Orange, Warmwhite, White Sequence, it seems another Project has done this calculation already:

https://community.home-assistant.io/t/sunrise-simulator-wake-up-gently-to-light/493778

@rednas201
Copy link

The reason could be that that I updated my home assistant recently, but currently this blueprint doesn't work anymore for me.
It keeps waiting at the second wait sequence.
image

When I debug the lines of code and simplify it a bit, I see it has to do with the timezone
image

In the screenshot, I set the next alarm at 23.20 and the current time is 23:06. With a sunrise time of 20 minutes, the value should be true already.
When I subtract the next alarm time and current time, I end up with a negative number. This is due to the fact that the next alarm time is not converted to my timezone (+2) when it is converted to a timestamp.
With my limited programming knowledge I tried everything to make the conversion right, but I didn't manage yet.
Oddly enough that this has worked in the past....
Anyone any idea on how to solve this?

@MoritzTockner
Copy link

The reason could be that that I updated my home assistant recently, but currently this blueprint doesn't work anymore for me. It keeps waiting at the second wait sequence. image

When I debug the lines of code and simplify it a bit, I see it has to do with the timezone image

In the screenshot, I set the next alarm at 23.20 and the current time is 23:06. With a sunrise time of 20 minutes, the value should be true already. When I subtract the next alarm time and current time, I end up with a negative number. This is due to the fact that the next alarm time is not converted to my timezone (+2) when it is converted to a timestamp. With my limited programming knowledge I tried everything to make the conversion right, but I didn't manage yet. Oddly enough that this has worked in the past.... Anyone any idea on how to solve this?

Same problem for me.

@fuxifex
Copy link

fuxifex commented Jan 20, 2024

Same for me .. stopped working since the last HA update :(

In 'template' condition: ValueError: Template error: as_timestamp got invalid input 'unknown' when rendering 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']}}' but no default was specified

Has anyone managed to repair it themselves?

@ChristophHandschuh
Copy link

ChristophHandschuh commented Jan 20, 2024

FIXED the error!
Heres the fixed Blueprint. Works only with sensor. Manual timesetting is not supported.

Change this part of the code:

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(now()) <= float(seconds)
    and states(check_entity) in [''unknown'', ''on'', ''home'']}}'
  • choose: []

basically changes as_timestamp(states('sensor.date_time_iso') to: as_timestamp(now())

@triplepoint
Copy link

triplepoint commented Jan 21, 2024

I don't understand why this blueprint uses this idiom of states("sensor.date") ~ " " ~ manual_time to construct a string for "today at the manual_time time".

today_at(manual_time) returns an equivalent value, except that it has the timezone offset attached. But since this blueprint is always using these strings as parameters for as_timestamp(), these are all equivalent:

{{ as_timestamp(states("sensor.date") ~ " " ~ "07:01:00") }}
{{ as_timestamp(now().date() ~ " " ~ "07:01:00") }}
{{ as_timestamp(today_at("07:01:00")) }}
1705762860.0
1705762860.0
1705762860.0

I'm testing a variant that doesn't need sensor.date and sensor.date_time_iso, have a look at https://gist.github.com/triplepoint/1b9006ddec208a07fc0100a2a30110eb

@rednas201
Copy link

@triplepoint Works for me! I owe you a big thanks!! My setup is working again.

@Delvint
Copy link

Delvint commented Aug 8, 2024

The reason why this querqy manual time computation is needed is the mqtt automation over Sleep As Android.

The manual time stamp format seems deprected or whatever. That is the reason why some mentioned defaulting to 0 would solve the problem. But it does not really solve it. I could by adding a markup for the time string to begin:
{{ states(''sensor.date'') ~ '' '' ~ manual_time }}
{{ states(''sensor.date'') ~ ''T'' ~ manual_time }}

The two time stamp computation produce different outputs.
{{ as_timestamp(states('sensor.date_time_iso')) }} # 1723102440.0
{{ as_timestamp(now()) }} # 1723098900.217016
I dont know why both are used. But I replaced every occurence of now() with states('sensor.date_time_iso') and it worked.

Have a look at: https://gist.github.com/Delvint/a4203ab90ec3000c590acb7bf16f6d86

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