/mqtt-email

Primary LanguagePython

mqtt-email.py

Python code that translate email sent by Dahua CCTV EZIP & IPC camera in MQTT message.
Another variant of code that it will run as a local email server it is available here: https://github.com/rapi3/mqtt-email-srv

q: why ?
a: Because for Dahua EZIP camera don't exist until now an integration for IOT and you can't integrate in HA (untill now).

q: what it does ?
a: parse emails sent by cctv camera, search for strings defined and if found it publish MQTT message in camera topic.

q: can work for any camera ?
a: yes, you need to add string to find and it will work for any camera that can send email when it detect motion.

q: what do I need to run ?
a: You need to have your Postfix email server and MQTT server running and optional Home Assistant.

Some camera will send mesages encoded in MIME 64 format.
To decode email sent by camera if it is encoded MIME Base 64 use:

https://online-free-tools.com/en/base_64_encoder_decoder


Dahua EZIP camera when detect motion send email message (encoded) in this format:

Alarm Event: Motion Detection Start
Alarm Input Channel: 1
Alarm Start Time(D/M/Y H:M:S): 21/12/2021 22:15:33
Alarm Device Name: EZIP08
Alarm Name: 
IP Address: 192.168.2.155

we need to search for first line: "Alarm Event: Motion Detection Start"
encoded will be: "QWxhcm0gRXZlbnQ6IE1vdGlvbiBEZXRlY3Rpb24gU3RhcnQ"

Dahua IPC camera when detect motion send email message (encoded) in this format:

Alarm Event: Motion Detection
Alarm Input Channel: 1
Alarm Start Time(D/M/Y H:M:S): 23/12/2021 10:27:46
Alarm Device Name: IPC06
Alarm Name: 
IP Address: 192.168.2.164

we need to search for first line and part of seccond line.
encoded will be: "QWxhcm0gRXZlbnQ6IE1vdGlvbiBEZXRlY3Rpb24NCkFsYXJtIElucHV0IENoYW5uZWw"

No Name camera when detect motion send email message (not encoded) in this format:

	This is an automatically generated e-mail from your IPC.

		EVENT TYPE: Motion Detected
		EVENT TIME: 27/12/2021 17:42:19
		IPC NAME: IPC
		CHANNEL NUMBER: 01
		IPC IP: 192.168.2.111

we need to search for first line: "EVENT TYPE: Motion Detected"

when string it is found script will publish message Alarm in camera named topic: IOT/cctv/XXXX
sender-name it is taken from cctv email:

Return-Path: <XXXX@myhome.local

so you can have any nr of cameras sending messages to that email box and each camera will have his own topic.

You need to add in python code your MQTT server-ip, user & password.
Modify the script as appropriate.

Postfix

this is required for python code to be executed when mailbox usercctv will receive mail:

add in /etc/aliases
usercctv: |/etc/postfix/script/mqtt-email.py

Don't forget to run: postalias /etc/aliases and afterwards reload Postfix with postfix reload

Advice
Use one mailbox/postfix user for all cctv camera that send email alerts, because alarm start & alarm end messages sent to this mailbox will be processed by this script and will not be stored/saved on Postfix.
All other messages will be saved ( change the code as you want to save all + alarm start/stop ) in /tmp/ and can be moved to another mailbox with a simple bash script that run from crontab.

If you need to install and configure Postfix on rpi you can use this excelent tutorial from Sam Hobbs
https://samhobbs.co.uk/2014/03/raspberry-pi-email-server


Home Assistant - optional

you need to create binary sensor motion for every camera
There is no need to send message when Alarm is off because the sensor created will auto change state to off after 60s

# CCTV EZIP08 Alarm
- platform: mqtt
  state_topic: "IOT/cctv/ez08"
  name: "CCTV-EZ08"
  off_delay: 60
  payload_on: "Alarm"
  unique_id: 333-00000000844444444444
  device_class: motion

Home Assistant motion sensors

Tags: CCTV, Dahua, MQTT, email, IOT, Tasmota