Python Forum
Sending different color codes in python at certain times
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending different color codes in python at certain times
#1
Hi all..

Looking for some help here.
Using below script.
Trying to add the following functions:

I want the pir sensor to basically do nothing between midnight and 0500. So disabled at certain times, but button should work still.

I want to be able to send different colors to the lights at certain times of the day.
So if motion is detected between 7am and 12 am it will be this color code sendt {"on" : True, "bri" : 255, "sat" : 0, "hue" : 0} but when its between 8pm and 23pm this will be sendt {"on" : True, "bri" : 255, "sat" : 173, "hue" : 333} for example.

Any easy way of achieving this? I have tried defining the command called when sending the color codes but im at a standstill here.

import sys
sys.path.append("/home/pi/.local/lib/python2.7/site-packages")

from phue import Bridge
import RPi.GPIO as GPIO
import time
import datetime
print 'Waiting for network...'
time.sleep(30)
print 'The wait is over. It\'s showtime!'
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN) #Read output from PIR motion sensor
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Read output from button.
b=Bridge('192.168.1.47')
try:
    b.connect()
except ImportError:
    print "Import error occurred!"
print "Connected to Hue bridge!"

lightson=b.get_light(2, "on")
if lightson: print "Lights are already on."
print 'Entering infinite loop...'

light_on_delay = 15  # time in min for light to stay on when button pressed
button_pressed = 0

while True:
# check for button press
input_state = GPIO.input(18)
if input_state == False:
    print('Button Pressed')
    end = (light_on_delay * 1) + time.time()
    button_pressed = 1
    command =  {"on" : True, "bri" : 255, "sat" : 0, "hue" : 0}
    b.set_group(2, command)
    lightson=True
    print('Lights are on for 15 minutes')    

# check if button has been pressed if it has check to see if time is up
if button_pressed == 1:
    if time.time() > end:
        button_pressed = 0

else:

    i=GPIO.input(4)
    timestamp=datetime.datetime.now().time()
    if (timestamp < offstarttime and timestamp > offendtime):
    if i==0:            #When output from motion sensor is LOW
        print ('No movement detected - Turning lights off')
        b.set_group(2, 'on', False) 
        lightson=False
        print ('Lights are off')
        time.sleep(0.1)
    else:               #When output from motion sensor is HIGH
        print ('Movement detected - Turning lights on')
        command =  {"on" : True, "bri" : 255, "sat" : 0, "hue" : 0}
        b.set_group(2, command)
        lightson=True
        print ('Lights are on.')
        time.sleep(5)    

# added delay to prevent program using 100% cpu time.
time.sleep(0.5)
Reply
#2
Something like this with a in_between function.
from datetime import datetime, time

def in_between(now, start, end):
    if start <= end:
        return start <= now < end
    else:
        return start <= now or now < end
Test:
>>> now = datetime.now().time()
>>> now
datetime.time(10, 39, 7, 444963)
>>> print("8 to 23" if in_between(now, time(8), time(23)) else "Some other time")
8 to 23
>>> print("8 to 23" if in_between(now, time(14), time(23)) else "Some other time")
Some other time
Reply
#3
Not quite what i was after. This is how i currently do it, but its way too slow and sluggish.

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
 
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]]

 
headers = {
    'Accept': 'application/json',
}
 
requests.put('http://192.168.1.47/api/xxxxxxxxxxxxxxxxxxx/lights/2/state', headers=headers, data=color)
Reply
#4
Anyone that would be able to help me out here? :)
Reply
#5
time_colors = {'red':(0,2),'blue':(2,7),'white':(7, 20),'yellow':(20,24)}
for k,v in time_colors.items():
    if(v[0] <= hour < v[1]):
        print(colors[k])
        break
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending a text from Python sawtooth500 2 180 Apr-14-2024, 01:56 PM
Last Post: sawtooth500
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 1,019 Oct-25-2023, 09:09 AM
Last Post: codelab
Photo Visual studio code unable to color syntax on python interpreter tomtom 4 6,910 Mar-02-2022, 01:23 AM
Last Post: tomtom
  Sending string commands from Python to a bluetooth device Rovelin 13 9,492 Aug-31-2021, 06:40 PM
Last Post: deanhystad
  How to set LD_LIBRARY_PATH on python codes? aupres 1 8,474 Mar-25-2021, 08:49 AM
Last Post: buran
  Automating to run python script 100 times by changing parameters pmt 1 2,610 Dec-29-2020, 10:31 AM
Last Post: andydoc
  Sending Out Email via Python JoeDainton123 1 4,786 Aug-31-2020, 12:54 AM
Last Post: nilamo
  How to load log.txt directly into python codes? sparkt 6 2,986 Aug-21-2020, 03:51 PM
Last Post: sparkt
  Python sockets : Client sending to the server nico31780 0 2,348 May-17-2020, 07:56 PM
Last Post: nico31780
  problems sending BIN file with XMODEM python shaya2103 0 2,834 Nov-23-2019, 10:27 AM
Last Post: shaya2103

Forum Jump:

User Panel Messages

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