Almost 4 years later but it is soon that time of the year again…
I wrote a small script for the python_integration
which toggles any set of lights on and off in random groups, with random colors and in random intervals. The upside of writing it in a script is that I can use millisecond wait time, the downside is that the sleep function slows down homeassistant so I am not running it all night long. My idea is to let my doorbell motion toggle an input_boolean on, and when noone has been there for 5 minutes turn it off. Then another automation to run it every once and then during the night.
Python script (named disco.py):
def sleep(millis):
end = datetime.datetime.now() + datetime.timedelta(milliseconds=millis)
while datetime.datetime.now() < end:
pass
def toggle(lights, brightness, color):
hass.services.call('light', 'toggle', {'entity_id': lights, 'brightness_pct': brightness, 'rgb_color': color})
logger.debug("Toggle lights: %s, %s, %s", lights, color, brightness)
switch = data.get("switch")
lights = data.get("lights", [])
colors = data.get("rgb_colors", [255, 255, 255])
brightness_min = max(data.get("brightness_min", 1), 1)
brightness_max = min(data.get("brightness_max", 100), 100)
sleep_min_ms = max(data.get("sleep_min_ms", 1500), 1)
sleep_max_ms = max(data.get("sleep_max_ms", 5000), 1)
input_is_ok = True
if hass.states.get(switch) is None:
input_is_ok = False
logger.warn("%s is not a valid homeassistant entity!", switch)
if len(lights) == 0:
input_is_ok = False
logger.warn("No lights defined, disco is cancelled!")
if input_is_ok:
while hass.states.get(switch).state == 'on':
lights_to_toggle = random.sample(lights, random.randint(1, len(lights)))
color = random.sample(colors, 1)[0]
brightness_value = random.randint(brightness_min, brightness_max)
sleep_value = random.randint(sleep_min_ms, sleep_max_ms)
toggle(lights_to_toggle, brightness_value, color)
logger.debug("Now sleeping for %s", sleep_value)
sleep(sleep_value)
logger.info("Exiting disco")
I use the input_boolean as source, and as long as it is set to on the script continues. Automation to start the script with halloween themed parameters:
- alias: Turn on halloween disco
initial_state: 'on'
trigger:
platform: state
entity_id: input_boolean.halloween_lights
to: 'on'
action:
- service: python_script.disco
data:
switch: input_boolean.halloween_lights
lights:
- light.outside1
- light.outside2
- light.outside3
- light.outside4
- light.outside5
rgb_colors:
- [255, 193, 0]
- [255, 140, 0]
- [255, 116, 0]
- [255, 69, 0]
- [255, 0, 0]
brightness_min: 15
brightness_max: 90
sleep_min_ms: 500
sleep_max_ms: 3500