Jan 202013

I have been playing around with Motion on one of my Linux boxes, and got annoyed at the constant stream of emails that I am receiving as I have setup Motion to send an email to myself when it detects motion, so I wrote up a short script to activate or deactivate motion if it can pick up certain devices by pinging them.

It’s not foolproof as my wifi is playing up a bit and some devices do not respond by ping sometimes, but it’s useful for deactivating something when you approach and reactivating it when you leave, or vice versa depending on your use of this script.
Setting this script up as a cronjob will allow it to run every say 5 minutes for automation.

people=( )
MotionStarted=`wget -o getlog -O /usr/scripts/status http://localhost:8080/0/detection/status | cat /usr/scripts/status | sed -e 's/<[^>]*>//g' | tail -n 3 | head -n -2`

for ip in "${people[@]}"; do
PersonAround=`ping -c 1 $ip | grep '64 bytes' | wc -l`
if [[ PersonAround -eq 1 ]]; then

if [[ $PeopleAround -eq 1 ]]; then
if [[ "$MotionStarted" =~ "ACTIVE" ]]; then
wget -O /usr/scripts/status
echo "People around, stopping motion" $run | mail -s 'Motion stopped' YOUR@EMAIL.COM
if [[ "$MotionStarted" =~ "PAUSE" ]]; then
wget -O /usr/scripts/status
echo "No One around, Starting motion" $run | mail -s 'Motion started' YOUR@EMAIL.com


  11 Responses to “Script : Activating / Deactivating Motion if device appears or disappears”

  1. Hi and thanks for your really useful guide to getting Motion running. Like you though, I’m getting lots of emails, so have tried to implement the script above. I’m a real newbie, so here’s what I’ve done:

    Created a script called ‘motion_check_nearby.sh’ and entered your script above. I’m pretty sure I’ve entered it correctly, adjusting only the email address.

    The problem is pretty early on though. Running (sh motion_check_nearby.sh?) gives me an error:

    motion_check_nearby.sh: 3: motion_check_nearby.sh: Syntax error: “(” unexpected

    So I’m guessing (again, newbie) that line 3 defines a variable called people, type list?, but the shell script is unhappy about the bracket. I’ve checked the script and can’t see any other issues, such as where the values of people is checked in line 8, but can’t see what I’m doing wrong?

    Also, do the permissions need to be changed on the script? ./motion_check_nearby.sh fails to run (does the #!/bin/bash declaration at the beginning indicate you should be able to run it by ./motion_check_nearby.sh?)

    Thanks in advance for your further help.

    BR, Adam.

    • Permissions need to include the execute permission.

      Can you paste the offending line ? Make sure there’s no spaces between the bracket and =

      Also, big thanks for the massive donation πŸ˜€

  2. Sorted it – me being a numpty and putting a comma in to separate the values in the list πŸ™

    Now this error:

    wget: missing URL
    Usage: wget [OPTION]… [URL]…
    Try `wget –help’ for more options.
    cat: ./motion_check_nearby.sh: line 7: http://localhost:8081/0/detection/status: No such file or directory
    /usr/scripts/status: No such file or directory
    sed: option requires an argument — ‘e’
    Usage: sed [OPTION]… {script-only-if-no-other-script} [input-file]…

    Incidentally, I have to connect to 8081 to see the stream from motion, despite not changing the default port. However, changing all references in the script to 8080 yields the same result.

  3. […] a follow up this this post, the script that I used had a few issues running on the server that I was using it on. For some […]

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>