Dahua SMD alerts over Apple HomeKit

IGx89

n3wb
Joined
Mar 27, 2021
Messages
5
Reaction score
4
Location
Minneapolis, MN
Hello! I recently purchased my first IP camera (Dahua N43AJ52) and have started working on integrating it into my existing home automation setup. I've been successful at getting the camera working in Apple HomeKit through Homebridge, notably with support for Dahua's smart motion detection (SMD) feature that allows motion detection to be filtered on the camera to just trigger on people and/or vehicles. I now get push notifications on my Apple devices, complete with a snapshot image, whenever a person or vehicle moves on my driveway.

Here's how I accomplished that, in case it helps others:

1. Set up a Homebridge server (I'm using a Raspberry Pi running their slick official image)
2. Install the homebridge-camera-ffmpeg plugin, configuring it with your camera(s) and localhttp enabled (see below for my config for pointers)
3. Install the homebridge-dahua-alerts plugin, also configuring it with your camera info
4. Modify the dahua plugin's JavaScript to have it listen for smart motion events instead of the normal ones. This is done by editing /usr/local/lib/node_modules/homebridge-dahua-alerts/dist/dahua.js (if you're using Linux), replacing the text "codes=[VideoMotion]" with "codes=[SmartMotionHuman%2CSmartMotionVehicle]" (I've filed a feature request with the author to incorporate that functionality into the plugin)

That should be it! End result should be the camera snapshot showing in your Home app, with a live feed showing when you tap it, that also sends you push notifications upon smart motion alerts. If you don't get alerts and have confirmed everything's set up right in your config, verify that you've enabled SMD on your camera(s) as well.

My (censored) Homebridge config:

JavaScript:
{
    "platforms": [
        {
            "name": "Camera FFmpeg",
            "porthttp": 8088,
            "localhttp": true,
            "cameras": [
                {
                    "name": "Driveway",
                    "manufacturer": "Dahua",
                    "model": "N43AJ52",
                    "motion": true,
                    "motionTimeout": 0,
                    "unbridge": true,
                    "videoConfig": {
                        "source": "-rtsp_transport tcp -i rtsp:/192.168.0.123:554/cam/realmonitor?channel=1&subtype=2&unicast=true&proto=Onvif",
                        "stillImageSource": "-i http://user:pass@192.168.0.123/cgi-bin/snapshot.cgi?1",
                        "maxWidth": 0,
                        "maxHeight": 0,
                        "maxFPS": 0,
                        "maxBitrate": 3000,
                        "forceMax": true,
                        "vcodec": "copy",
                        "packetSize": 1316,
                        "audio": true,
                        "debug": false
                    }
                }
            ],
            "platform": "Camera-ffmpeg"
        },
        {
            "homebridgeCameraFfmpegHttpPort": 8088,
            "cameras": [
                {
                    "index": 0,
                    "cameraName": "Driveway",
                    "cameraCredentials": {
                        "host": "192.168.0.123",
                        "user": "admin",
                        "pass": "admin"
                    }
                }
            ],
            "platform": "dahua-alerts"
        }
    ]
}
 

psycik

Getting the hang of it
Joined
Dec 9, 2015
Messages
198
Reaction score
31
Location
Wellington, New Zealand
It's a pity that dahua monitor can't get access to the playback link of a motion event like the native dmss app can (or does when notifications are working propery).
 

generalinq

Young grasshopper
Joined
Oct 2, 2020
Messages
38
Reaction score
20
Location
Canada
I tried the HomeBridge + Raspi 4 with my 5216-16P-4KS2E (for camera IPC-T5442TM-AS). Didn't work very well. I took the Docker Image approach for HomeBridge. HomeBridge is working fine for other plugins, cpu utilization on my Pi is only 1%, lots of memory left. The pi was able to send me the snapshots to my home app on my phone and MAC but when I try to click on the image for a 'live' feed, this is where everything falls apart. The debug logs in homebridge show several dropped frames, some buffer not big enough error, etc. The PI spikes to 50% CPU instantly. I used your json settings and also tinkered with the settings myself to lower the bitrate, fps, etc. If I don't lower several of the settings, I start to also get the following siumilar errors in the homebridge logs which result in the feed cutting in/out constantly:

[Driveway] [Snapshot] [hevc @ 0x31fa6c0] Could not find ref with POC 4

Once I do end up getting the feed to display without cutting in out (by adjusting some of the aforementioned settings), it only updates the live feed every 7-12 seconds, which is useless.

FYI: This directory did not exist as you mentioned: /usr/local/lib/node_modules/homebridge-dahua-alerts (nor could I locate a dahua.js file anywhere on the filesystem in my docker container via HomeBridge).

Even once getting the live feed to display ever 7 seconds, I still wasn't seeing any options in HomeBridge or in Home app that allows me setup any additional motion detection or automations? Is this plugin just for the 'live' feed? Maybe my camera or NVR isn't supported (even though its one of the most common Dahua items)

Anyhow, it this actually works, that would be really awesome.
 

IGx89

n3wb
Joined
Mar 27, 2021
Messages
5
Reaction score
4
Location
Minneapolis, MN
Sorry you're having so many problems! I've had a VERY frustrating time myself getting a smooth live stream from my Dahua camera -- it's like FFMPEG and Dahua hate each other :/. I've actually since switched from Homebridge to Scrypted and am getting a better experience -- it has an ONVIF plugin that, when coupled with their HomeKit plugin, automates most of the setup (and supports HomeKit Secure Video!). I've also added Synology Surveillance Station into the mix and have written a plugin for Scrypted to automate setup of cameras from that NVR, though that's not necessary -- it mostly just speeds up snapshots (Dahua's snapshot service is SLOW).

To address a few of your problems, in case it helps:

1. Your dropped frames issue sounds almost like you're using the UDP transport and not TCP. But if you're using my settings, you should be on TCP. Don't really know what to suggest, except maybe restarting your camera -- that sometimes has helped me (mine gets pretty laggy sometimes, seems like it's out of RAM or something).
2. Did you install the homebridge-dahua-alerts plugin? That's necessary for motion alerts -- that's all it does. You can skip step #4 above if you're OK with basic motion alerts and not SMD
 

generalinq

Young grasshopper
Joined
Oct 2, 2020
Messages
38
Reaction score
20
Location
Canada
Ya somehow I missed the part that transport part in the rtsp call. I added it. Your right, the dropped frames are gone. But ffmeg will stop displayed the stream after a few seconds with the following message in the log:
[11/22/2021, 3:54:24 PM] [Camera FFmpeg] [Driveway] FFmpeg exited with code: 0 and signal: null (Expected)

Only way to get the stream back up is the recycle HomeBridge (and the same thing keeps happening). I'll checkout Scrypted (thanks). I'm also thinking of toying around with Home Assistant as a general IoT management.

I also get the following in the log (not sure if you noticed this as well when you were testing):
[11/22/2021, 4:00:38 PM] [dahua-alerts] Error received from host: 192.168.30.6 (for more info enable debug logging)

My Cameras are in their own VLAN so I also wonder if the segregation causes additional latency. My home bridge server is in a different VLAN than my Cameras/NVR.
 
Top