Home Assistant - Random Color changing lights

blargin ... Want to get a random color?

Home Assistant - Random Color changing lights

GOAL: When I turn on a "switch" ( switch meaning a home assistant, boolean, binary virtual switch ), have a time interval in which all the lights I designate will change to random colors continuously until the switch it turned off.

FYI: this could easily be made to work with a real wall switch if so desired.

If you are like me, and like playing with home automation, you have probably stumbled upon Home Assistant.

Recently Home Assistant released AppDaemon to allow you to run python scripts whenever you desire.

I am a developer but have not done anything with python previously. I quickly realized I should learn a little of the basics for what I considered to be a "need" in my home. I "needed" to be able to turn a sub-set of my lights on and have them change to random colors. Who wouldn't need this?

So the basics are:

Needed Import

import random

Lights

I only wanted certain lights to change color. So I created an array of the lights to turn on, off, or change color.

    self.lights = [ 
        "light.living_room_1",
        "light.livingroom_2",
        "light.livingroom_3",
        "light.entryway_1",
        "light.entryway_2",
        "light.entryway_3",
        "light.entryway_4",
        "light.dinning_room"
    ]

Function to get random color

  def getRandomColorInt( self ):
    return random.randint( 1 , 255 ) 

Function to change the colors

  def changeColorOfLights( self , kwargs ):
    for s in self.lights:
      self.turn_on( s , rgb_color =  [ self.getRandomColorInt() , self.getRandomColorInt() , self.getRandomColorInt() ]  , brightness = 255 )

YAY nerds.. here is the file:

import appdaemon.appapi as appapi
import random
#
# Crazy Lights
#
# Args:
#
class CrazyLights(appapi.AppDaemon):

  def initialize(self):
    self.lights_on = 0
    self.timer = None
    self.allLights = [ 
        "light.living_room_1",
        "light.livingroom_2",
        "light.livingroom_3",
        "light.entryway_1",
        "light.entryway_2",
        "light.entryway_3",
        "light.entryway_4",
        "light.dinning_room"
    ]
    
    self.listen_state( self.state_change , "input_boolean.crazy_lights")

  def state_change(self, entity, attribute, old, new, kwargs):
    if new == "on":
        self.lights_on = 1
        self.start_timer()
    else:
        self.lights_on = 0
        self.cancel_timer(self.timer)
        
  def start_timer(self):
    self.timer = self.run_in( self.changeColorOfLights , 7 )

  def turnOffLights(self):
    for s in self.allLights:
      self.turn_off( s )
  
  def changeColorOfLights( self , kwargs ):
    if( self.lights_on == 1 ):
        for s in self.allLights:
          self.turn_on( s , rgb_color =  [ self.getRandomColorInt() , self.getRandomColorInt() , self.getRandomColorInt() ]  , brightness = 255 )
        self.start_timer()
    else:
        self.stop_timer()
        
  def getRandomColorInt( self ):
    return random.randint( 1 , 255 )   

Tags

  • Home automation
  • Home Assistant App Daemon
  • Home Assistant

Add Comment