Getting IP camera motion events into Home Assistant to trigger things

Oh this is exciting! I’ve been trying to figure out how to get motion events from my IP camera into my home-assistant instance running on my Raspberry Pi, and I just did a successful test! It works! Hooray. Briefly, I set up an email server on the Pi, have the camera email the Pi, have the email server trigger a script which parses the email for key words and sends MQTT signals as appropriate, at which point the home-assistant MQTT client sees them and triggers automations (like blinking a light to scare people off). Here’s how I did it.

Camera-based motion sensors in home-assistant

Set up an email server on the Raspberry Pi

I set up a LAN-only postfix email server on my Raspberry Pi with

and chose the “internet” choice so it wasn’t just local to the Pi. Since I don’t forward any ports from the real internet to the Pi, this is OK because it’s isolated on my LAN behind my router.

After some basic config, I tested the system’s ability to receive emails by sending them from my laptop using Telnet:

Note: You can automate this with autoexpect.

NOTE: Do not do this on a public server! If you have any access to the pi from the internet this can put you at risk of becoming a spam drone or worse. If you want set up a legit public email server, you have to do a lost more work. 

Set up email motion alerts from the camera

Most cameras (foscam, amcrest, etc.) have email options on motion. Have them send an email to the raspberry pi. Trigger the motion event and look at what the emails look like so you can figure out what to read from them to trigger motion alarms and maybe all clears. I used mutt to read emails on the pi, but you can also just view /var/mail/pi with a text editor if you want. Once you’ve gotten a few emails and can see key phrases in them, you’re ready for the next step.

Set up a MQTT server somewhere and a binary sensor

Home-assistant has some nice info on setting up a MQTT broker. You can use the built-in one (I couldn’t get it working) or use one on an ubuntu server (my choice since I have a webserver), or you can install one on the pi itself. See here for more info. This will be the conduit home-assistant reads the signals in from and triggers automations, etc. Here is my config:

Set up automations with the binary sensor now.

Make a script to parse the incoming emails and trigger MQTT updates

Now make a script that can parse an email from the camera, scan it for key phrases, and trigger MQTT publishing as necessary. I installed the mosquitto package on my pi to get access to the publishing command. My script looks like this:

Your will probably look different. This triggers an alarm on an older Foscam camera. Note that I had to actually decode the subject line which was encoded for MIME. For Amcrest, the whole message is MIME encoded so you’ll have to figure your camera out. This Python email doc page was useful. For Amcrest cameras, my code ended up having lines like these:

Giving an email result somewhat like this:

Anyway, save that file as /home/pi/camera/alert.py and chmod 755 it to make it executable. Now you’re on to the final step.

Tell your email server to pass incoming emails to the script for potential signaling

In a nutshell, follow the instructions from this page to point postfix to your script. This will call your script and dump the incoming email to its stdin (which is why we read stdin in the script).
Note that once you do this, you will not get the emails in the inbox anymore. But you don’t need them if this is all you’re using the email server for. If you want to have multiple scripts and still get the email, then you have to work harder and set up multiple smtp daemons on different ports and pass them around.

10 thoughts on “Getting IP camera motion events into Home Assistant to trigger things”

  1. Hi.. Can you tell a little about configuring mail in raspberry pi in the config used of mail in foscam. I am unable to install postfix properly.

    1. Hi. First just do apt-get install postfix and edit /etc/postfix/main.cf. I turned of TLS and set my hostname and whatnot. Then see if you can send mail to the Pi from the foscam (check mail with mutt or something similar). Then set up the pipe to the script in /etc/postfix/master.cf if you want to complete the final link.

  2. Thanks for the quick reply. I will try to setup it. I am totally new to linux and home-assistant 🙂

    1. Still stuck on postfix configuration. I have installed postfix and dovecot but still have some doubts. Do i have to get a external domain name (*.com) or i have to configure local hostname on my pi. If we use local hostname then what will we use in SMTP setting for foscam camera. I apologies for the questions but still figuring home-assistant and raspbian. Can you please guide the website or instruction in layman terms.

  3. I’m so glad I found your post! I managed to get this working based on your write-up.

    BTW, I’m using the built-in MQTT broker. I’m not sure what specific issue you ran into with that, since so many things could have gone wrong. For me, HTTPS is enabled on my Home Assistant, which I suspect causes an issue with a simple mosquitto_pub call. Instead, I use Home Assistant’s MQTT publish HTTP API to publish MQTT through Postfix.

    Thanks again for sharing the idea and the write-up!

  4. This is exactly what I was looking for, thanks so much!

    Only minor notifications needed for my Foscam FI9821P v3 (s/motion/detected). I’ve wired mine up to HTML5 notifications which works fairly well.

  5. I setup mine via command line sensor, this way its a lot quicker activating as I cut out round trip time, email, mqqt, ifttt or whatever else. The following works for the Vstarcam C2784WIP. I use a similar method to move the camera via homeassistant also so I can react to the alarms or setup camera POV based on events.

    – platform: command_line
    name: “camera bedroom motion”
    device_class: switch
    command: curl -k –silent “http://:/get_status.cgi?loginuse=&loginpas=” | grep -oP “(alarm_status=).*?(;)”
    value_template: >-
    {%- if value == “alarm_status=0;” -%}
    off
    {%- elif value == “alarm_status=1;” -%}
    on
    {%- endif -%}
    scan_interval: 1.5

  6. Comments have cut out IP, port, username and password from the url. But you can imagine where they go.

  7. To bad you did not share the whole python script for Amcrest cameras to help many old people like me that don’t have enough knowledge of python programming to make this script to work with is cameras… 🙁
    I succeed to integrate my cameras in HA with imap_email_content but it is to slow and buggy so not really usable if time it is critical for notification and automation 🙁

Leave a Reply

Your email address will not be published. Required fields are marked *