I made some code to turn on a light depending on movement and the time of the day in an if statement. If the (local)time is between sunrise and a preset time or sunset and a preset time the if statement is true and the motion detection statements will follow.
At the moment the time is only presented in hours as an integer. How can i make my if statement to work with hours and minutes (think this will need to be a string). So the statement will work with 6:45 instead of 7 for example.
Here is my code.
import RPi.GPIO as GPIO
import time
import urllib2
from datetime import datetime
from business_calendar import Calendar, MO, TU, WE, TH, FR
from pytz import timezone
#uur
fmh = "%H"
fmt = "$H:%M"
#Def. var.
x=0
now_utc = 0
now_amsterdam = 0
print "Here we go! Press CTRL+C to exit"
try:
while x < 1:
count = 0
date = datetime.today()
#werkdag def.
cal = Calendar(workdays=[MO,TU,WE,TH,FR])
busday = cal.isbusday(date)
# dT AMS-UTC
now_utc = datetime.now(timezone('UTC'))
now_amsterdam = now_utc.astimezone(timezone('Europe/Amsterdam'))
UTC = int(now_utc.strftime(fmh))
AMS = int(now_amsterdam.strftime(fmh))
dT = AMS - UTC
# Zonsopgang/Ondegang
request = 'http://ift.tt/2boghMY'
response = urllib2.urlopen(request)
timestring = response.read()
utcsunrise = timestring[35:39] #Will be 35:39 for HH:MM and cant be a string then
utcsunset = timestring[71:73] #Will be 71:79 for HH:MM
amssunrise = int(utcsunrise) + dT #Wont work with a string
amssunset = int(utcsunset) + dT
if amssunrise < 8 #Will be 7:30
amssunrise = 8 #Will be 7:30
if amssunset < 21 #Will be 21:30
amssunset = 21 #Will be 21:30
#uur
uur = date.hour #Must be HH:MM instead of only hours
if busday == True and ((uur >= 7 and uur <= amssunrise) or (uur >= amssunset and uur <= 23)):
# rest of the statements
# end statement
except KeyboardInterrupt:
GPIO.cleanup()
I would like to hear your thoughts. Little note I'm new to python but not to coding.
Greetings, Thijs
Aucun commentaire:
Enregistrer un commentaire