Daylight

Daylight

The Tron lights in my office wrap around the baseboards and give a cool hue throughout the day. Through the built-in WLED software, they were set to change to a random color every two hours. This was the peak functionality allowed through their built-in settings page. However, the brilliant creators of the script also included an API that allows users to control the settings remotely. Knowing this, I spent some time writing Python code to change the lights dynamically. You can see it below.

The idea was to have them mimic the colors of a day using a daylight color scale. I’ve translated those colors into RGB values and set the brightness to allow for a calmer transition into the evening. When guests are over and sleeping, it’s not necessary to keep the room bright with glowing LEDs. This way, they’ll be able to see where they’re walking in the dark and won’t need to wear eye shades in bed.

After more testing and tweaking, I’ll add holiday transitions. For example, red, green, and white for Christmas, and maybe purple and green for Halloween. WLED also provides animation templates, so perhaps animated candy cane lights would look cool.

This was a little project enhancement, but I figured it was cool enough to share.

lights.py

    import requests
    import time
    from datetime import datetime
    import logging

    logging.basicConfig(filename='/home/pi/myproject/my_script.log', level=logging.DEBUG, 
                        format='%(asctime)s - %(levelname)s - %(message)s')

    addresses = [
        'http://{wled_ip_address}/json/state'   # add additional WLED installs here
    ]

    # Define hourly colors
    colors = {
        1: {'on': True, 'bri': 10, 'seg': [{'col': [[1, 30, 70]]}]},
        2: {'on': True, 'bri': 15, 'seg': [{'col': [[0, 48, 96]]}]},
        3: {'on': True, 'bri': 20, 'seg': [{'col': [[0, 46, 93]]}]},
        4: {'on': True, 'bri': 25, 'seg': [{'col': [[0, 52, 89]]}]},
        5: {'on': True, 'bri': 25, 'seg': [{'col': [[0, 52, 89]]}]},
        6: {'on': True, 'bri': 35, 'seg': [{'col': [[0, 81, 118]]}]},
        7: {'on': True, 'bri': 45, 'seg': [{'col': [[7, 92, 133]]}]},
        8: {'on': True, 'bri': 80, 'seg': [{'col': [[13, 138, 168]]}]},
        9: {'on': True, 'bri': 110, 'seg': [{'col': [[89, 191, 194]]}]},
        10: {'on': True, 'bri': 128, 'seg': [{'col': [[197, 228, 193]]}]},
        11: {'on': True, 'bri': 128, 'seg': [{'col': [[227, 224, 122]]}]},
        12: {'on': True, 'bri': 128, 'seg': [{'col': [[246, 207, 100]]}]},
        13: {'on': True, 'bri': 128, 'seg': [{'col': [[255, 188, 107]]}]},
        14: {'on': True, 'bri': 128, 'seg': [{'col': [[252, 181, 93]]}]},
        15: {'on': True, 'bri': 128, 'seg': [{'col': [[253, 175, 92]]}]},
        16: {'on': True, 'bri': 100, 'seg': [{'col': [[244, 150, 78]]}]},
        17: {'on': True, 'bri': 90, 'seg': [{'col': [[241, 122, 113]]}]},
        18: {'on': True, 'bri': 80, 'seg': [{'col': [[213, 102, 135]]}]},
        19: {'on': True, 'bri': 75, 'seg': [{'col': [[124, 59, 133]]}]},
        20: {'on': True, 'bri': 60, 'seg': [{'col': [[71, 35, 124]]}]},
        21: {'on': True, 'bri': 45, 'seg': [{'col': [[46, 24, 111]]}]},
        22: {'on': True, 'bri': 45, 'seg': [{'col': [[33, 37, 101]]}]},
        23: {'on': True, 'bri': 40, 'seg': [{'col': [[11, 21, 74]]}]},
        0: {'on': True, 'bri': 10, 'seg': [{'col': [[2, 18, 69]]}]}
    }

    # Set color
    def set_color(address, color):
        try:
            response = requests.post(address, json=color)
            if response.status_code == 200:
                logging.info(f'Successfully set {address} to {color}')
            else:
                logging.error(f'Failed to set {address} to {color}, status code: {response.status_code}')
        except requests.exceptions.RequestException as e:
            logging.error(f'Error setting {address} to {color}: {e}')

    # Main loop
    while True:
        current_hour = datetime.now().hour
        color = colors.get(current_hour)

        if color:
            for address in addresses:
                set_color(address, color)
            logging.info(f'All addresses set to color for hour {current_hour}. Waiting for ten minutes...')
        else:
            logging.error(f'No color setting found for hour {current_hour}')

        time.sleep(600)  # Wait for ten minutes