'How can I use the "on_motion_detected" method on Raspberry Pi motion?

I have been using my Raspberry Pi 2 to do some motion detection using a USB webcam and the motion package and am incredibly frustrated.

Can someone explain to me how the on_motion_detected method is supposed to work??????

The idea is that when the camera detects motion, a script is executed. The script just echo's a few words for testing purposes.

The video stream works great on my local network, and I can see the motion writing the JPG and .avi files to the directory.

Even when I try to add my script to the movie start trigger, nothing happens.

Some examples I have tried:

  • ; on_motion_detected python /home/pi/Desktop/Python/script.py
  • ; on_movie_start python /home/pi/Desktop/Python/script.py
  • ; on_picture_save sudo python /home/pi/Desktop/Python/script.py

I have also changed the script to different directories, speculating a permission issue. Still nothing happens. I have tried removing the ; before the methods, still nothing happens. I have tried sudo, I have tried executing as a script. Please, can someone offer some help. I have searched years and years of threads and not found an answer anywhere.

My script is not being executed.

This question has been asked 1000 times and nobody has answered it. I have been searching for several hours for an answer.

Here's just a few of the threads that have gone uanswered:

https://unix.stackexchange.com/questions/59091/problems-running-python-script-from-motion https://www.raspberrypi.org/forums/viewtopic.php?t=86534&p=610482 https://raspberrypi.stackexchange.com/questions/8273/running-script-in-motion



Solution 1:[1]

Ypu have to remove ; which stands for commenting the line

; on_motion_detected python /home/pi/Desktop/Python/script.py

but

 on_motion_detected python /home/pi/Desktop/Python/script.py

Solution 2:[2]

if you use threads: threat1.conf, threat2.conf -- try to write script config to them. My threads: thread1.conf

videodevice /dev/video0
text_left USBWebcam-1
target_dir /samba/hdd/share/motion/usb
rotate 180
width 1024
height 768
#webcam_port 9081
#on_event_start /bin/bash /root/scripts/tg_msg.sh
on_picture_save /bin/bash /root/scripts/tg_msg.sh %f
#on_motion_detected /bin/bash /root/scripts/tg_msg.sh
#on_area_detected /bin/bash /root/scripts/tg_msg.sh
#on_movie_start /bin/bash /root/scripts/tg_msg.sh

and thread2.conf:

text_left Foscam IP
netcam_url http://192.168.111.7:8080/video
target_dir /samba/hdd/share/motion/ip
width 1024
height 768
#on_event_start /bin/bash /root/scripts/tg_msg.sh
on_picture_save /bin/bash /root/scripts/tg_msg.sh %f
#on_motion_detected /bin/bash /root/scripts/tg_msg.sh
#on_area_detected /bin/bash /root/scripts/tg_msg.sh
#on_movie_start /bin/bash /root/scripts/tg_msg.sh

Solution 3:[3]

I have a simple solution but someone needs to explain how this works. Start motion as root. Add a line to /etc/rc.local 'sudo motion' reboot and most events will work if started as root, on_event_end sudo ...command or script...

Solution 4:[4]

Check that you have specified the process id properly in motion.conf:

process_id_file /var/run/motion/motion.pid

Once you have checked that, change the following settings to arbitrarily low values e.g.

threshold 1

noise_level 1

Solution 5:[5]

sudo chown motion:motion /home/pi/Desktop/Python/script.py

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jdeboer
Solution 2 alteruser
Solution 3 mrjeff
Solution 4 IanB
Solution 5 MUD