Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5045

Automation, sensing and robotics • Re: Script lines to run only between certain times of day

$
0
0
this should work between 8am and 8pm (20:00), adjust as required... ( obviously your pi clock needs to be correct!)

Code:

from gpiozero import Buttonimport requestsfrom signal import pausefrom time import sleepimport timeimport datetimeprint(now,start_period,stop_period)#  Define inputs - these are GPIO numbersLRSlider = Button(21)  # Input from den entry door#  Define what to do when switches or PIRs are activedef LRSlideropen():    now = datetime.datetime.now()    start_period=now.replace(hour=8, minute=0, second=0, microsecond=0)    stop_period =now.replace(hour=20, minute=0, second=0, microsecond=0)    if now > start_period and now < stop_period:       print("open")    def LRSliderclosed():    now = datetime.datetime.now()    start_period=now.replace(hour=8, minute=0, second=0, microsecond=0)    stop_period =now.replace(hour=20, minute=0, second=0, microsecond=0)    if now > start_period and now < stop_period:        print("closed")    #  Define switch and PIR statesLRSlider.when_pressed = LRSliderclosedLRSlider.when_released = LRSlideropenpause()

Statistics: Posted by gordon77 — Sun Dec 22, 2024 10:51 am



Viewing all articles
Browse latest Browse all 5045

Trending Articles