My Alarm Clock Setup - "Circadian Rhythm" Lights

Hey guys - I just wanted to share the “alarm clock” I built for me and my kids. The idea is slowly wake everyone up using light. Works pretty well. I had to do it this way as I use insteon light switches - so there is no native transition support. Basically, in the frontend, I expose 3 inputs per alarm:

  • 1x binary to say if the alarm should be triggered and is in progress. Turning it off cancels an active alarm
  • 1x input slider used to set the max brightness
  • 1x input slider to set the number of seconds for the brightness to increase over

I control when the alarm goes off through homekit, which works super well. The script is below. One improvement I want to make is transform this into a utility script so that I dont need one script per person…

Open to feedback!

import appdaemon.appapi as appapi
#from datetime import datetime, timedelta
import time
#import math



class wakeup(appapi.AppDaemon):
  def initialize(self):
    self.handle = self.listen_state(self.checkInput,"input_boolean.wake_up_jared", alarm = "on")
    
  def checkInput(self,switch_name,attribute,old,new,kwargs):
    
    
    #timer = 1800 #Number of seconds to execute over 
    timer=float(self.entities.input_number.wakeupjared_timer.state)
    desired_brightness=float(self.entities.input_number.wakeupjared_brightness.state)
    lightname = "light.jareds_room"
    i=0
    brightness=0
    old_brightness=0
    dim_rate = desired_brightness/timer
    
    self.log("*************Starting App Logic*****************")

    if ((self.get_state(switch_name) == "on") and (self.getBrightness(lightname) < desired_brightness)):
      self.log("**Boolean is true**")
      while (self.getBrightness(lightname)< desired_brightness):
        if ((self.get_state(switch_name) == "on") and ((self.getBrightness(lightname) <= brightness))):
#          self.log("SLEEP!")
          brightness=i*dim_rate
          time.sleep(1)
          i+=1
          if ((int(brightness)>int(old_brightness)) and (int(old_brightness) == self.getBrightness(lightname))):
            self.turn_on(lightname,brightness=brightness)
            self.log('***STARTING BRGHTNESS CYCLE*** dim_rate is int {}'.format(int(brightness)))
    
          old_brightness = brightness
        else:
          self.turn_off(switch_name)
          self.log('***DETECTED VARIABLE CHANGE***')
          break
      self.log('***Done. Resetting Input***')
      self.turn_off(switch_name)

  def getBrightness(self,lightname):
    actual=self.get_state(lightname, "brightness")
    if actual is None:
      actual=0
    return actual