OK to Wake & Fade to Sleep Light

So we made a mistake with our young son awhile back telling him it wasn’t OK to get out of bed in the morning until the sun was up. Fast forward to the spring/summer months and the sun is up in the 5:00 a.m. hour and we are paying for past choices.

Our son is learning his numbers and can identify them, however, telling time isn’t a bridge we have crossed yet. Colors however, he does well.

A number of friends have had good luck with products like this OK to Wake Light. Of course homeassistant seemed like a better solution to me and could allow dynamic wake up times and easy remote changing of parameters!

Materials:

This could be done with just about any light bulb or other light that can do color and can be connected to Home Assistant. These are just the specific materials I used.

I picked up a WiFi RGBW LED Controller and a RGBW LED strip off Amazon. The RGBW is important versus a RGB as it has a separate white channel. The RGB strips, IMO, aren’t great at creating white light. We wanted to use this as a reading light also so having the white option was important. You will need a 12V power supply also (I had a spare laying around).

I had previously picked up some Aluminum LED Channel/Diffuser and use this to mount the lights to his bed. The controller is stuck on the back of his bed with the supplied adhesive strip.

Optional: I use a Kuled K36 switch flashed with Tasmota for the main room light. The double tap feature in Tasmota was used to control the LED strip.

Setup and HomeAssitant Integration

The LED controller works with the Flux LED integration:

My setup for the light is as follows:

light:
  - platform: flux_led
    devices:
      192.168.1.4:
        name: Mikey's Bed Lights
        mode: "rgbw"

Automations:

Reading Light:

The first automation is a simple one to turn on the LED strip at night when the main wall switch in his room is given a double tap.

  - alias: "Kuled3 Double Tap"
    initial_state: true
    trigger:
      platform: mqtt
      topic: cmnd/kuledbutton3/POWER
      payload: "TOGGLE"
    action:
      - service_template: >-
          {%- if is_state('light.mikey_s_bed_lights' , 'off') -%}
            script.readinglight
          {%- else -%}
            script.fadebedlightoff
          {%- endif -%}

The subsequent script that is called if the LED strip is off is just a specific color to allow a warmer white light. The LED strip has a pretty cool white LED. It calls the same service twice as it sometimes doesn’t like to change the color LED and the white LED at the same time.

  readinglight:
    sequence:
      - service: light.turn_on
        entity_id: light.mikey_s_bed_lights
        data:
          rgb_color: [255,100,0]
          brightness: 255
          white_value: 255
      - delay: '00:00:01' 
      - service: light.turn_on
        entity_id: light.mikey_s_bed_lights
        data:
          rgb_color: [255,100,0]
          brightness: 255
          white_value: 255

Fade to Sleep Light:

Another double tap on the wall switch when we leave the room triggers the automation from the reading light info above. However, since the LED strip is already on, it triggers a different script to slowly fade the LED strip off over ~20 minutes.

This is a super long script due to the capabilities of my LED strip controller and HA combined. It basically takes the current level of the RGB and White LED and subtracts ~10% every two minutes in the beginning and less as time goes on to make the change more gradual. It accounts for the light being any color and if the RGB or White LED is already off or very low and sends an 0 value as negative numbers will throw an error.

The script is really long so I will just link to it on my GitHub:

I think this could be done ALOT easier with the transistion function available, however it doesn’t work with my lights. If are using an RGB only strip, the brightness_step options are likely also easier.

Morning Time/ OK to Wake

At 5:00 a.m the light strip turns on a very low red. If he wakes up and sees red, he knows to go back to sleep.

  - alias: "OK to Wake Red"
    initial_state: true  
    trigger:
      platform: time
      at: '05:00'
    action:
      service: light.turn_on
      data:
        entity_id: light.mikey_s_bed_lights
        brightness: 5
        rgb_color: [255,0,0]
        white_value: 0

At 7:00 the light turns green and lets him know it is OK to get out of bed.

  - alias: "OK to Wake Green"
    initial_state: true  
    trigger:
      platform: time
      at: '07:00'
    action:
      service: light.turn_on
      data:
        entity_id: light.mikey_s_bed_lights
        brightness: 5
        rgb_color: [0,255,0]
        white_value: 0

At 8:00 a final automation turns off the light. He is always up by this point.

  - alias: "OK to Wake Off"
    initial_state: true  
    trigger:
      platform: time
      at: '08:00'
    action:
      service: light.turn_off
      data:
        entity_id: light.mikey_s_bed_lights

Teaching your Kid:

One of the biggest parts of this is telling your kid how this all works. They need to be able to identify colors and understand what each of them means in the morning. Also explain that lights are going to fade off by themselves at night. My son really loves having the light and understood it from the start. He did request that the light be able to be any color at night while it fades to off. This required some revision of the script but works now.

Future Plans:

I want to make the morning times dynamic. I think it would be nice to have different wake up times based on different needs. When he is back in school he may need to wake up earlier or on the weekends it could be nice to sleep in a bit. I think this is very possible, I just need to take the time to figure out.

1 Like

This was easy with the help of this post.

I created three input datetimes

input_datetime.green_light_time
input_datetime.red_light_time
input_datetime.ok_to_wake_off

Then adjusted the automations to trigger off these helpers.

#########################################################
#                                                       #
#                    OK TO WAKE - RED                   #
#                                                       #
#########################################################

  - alias: "OK to Wake Red"
    initial_state: true  
    trigger:
      platform: template
      value_template: "{{ states('sensor.time') == states('input_datetime.red_light_time').rsplit(':',1)[0] }}"
    condition:
      - condition: state
        entity_id: input_boolean.vacation
        state: 'off'
    action:
      - service: light.turn_on
        data:
          entity_id: light.mikey_s_bed_lights
          brightness: 5
          rgb_color: [255,0,0]
          white_value: 0

#########################################################
#                                                       #
#                  OK TO WAKE - GREEN                   #
#                                                       #
#########################################################

  - alias: "OK to Wake Green"
    initial_state: true  
    trigger:
      platform: template
      value_template: "{{ states('sensor.time') == states('input_datetime.green_light_time').rsplit(':',1)[0] }}"
    condition:
      - condition: state
        entity_id: input_boolean.vacation
        state: 'off'
    action:
      - service: light.turn_on
        data:
          entity_id: light.mikey_s_bed_lights
          brightness: 5
          rgb_color: [0,255,0]
          white_value: 0
   
#########################################################
#                                                       #
#                  OK TO WAKE - OFF                     #
#                                                       #
#########################################################

  - alias: "OK to Wake Off"
    initial_state: true  
    trigger:
      platform: template
      value_template: "{{ states('sensor.time') == states('input_datetime.ok_to_wake_off').rsplit(':',1)[0] }}"
    action:
      service: light.turn_off
      data:
        entity_id: light.mikey_s_bed_lights

i am trying to do something similar but i want my light to fade up in the morning as my alarm goes off to help me wake up. how did you setup the fade script? did you write/find a tool to do it or did you calculate and write it all by hand?

With a recent update to HA this script was made much simpler.

It was a bit of calculation and a bit of trial and error. I didn’t want the transitions to be noticeable so I setup the script and watched the changes. The basics are 255=100% and 0=Off. You can setup steps from there.

I would strongly suggest you look at the light documentation, and if your light supports it, use the transition service, it take all the BS I am doing out of the picture. Also using brightness_pct would be much easier than working with the 0-255 scale in brightness. Unfortunately, my light strip controller didn’t play nice with these service calls.

Also check this

thanks for the updated script that will be much easier to reverse to fade on not fade off. i did try the transition parameter and it did not work with my strip either.

this might help you if something breaks your current setup. while i did get this working i did not like the effect so i ended up pulling my led controller (like yours but only 1 strip so smaller) appart and reflashing the ESP chip with tasmota. this was quite easy if you can solder and the firmware does support the transition function so it is much simpler to do

EDIT: ok some follow up. something i did killed the green chanel. i don’t think it was the soldering or the flashing but i did test it before putting the casing back on and with the jumpers for the flashing still installed so i think i must have shorted something that killed that mosfet or gpio or something. so i would still say it is a safe procedure and i am going to do it to my other 2 controllers however beware of where any exposed wires are going and put the case back on as soon as possible.

EDIT 2: never mind i have since found it was a configuration issue with the pin mapping i had set. now got 2 led strips controlled perfectly with home assistant with no external server connection and fading smoothly thanks to your recommendation

1 Like