Hello,
I know very little about Python programming, so be gentle with me please
I have a script for detecting a PIR movement on a Raspberry Pi 3b.
from gpiozero import MotionSensor
import os
import RPi.GPIO as GPIO
import time
pir = MotionSensor(21)
while True:
pir.wait_for_motion()
print("You moved")
pir.wait_for_no_motion()
I want to turn a monitor on with the command 'DISPLAY=:0.0 xrandr --output HDMI-1 --auto --rotate left' if movement is detected and off with the command 'DISPLAY=:0.0 xrandr --output HDMI-1 --off' after, say, 5 minutes.
I would be grateful if anyone can tell me how to run these commands from within a Python script
Thanks in advance
"I want to turn a monitor on with the command"
If you mean enter it on the command line, use subprocess.run
That is the command line that I want to run from within a python script
(Mar-24-2025, 08:49 AM)lif Wrote: [ -> ]if movement is detected and off with the command 'DISPLAY=:0.0 xrandr --output HDMI-1 --off' after, say, 5 minutes.
As mention use
subprocess and is better to use eg
schedule when you need to wait 5-min.
import schedule
import time
import subprocess
from gpiozero import MotionSensor
pir = MotionSensor(21)
def turn_monitor_on():
subprocess.run("DISPLAY=:0.0 xrandr --output HDMI-1 --auto --rotate left", shell=True)
print("Monitor turned on.")
def turn_monitor_off():
subprocess.run("DISPLAY=:0.0 xrandr --output HDMI-1 --off", shell=True)
print("Monitor turned off.")
def schedule_monitor_off():
schedule.clear()
schedule.every(5).minutes.do(turn_monitor_off)
print("Monitor off scheduled in 5 minutes.")
while True:
pir.wait_for_motion()
turn_monitor_on()
schedule_monitor_off()
# Check periodically for new motion and run scheduled jobs.
# If new motion is detected, break out to restart the cycle.
while not pir.motion_detected:
schedule.run_pending()
time.sleep(1)
Thank you very much, that works perfectly apart from running a second time after I ctrl z to stop the script running.
(Mar-24-2025, 04:23 PM)snippsat Wrote: [ -> ] (Mar-24-2025, 08:49 AM)lif Wrote: [ -> ]if movement is detected and off with the command 'DISPLAY=:0.0 xrandr --output HDMI-1 --off' after, say, 5 minutes.
As mention use subprocess and is better to use eg schedule when you need to wait 5-min.
import schedule
import time
import subprocess
from gpiozero import MotionSensor
pir = MotionSensor(21)
def turn_monitor_on():
subprocess.run("DISPLAY=:0.0 xrandr --output HDMI-1 --auto --rotate left", shell=True)
print("Monitor turned on.")
def turn_monitor_off():
subprocess.run("DISPLAY=:0.0 xrandr --output HDMI-1 --off", shell=True)
print("Monitor turned off.")
def schedule_monitor_off():
schedule.clear()
schedule.every(5).minutes.do(turn_monitor_off)
print("Monitor off scheduled in 5 minutes.")
while True:
pir.wait_for_motion()
turn_monitor_on()
schedule_monitor_off()
# Check periodically for new motion and run scheduled jobs.
# If new motion is detected, break out to restart the cycle.
while not pir.motion_detected:
schedule.run_pending()
time.sleep(1)