Simple python script for pir motion sensor



  • Hey guys
    I managed to throw together this bit below by looking at other pir sensor scripts to control a set of hue strips. Its probably a million better ways to do this but this is where im at.

    The problem is that this scripts overrides whatever i do from say my phone app.
    If i turn on the lights from my phone and there is no movement next to the sensor it will kill the lights after a sec or two. This i want to avoid, i would like this script to be able to be overrun by the controls elsewhere. Could someone please help me out with this?

    import RPi.GPIO as GPIO
    import time
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(15, GPIO.IN)         #Read output from PIR motion sensor
    while True:
           i=GPIO.input(15)
           if i==0:                 #When output from motion sensor is LOW
                 print "No movement detected - Turning lights off",i
                 exec(open("./LightsOff.py").read(), globals())
                 time.sleep(1)
           elif i==1:               #When output from motion sensor is HIGH
                 print "Movement detected - Turning lights on",i
                 exec(open("./LightsOn.py").read(), globals())
                 time.sleep(60 * 30)
    

    Edit; the time.sleep (60 * 30) is used because i want the light to stay on for 30 minutes after there has been movement infront of the pir sensor. There is probably a better solution to this somewhere?

    The two scripts being executed is merely a couple of scripts telling the hue bridge to turn lights on/off.
    Let me know if those are needed for any reason and i will post them.



  • Anybody?


Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.