mugurdi
Posts: 68
Joined: Tue Oct 14, 2014 3:49 am

How can I limit the number of emails when motion is detected?

Wed Mar 14, 2018 6:19 am

Hello,
I have a Raspberry 3 with its camera. I installed motion. When it detects motion, it saves 3-4 images in the target directory for pictures, but sends many emails, sometimes up to 20 or more.

I have configured it like this:

Code: Select all

on_motion_detected python /home/pi/motion/email_on_motion.py %f
How can I limit the number of emails when motion is detected?

User avatar
DougieLawson
Posts: 39576
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How can I limit the number of emails when motion is detected?

Wed Mar 14, 2018 8:36 am

Where did you get that python program from? It's cleary got some sort of loop in it.

Debugging by crystal ball doesn't work since the Great Crystal Ball Disaster of 1982.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

mugurdi
Posts: 68
Joined: Tue Oct 14, 2014 3:49 am

Re: How can I limit the number of emails when motion is detected?

Thu Mar 15, 2018 4:52 am

Hello,
The code to send email I got it from here:
https://github.com/mattgmg1990/raspberr ... _motion.py
I will have to add some delay after sending the message?

Code: Select all

#! /usr/bin/python
import smtplib
import os
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

MOTION_OUTPUT_DIRECTORY = '/home/pi/motion/images/'

SMTP_SERVER = 'smtp.gmail.com:587'

FROM_ADDRESS = "email_from.com"
TO_ADDRESSES = ["emai_to.com"]

COMMASPACE = ", "

# Credentials (might be better stored as an environment variable)
USERNAME = "email_from@gmail.com"
PASSWORD = "pasword"

def create_message():
    msg = MIMEMultipart()
    msg['Subject'] = 'Motion detected!'
    msg['From'] = 'The Pi'
    msg['To'] = COMMASPACE.join(TO_ADDRESSES)
    msg.preamble = "Motion detected."
    msg_text = MIMEText("look at the attached image")
    msg.attach(msg_text)

    # Get the last image taken and attach it
    filename = find_most_recently_modified_jpg_files(MOTION_OUTPUT_DIRECTORY)

    # print("\nfilename: %s" % filename)

    if not filename == '':
        image_file = open(MOTION_OUTPUT_DIRECTORY + '/' + filename, 'rb')
        img = MIMEImage(image_file.read())
        image_file.close()
        msg.attach(img)

    return msg

def send_email(msg):
    # Send the email
    server = smtplib.SMTP(SMTP_SERVER)
    server.starttls()
    server.login(USERNAME, PASSWORD)
    server.sendmail(FROM_ADDRESS, TO_ADDRESSES, msg.as_string())
    server.quit()

# Does not count or return paths to symlinks
def find_most_recently_modified_jpg_files(dir_path):
    max_mtime = 0
    max_file = ''
    for dirname,subdirs,files in os.walk(dir_path):
        for fname in files:
            full_path = os.path.join(dirname, fname)
            if os.path.islink(full_path) or not 'jpg' in full_path:
                continue
            mtime = os.stat(full_path).st_mtime
            if mtime > max_mtime:
                max_mtime = mtime
                max_dir = dirname
                max_file = fname
    return max_file

print("\nSending message...")
msg = create_message()
send_email(msg)
print("\nMessage sent!")


User avatar
DougieLawson
Posts: 39576
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How can I limit the number of emails when motion is detected?

Thu Mar 15, 2018 3:48 pm

That program can only ever send ONE email when it's called. So you need to determine why motion with the "on_motion_detected" parameter is calling it more than once.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Return to “General discussion”