Python Forum

Full Version: Time based control of phillips hue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

Very new to all of this so bear with me.
Below is my script, ive commented out a few colors.

What i am trying to do is the following;

A second script reads motion from a sensor. Wich triggers the script below.
Wich atm will turn my hue light strip on at full white. Great!

But say if i want the color to change to full red (just for example purposes) if the time is between 24:00 and 02:00

How would i go about achieving this?
The goal here is to have the light gradually change to a darker white during the time of day.
Just using theese colors to test.


import requests
import time

time.sleep(0.1)

headers = {
    'Accept': 'application/json',
}
# White
data = '{"on":true,"bri":255,"sat":0,"hue":0}'
# Blue
#data = '{"on":true,"bri":255,"sat":255,"hue":46920}'
# Red
#data = '{"on":true,"bri":255,"sat":255,"hue":0}'
# Yellow
#data = '{"on":true,"bri":255,"sat":255,"hue":12750}'


requests.put('http://192.168.1.47/api/xxxxxxxxxxxxxxxxxxx/lights/2/state', headers=headers, data=data)
something like this

import requests
import time

colors = {'white':'{"on":true,"bri":255,"sat":0,"hue":0}',
          'blue':'{"on":true,"bri":255,"sat":255,"hue":46920}',
          'red':'{"on":true,"bri":255,"sat":255,"hue":0}',
          'yellow':'{"on":true,"bri":255,"sat":255,"hue":12750}'}

hour = time.localtime().tm_hour

if hour > 19:
    color = colors['yellow']
elif hour > 6:
    color = colors['white']
elif hour > 1:
    color = colors['blue']
else:
    color = colors['red']


# ----- alternative to above if/elif/else block -----

time_colors = {(0,2):'red', (2,7):'blue', (7, 20):'white', (20,24):'yellow'}
color_schema  = {hour:color for hours, color in time_colors.items() for hour in range(hours[0], hours[1])}
color = colors[color_schema[hour]]


# ----- end of the alternative -----


headers = {
    'Accept': 'application/json',
}

requests.put('http://192.168.1.47/api/xxxxxxxxxxxxxxxxxxx/lights/2/state', headers=headers, data=color)
To pick your colors, you can use a simple tkinter program like:
import sys
version = sys.version

if 'v: 2' in version:
    import Tkinter as tk
    import Tkinter.colorchooser as tcc
else:
    import tkinter as tk
    import tkinter.colorchooser as tcc


class PickColor:
    def __init__(self, parent):
        self.version = sys.version
        self.parent = parent
        self.parent.geometry('600x400+10+10')
        self.btn1 = tk.Button(self.parent, text='Pick Color',
                              relief=tk.RAISED, padx=2, pady=2,
                              command=self.pick_a_color)
        self.btn1.grid(row=0, column=0)
        self.btn2 = tk.Button(self.parent, text='Quit',
                              relief=tk.RAISED, padx=2, pady=2,
                              command=self.quit)
        self.btn2.grid(row=0, column=1)

    def pick_a_color(self):
        color = tcc.askcolor()
        print('Color Info: {}'.format(color))

    def quit(self):
        self.parent.destroy()
        sys.exit()

def main():
    root = tk.Tk()
    pc = PickColor(root)
    root.mainloop()

if __name__ == '__main__':
    main()
Aaah bloody perfect! Thank you both so much! :D