Tired waking up with a shock from your alarm sound?
Would you like to wake up gently with light?
Do you have smart bulbs with RGB, CCT or just Brightness attributes?
Or even better… Do you have WLED strips with Sunrise effect?
Then I have a script for you…
The script accepts the following parameters:
duration: the duration for the simulation (default: 10minutes)
max_brightness: the maximum brightness that the lamps should turn on (if you don’t need them to turn on to 100%, default: 100)
target:
- area (i.e. Sleeproom) that contains light entities
- group that contains light entities
- or
light.
entities (default: all light entities)
Entities under the target variable will be automatically sorted depending their supporting color modes (RGB / XY / WLED / CCT / Brightness)
If you need to manually sort the lights to match your preference i.e. only CCT if they support RGB, you can use the following variables:
brightness: force these entities to dim only from 0 to max_britghtness given (see bellow)
cct: force these entities to dim and change the color temperature (no need to use it if the entities are supporting only color temperature)
If any of the parameter is not given, the script uses the default values from above.
Example usage:
service: script.< script_name >
data:
duration: 10
target:
- light.sleeproom
- group.sleeproom
- Sleeproom
max_brightness: 100
- Replace script.< script_name > with the entity_id from your home assistant
HOW IT WORKS
It catches all the lights that it finds from the data given and separates them to lights with brightness only, color temperature only, lights with rgb and WLED lights, from their attributes.
The lights with brightness only as color_mode make a transition from 1 to brightness given.
For the lights with CT only it makes a transition from warm white to cool white and from 1% brightness to the brightness given.
For the RGB lights it goes through the colors from the Sunrise palette with a brightness transition.
For the WLED entities the script turns them on with Sunrise effect for the given duration as transition and intensity from the brightness given.
alias: Sunrise Simulator Manual
sequence:
- parallel:
- if:
- condition: template
value_template: "{{ wled | count > 0 }}"
then:
- repeat:
for_each: "{{ wled }}"
sequence:
- service: light.turn_on
data:
effect: Sunrise
target:
entity_id: |
{{ repeat.item }}
- service: number.set_value
data:
value: "{{ [(255 * (maxbri/100)) | int, 255] | min }}"
target:
entity_id: |
{{ device_entities(device_id(repeat.item))
| select('search', 'number.')
| select('search', 'intensity')
| list }}
- service: number.set_value
data:
value: |
{{ [dur, 128] | min }}
target:
entity_id: |
{{ device_entities(device_id(repeat.item))
| select('search', 'number.')
| select('search', 'speed')
| list }}
- if:
- condition: template
value_template: "{{ bright | count > 0 }}"
then:
- repeat:
while:
- condition: template
value_template: |
{{ repeat.index <= maxbri
and not ( repeat.index > 1
and states.light
| selectattr('entity_id', 'in', bright)
| selectattr('state', 'eq', 'on') | list | count
== 0 ) }}
sequence:
- variables:
bright: |
{% if repeat.index > 1 %}
{{ expand(bright)
| selectattr('state', 'eq', 'on')
| map(attribute = 'entity_id')
| list }}
{% else %}
{{ bright }}
{% endif %}
- service: light.turn_on
data:
transition: |
{{ ( ( dur * 60 ) / (maxbri) ) | int }}
brightness_pct: |
{{ [maxbri, ((repeat.index * maxbri) / 100)] | min }}
target:
entity_id: |
{{ bright }}
- delay:
milliseconds: |
{{ ( ( ( dur * 60 ) / (maxbri) ) * 1000
) | int }}
- if:
- condition: template
value_template: "{{ temp | count > 0 }}"
then:
- repeat:
while:
- condition: template
value_template: |
{{ repeat.index <= 100
and not ( repeat.index > 1
and states.light
| selectattr('entity_id', 'in', temp)
| selectattr('state', 'eq', 'on') | list | count
== 0 ) }}
sequence:
- variables:
step: "{{ repeat.index }}"
temp: |
{% if repeat.index > 1 %}
{{ expand(temp)
| selectattr('state', 'eq', 'on')
| map(attribute = 'entity_id')
| list }}
{% else %}
{{ temp }}
{% endif %}
- repeat:
for_each: "{{ temp }}"
sequence:
- service: light.turn_on
data:
transition: |
{{ ( ( dur * 60) / 100 ) | int }}
brightness_pct: |
{{ [ ((step * maxbri) / 100) | int, maxbri] | min }}
target:
entity_id: |
{{ repeat.item }}
- service: light.turn_on
data:
color_temp: >
{% set max = state_attr(repeat.item, 'max_mireds')
%} {% set min = state_attr(repeat.item,
'min_mireds') %} {% set dec = (max-min) / 100 %} {{
iif( step < 100, ( max - (dec * (step - 1) ) ) |
int, min ) }}
target:
entity_id: |
{{ repeat.item }}
- delay:
milliseconds: |
{{ ( ( ( dur * 60 ) / 100 ) * 1000
) | int }}
- if:
- condition: template
value_template: "{{ color | count > 0 }}"
then:
- repeat:
while:
- condition: template
value_template: |
{{ repeat.index <= 100
and not ( repeat.index > 1
and states.light
| selectattr('entity_id', 'in', color)
| selectattr('state', 'eq', 'on') | list | count
== 0 ) }}
sequence:
- variables:
color: |
{% if repeat.index > 1 %}
{{ expand(color)
| selectattr('state', 'eq', 'on')
| map(attribute = 'entity_id')
| list }}
{% else %}
{{ color }}
{% endif %}
- service: light.turn_on
data:
transition: |
{{ ( ( dur * 60 ) / 100 ) | int }}
brightness_pct: >
{{ [((repeat.index * maxbri) / 100) | int , maxbri] | min
}}
target:
entity_id: |
{{ color }}
- service: light.turn_on
data:
rgb_color: |
{% if repeat.index <= 20 %}
[255,0,0]
{% elif repeat.index <= 40 %}
[255,77,0]
{% elif repeat.index <= 60 %}
[255,103,0]
{% elif repeat.index <= 80 %}
[255,129,0]
{% else %}
[255,167,0]
{% endif %}
target:
entity_id: |
{{ color }}
- delay:
milliseconds: |
{{ ( ( ( dur * 60 ) / 100 ) * 1000 ) | int }}
variables:
dur: |
{{ duration | default(10, true) | int(10) }}
entities: |
{% if target is defined %}
{% if target in states.light
| rejectattr('state','in', ['unavailable', 'unknown', None])
| selectattr('attributes.supported_color_modes', 'defined')
| rejectattr('attributes.supported_color_modes', 'eq', None)
| map(attribute='entity_id')
| map('area_name')
| list %}
{% set target = area_entities(target) %}
{% endif %}
{{ expand(target)
| selectattr('domain', 'eq', 'light')
| selectattr('attributes.supported_color_modes', 'defined')
| rejectattr('attributes.supported_color_modes', 'eq', None)
| rejectattr('state', 'in', ['unavailable', 'unknown', None])
| map(attribute='entity_id')
| list }}
{% elif cct is not defined
and brightness is not defined %}
{{ states.light
| rejectattr('state','in', ['unavailable', 'unknown', None])
| selectattr('attributes.supported_color_modes', 'defined')
| rejectattr('attributes.supported_color_modes', 'eq', None)
| map(attribute='entity_id')
| list }}
{% endif %}
bright: |
{{ expand(entities)
| selectattr('attributes.supported_color_modes', 'eq', [ 'brightness'])
| map(attribute='entity_id')
| list + expand( brightness | default([], true) )
| rejectattr('state', 'in', [ 'unavailable', 'unknown', None ])
| rejectattr('entity_id', 'in', entities)
| selectattr('attributes.supported_color_modes', 'defined')
| selectattr('attributes.supported_color_modes', 'search', 'brightness')
| map(attribute='entity_id')
| list }}
temp: |
{{ expand(entities)
| selectattr('attributes.supported_color_modes', 'eq', [ 'color_temp'])
| map(attribute='entity_id')
| list + expand( cct | default([], true) )
| rejectattr('state', 'in', [ 'unavailable', 'unknown', None ])
| rejectattr('entity_id', 'in', entities)
| selectattr('attributes.supported_color_modes', 'defined')
| selectattr('attributes.supported_color_modes', 'search', 'color_temp')
| map(attribute='entity_id')
| list }}
wled: |
{{ integration_entities('wled')
| select('in', expand(entities)
| selectattr('attributes.effect_list', 'defined')
| selectattr('attributes.effect_list', 'search', 'Sunrise')
| map(attribute='entity_id')
| list)
| list }}
color: |
{{ entities | reject('in', temp + wled + bright) | list }}
maxbri: >
{% set bri = max_brightness | default(100, true) | int(100) %} {{ bri if bri
< 100 else 100 }}
mode: single
Last Edit-Update Script: 24/10/2023
-
BREAKING CHANGE:
max_brightness is the variable to enter for the maximum brightness that the lights should turn on -
NEW FEATURE:
new variables:
brightness:
entities that should be forced to only dim from 0 to max_brightness given
cct:
entities that should be forced to change their color temperature and brightness
Edit: 14/10/2023
- Ignore entities that turned off during script actions
- Continue adjusting rgb color / color temperature after max brightness reached
Edit: 13/10/2023
- Handle color_temp seperate for every entity based in its min/max values
- Support entities with brightness (dimmable lights only)
- Support WLED lightstrips with Sunrise effect