MagicMirror² v2.4.1 is available! For more information about this release, check out this topic.
Please read the release notes carefully since this update requires adjustments to your Raspberry Pi configuration!

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.setup(15, GPIO.IN)         #Read output from PIR motion sensor
    while True:
           if i==0:                 #When output from motion sensor is LOW
                 print "No movement detected - Turning lights off",i
                 exec(open("./").read(), globals())
           elif i==1:               #When output from motion sensor is HIGH
                 print "Movement detected - Turning lights on",i
                 exec(open("./").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?