Building a Sleep Training Light with ESP8266, ESPHome, & Home Assistant

My kids, specifically the eldest, loves to wake up before 7. My wife and I, do not like to wake up before 7. My kids can’t read the time yet or understand why every month the sun seems gets up at a different time.

Our solution to help stop this was a sleep training clock: a visual way to differentiate sleeping time with awake time. Online they run about $40 and, in my opinion, are just over the top with features and buttons. A simple scheduled LED was all we were after. So I built one with an esp8266, ESPHome, & HomeAssistant:

Requirements:

Wiring it Together

I’ve just moved to using an iPad as my primary personal computer so I no longer have Fritzing, so my crude drawing in Notes.app will have to do:

Common anaode RGB wiring to an ESP8266, pins D5, D6, D7.

Setting up ESPHome Module

In Home Assistant follow the ESPHome Add Module wizard filling in info about your ESPHome and WIFI networking settings. Editing the file once complete, mine looks like:

esphome:
  name: sleep_training_light
  platform: ESP8266
  board: esp12e

wifi:
  ssid: !secret wifi_name
  password: !secret wifi_pass
  manual_ip:
    static_ip: !secret sleep_training_light_ip
    gateway: !secret ip_gateway
    subnet: !secret ip_subnet

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "sleep-training-light"
    password: !secret fallback_pass

# LED
# @see https://community.home-assistant.io/t/inverse-rgb-led-values/170069/3
# common Anode
light:
  - platform: rgb
    name: "Sleep Training Light"
    red: output_red
    green: output_green
    blue: output_blue  
    
output:
  - platform: esp8266_pwm
    id: output_red
    #D6
    pin: GPIO12
    inverted: true
  - platform: esp8266_pwm
    id: output_green
    #D7
    pin: GPIO13
    inverted: true
  - platform: esp8266_pwm
    id: output_blue
    #D5
    pin: GPIO14
    inverted: true

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret api_pass

ota:
  password: !secret ota_pass

Add to HA

Home Assistants notification area should alert you to the new ESPHome device. You can add this entity.

Automation

At time of writing, I found the Confugration > Automattions area of Home Assistant a bit buggy, I ended up mostly setting up the automation via the File Editor. Here’s why my /config/automations.yaml looks like:

- id: sleep_training_light_night
  alias: Sleep Training Light -> Night
  description: ''
  trigger:
  - platform: time
    at: '20:00:00'
  action:
  - data:
      brightness: 80
      entity_id:
      - light.sleep_training_light
      rgb_color:
      - 162
      - 116
      - 255
    service: light.turn_on
    entity_id: light.sleep_training_light
  mode: single
- id: sleep_training_light_wakeup
  alias: Sleep Training Light -> Wake Up
  description: ''
  trigger:
  - at: 07:00:00
    platform: time
  action:
  - data:
      brightness: 0
      entity_id:
      - light.sleep_training_light
      rgb_color:
      - 0
      - 0
      - 0
    entity_id: light.sleep_training_light
    service: light.turn_on
  mode: single

This turns on the light at 8:00pm, and turns off the light at 7:00am.

Case

I designed a custom case (will update this post with a link to the thingiverse.com design shortly) for another project (IR Blaster) and reused it for this one, however I printed with clear filament and kept the RGB LED inside, giving the box a nice glow:

Leave a Reply

Your email address will not be published. Required fields are marked *