Python Forum
Time based control of phillips hue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Time based control of phillips hue
#1
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)
Reply
#2
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)
Reply
#3
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()
Reply
#4
Aaah bloody perfect! Thank you both so much! :D
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Update Date based on Time/String stevezemlicka 1 2,038 Jan-08-2021, 06:54 PM
Last Post: Gribouillis
  Split gps files based on time (text splitting) dervast 0 1,892 Nov-09-2020, 09:19 AM
Last Post: dervast
  Time based loops Owenix 4 3,752 Sep-20-2018, 05:37 PM
Last Post: Owenix
  Phillips hue simple on script Kimzer 6 4,575 Jan-16-2018, 07:53 AM
Last Post: Kimzer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020