Schedule Motion Movement Detection

The following script should be run every minute (placed in /etc/cron.d/minutely) in order to start or stop detection depending on the current time. The script can be configured by changing the parameters in the CONFIGURATION section.

#!/bin/sh
###########################################################################
##  Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3      ##
##  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  ##
##  rights of fair usage, the disclaimer and warranty conditions.        ##
###########################################################################
## The script uses motion to start and stop motion detection such that   ##
## the camera will start recording only within the given time intervals. ##
##                                                                       ##
## To use the script, drop into /etc/cron.minutely such that the script  ##
## will be called every minute by crontab and pause or start detection.  ##
###########################################################################
 
###########################################################################
##                            CONFIGURATION                              ##
###########################################################################
 
# The detection URL to the motion webserver.
MOTION_URL_DETECTION=http://localhost:8085/0/detection
 
# The start time after which recording should start (HHmm)
START_TIME=0705
 
# The end time after which recording should stop (HHmm)
PAUSE_TIME=1700
 
###########################################################################
##                              INTERNALS                                ##
###########################################################################
 
# Get the current recording status.
DETECTION_STATUS=`curl -s $MOTION_URL_DETECTION"/status" 2>/dev/null | grep -Po '(?<=Detection status )(.+)'`
 
# Get the current time.
CURRENT_TIME=`date +%H%M`
 
# Whether the current time falls between the defined start and stop times.
test `[ $CURRENT_TIME -ge $START_TIME ] && [ $CURRENT_TIME -le $PAUSE_TIME ]`
case $? in
    0)
        case $DETECTION_STATUS in
            PAUSE)
                logger "Start recording..."
                # Instruct the camera to start detection and recording.
                curl -s $MOTION_URL_DETECTION"/start" >/dev/null
            ;;
        esac
        ;;
    *)
        case $DETECTION_STATUS in
            ACTIVE)
                logger "Stop recording..."
                # Instruct the camera to start detection and recording.
                curl -s $MOTION_URL_DETECTION"/pause" >/dev/null
            ;;
        esac
        ;;
esac

Reduce CPU Usage

Motion can be very CPU intensive when capturing streams such that the following tips can be used as a guide to lowering the CPU usage considerably:

ffmpeg_video_codec mp4:h264_omx

in order to benefit from hardware accelerated video capturing. This option alone should lower the CPU consumption considerably and the hardware acceleration is now present on Raspberry Pi 2 and 3.