Hi,
Based on this post Phillips Hue Wakeup Light with fade-in I’ve made several adjustments to work with my lights (as only Hue and Lifx platforms accept transition).
I use MiLight but served by a NodeMCU running this project https://github.com/sidoh/esp8266_milight_hub, not the ibox. Basically you can have as many groups as you want and loose the ibox. The performance is much better and on the gateway can be created as many lights and groups as you want (instead of buying an ibox for each group of four lights) thus limiting costs to a single NodeMCU and a NRF24L01.
The scripts for fade-in should work on any light platform that can be dimmed.
Apart from transition I’ve made an input select (not limiting thus to a single light). There is a light named light.all_lights which is a group, however not in HA but a group made in MiLight gateway (as turning a group of lights in HA would set them individually). Also, there are input numbers for maximum brightness and transition time in seconds (this is only intermediary as, depending on the the total time for the fade-in, this would be adjusted to hurry-up or slow-down the transitions due to the fact that brightness can be served to bulbs only as integer).
When using with MiLight (probably also with other platforms) an automation should set the brightness to 1 before the alarm is set during the evening (after: sunset) / night (before: sunrise) as otherwise the light would start at the last known brightness level. Setting the brightness to 1 when the light turn all time has the disadvantage of flickering but using the input boolean “Enable wakeup light” as condition this can be limited only to the last hour/hours before sleep (this is not covered here).
My configuration is split into individual folders for each component so indentation needs to be amended if using a single configuration file (although I strongly advise splitting it).
#1. Automation
- alias: 'Wakeup Light'
trigger:
platform: template
value_template: '{{ states.sensor.time.state == states.sensor.wakeup_start_time_lights.state }}'
condition:
condition: and
conditions:
- condition: state
entity_id: input_boolean.wakeup
state: 'on'
- condition: or
conditions:
- condition: state
entity_id: input_boolean.wakeup_weekends
state: 'on'
- condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
action:
- service: script.wakeup_room
#2. Sensors
- platform: template
sensors:
wakeup_alarm_time:
friendly_name: 'Alarm time'
value_template: '{% if states.input_number.wakeup_hour.state|length == 3 %}0{% endif %}{{ states.input_number.wakeup_hour.state | int }}:{% if states.input_number.wakeup_minutes.state|length == 3 %}0{% endif %}{{ states.input_number.wakeup_minutes.state | int }}'
- platform: template
sensors:
wakeup_fadein_duration:
friendly_name: 'Fade-in duration'
value_template: '{{ states.input_number.wakeup_duration.state | int }}'
unit_of_measurement: 'min'
- platform: template
sensors:
wakeup_start_time_lights:
friendly_name: 'Fade-in start time'
value_template: >
{% if states.sensor.wakeup_alarm_time and states.input_number.wakeup_duration %}
{% set alarmtime = states.sensor.wakeup_alarm_time.state %}
{% set alarm_hour = alarmtime.split(":")[0] | int %}
{% set alarm_min = alarmtime.split(":")[1] | int %}
{% set light_dur = states.input_number.wakeup_duration.state | int %}
{% set alarm_min_light = alarm_min - light_dur %}
{% if alarm_min_light < 0 %}
{% set alarm_min_light = alarm_min_light + 60 %}
{% set alarm_hour_light = alarm_hour - 1 %}
{% if alarm_hour_light < 0 %}{% set alarm_hour_light = 23 %}{% endif %}
{% if alarm_hour_light < 10 %}0{% endif %}{{ alarm_hour_light}}:{% if alarm_min_light < 10 %}0{% endif %}{{ alarm_min_light }}
{% else %}
{% if alarm_hour < 10 %}0{% endif %}{{ alarm_hour }}:{% if alarm_min_light < 10 %}0{% endif %}{{ alarm_min_light }}
{% endif %}
{% endif %}
#replace with own light ids
- platform: template
sensors:
wakeup_light_current_brightness:
friendly_name: 'Light brightness'
value_template: >
{% if is_state('input_select.wakeup_light_id', 'light.light1') %}
{{states.light.light1.attributes.brightness | int}}
{% elif is_state('input_select.wakeup_light_id', 'light.light2') %}
{{states.light.light2.attributes.brightness | int}}
{% elif is_state('input_select.wakeup_light_id', 'light.light3') %}
{{states.light.light3.attributes.brightness | int}}
#add the other lights
{% else %}
0
{% endif %}
- platform: template
sensors:
wakeup_adjusted_transition:
friendly_name: 'Adjusted transition'
value_template: >
{% if states.sensor.wakeup_light_current_brightness.state|int < ((((states.input_number.wakeup_max_brightness.state|int/(states.input_number.wakeup_max_brightness.state|int*states.input_number.transition.state|int/(states.input_number.wakeup_duration.state|int*60))|round)|round)*(states.input_number.transition.state | int + 1)) -states.input_number.wakeup_duration.state|int*60)*(((states.input_number.wakeup_max_brightness.state|int)*(states.input_number.transition.state | int))/(states.input_number.wakeup_duration.state | int * 60))|round %}
{{states.input_number.transition.state|int-([states.input_number.transition.state|int-(states.input_number.wakeup_duration.state|int*60/(states.input_number.wakeup_max_brightness.state|int/((states.input_number.wakeup_max_brightness.state|int*states.input_number.transition.state|int/states.input_number.transition.state|int)|round)|round)|round),0]|max)}}
{% else %}
{{states.input_number.transition.state|int-([states.input_number.transition.state|int-(states.input_number.wakeup_duration.state|int*60/(states.input_number.wakeup_max_brightness.state|int/((states.input_number.wakeup_max_brightness.state|int*states.input_number.transition.state|int/states.input_number.transition.state|int)|round)|round)|round),0]|max) + 1}}
{% endif %}
#3. input boolean
wakeup:
name: 'Enable wakeup light'
icon: mdi:power
initial: off
wakeup_weekends:
name: 'Wakeup weekends'
icon: mdi:power
initial: off
#4. input number
wakeup_hour:
name: Hour
icon: mdi:clock-in
initial: 7
min: 0
max: 23
step: 1
wakeup_minutes:
name: Minutes
icon: mdi:clock-in
initial: 30
min: 0
max: 55
step: 5
wakeup_duration:
name: Light fade-in duration
icon: mdi:clock-in
initial: 20
min: 5
max: 60
step: 5
wakeup_max_brightness:
name: Maximmum brightness
icon: mdi:white-balance-sunny
initial: 225
min: 15
max: 255
step: 17
transition:
name: Transition in seconds
icon: mdi:clock-in
initial: 10
min: 1
max: 60
step: 1
#5. input select
wakeup_light_id:
name: Light id
options:
- light.all_lights
- light.light1
- light.light2
- light.light3
#add the other lights
initial: light.light1
icon: mdi:weather-sunset-up
#6. scripts
wakeup_room_initial:
alias: 'Wakeup initial'
sequence:
- service: light.turn_on
data_template:
entity_id: '{{ states.input_select.wakeup_light_id.state}}'
brightness: 0
rgb_color: [1,1,1]
wakeup_room_turnon:
alias: 'Wakeup turnon'
sequence:
- service: script.turn_off
entity_id: script.wakeup_room_iterate
- service: light.turn_on
data_template:
entity_id: '{{ states.input_select.wakeup_light_id.state}}'
brightness: '{{ states.sensor.wakeup_light_current_brightness.state|int + (((states.input_number.wakeup_max_brightness.state|int)*(states.input_number.transition.state | int))/(states.input_number.wakeup_duration.state | int * 60))|round }}'
rgb_color: [1,1,1]
- delay: '00:00:{{states.sensor.wakeup_adjusted_transition.state|int}}'
- service: script.turn_on
entity_id: script.wakeup_room_iterate
wakeup_room_iterate:
alias: 'Wakeup iterate'
sequence:
- service: script.turn_off
entity_id: script.wakeup_room_turnon
- service_template: >
{% if states.sensor.wakeup_light_current_brightness.state|int < states.input_number.wakeup_max_brightness.state|int %}
script.turn_on
{% else %}
script.turn_off
{% endif %}
data:
entity_id: script.wakeup_room_turnon
wakeup_room:
alias: 'Wakeup'
sequence:
- service: script.turn_on
entity_id: script.wakeup_room_initial
- delay: '00:00:{{states.input_number.transition.state|int-([states.input_number.transition.state|int-(states.input_number.wakeup_duration.state|int*60/(states.input_number.wakeup_max_brightness.state|int/((states.input_number.wakeup_max_brightness.state|int*states.input_number.transition.state|int/states.input_number.transition.state|int)|round)|round)|round),0]|max)}}'
- service: script.turn_on
entity_id: script.wakeup_room_turnon