Dahua IPC to MQTT App

Thank you very much. Works perfectly.

Does anybody has this working on a Dahua NVR?
I have the script working, nut my NVR has 5 camera’s attached…

Could you please post AppDaemon log?

The app has stopped working since I enabled HTTPS on my Dahua NVR. I assume HTTP should continue working, but it looks like the call is redirected and then I get a certificate error:

SSL certificate problem: unable to get local issuer certificate

I guess the solution would be to disable HTTPS again, but would there be a possibility to use HTTPS?

Yes, there should be, but I didn’t tried HTTPS on my cameras, so didn’t implemented it. Is your certificate self-signed?

Yes, self signed. I don’t know about the cameras themselves, but the NVR has an option to generate a certificate.

Ok, I’ll take a look. I guess component just needs option to accept self-signed certificate.

Could you pull updated app from my repository (here) and try setting ignore_ssl: True to configuration file?

Great work, @algirdasc! I am experiencing some issues with parsing the JSON payload, maybe because it contains a lot of “\n”?

{"code": "CrossRegionDetection", "action": "Stop", "index": "2", "data": "{\n   \"Action\" : \"Appear\",\n   \"Class\" : \"Normal\",\n   \"CountInGroup\" : 1,\n   \"DetectRegion\" : [\n      [ 346, 2473 ],\n      [ 2512, 1092 ],\n      [ 7754, 2249 ],\n      [ 7827, 4722 ],\n      [ 8045, 5204 ],\n      [ 3822, 6681 ],\n      [ 8100, 7773 ],\n      [ 8082, 8063 ],\n      [ 36, 8127 ],\n      [ 55, 5332 ],\n      [ 419, 5300 ]\n   ],\n   \"EventSeq\" : 84,\n   \"Extension\" : {\n      \"EventLongID\" : \"0220190507170948003539900000\"\n   },\n   \"FrameSequence\" : 21637811,\n   \"GroupID\" : 84,\n   \"IndexInGroup\" : 0,\n   \"LocaleTime\" : \"2019-05-07 17:09:51\",\n   \"Mark\" : 0,\n   \"Name\" : \"Voorkant\",\n   \"Object\" : {\n      \"Action\" : \"Appear\",\n      \"BoundingBox\" : [ 5840, 2400, 6432, 4448 ],\n      \"Center\" : [ 6136, 3424 ],\n      \"Confidence\" : 0,\n      \"FrameSequence\" : 0,\n      \"LowerBodyColor\" : [ 0, 0, 0, 0 ],\n      \"MainColor\" : [ 0, 0, 0, 0 ],\n      \"ObjectID\" : 840,\n      \"ObjectType\" : \"Human\",\n      \"RelativeID\" : 0,\n      \"Source\" : 0.0,\n      \"Speed\" : 0,\n      \"SpeedTypeInternal\" : 0\n   },\n   \"PTS\" : 43814814030.0,\n   \"RuleId\" : 1,\n   \"Sequence\" : 0,\n   \"Source\" : 55530608.0,\n   \"Track\" : null,\n   \"UTC\" : 1557248988,\n   \"UTCMS\" : 273\n}\n"}

Could you please specify what issues are you experiencing? A lot of “\n” might be from printing debuging info, which should be ignored and JSON should be parsed anyway.

I would like to parse out the “Name” field. I use dev-template to test my template.

Template:

{% set my_test_json = {"code": "CrossRegionDetection", "action": "Stop", "index": "2", "data": "{\n   \"Action\" : \"Appear\",\n   \"Class\" : \"Normal\",\n   \"CountInGroup\" : 1,\n   \"DetectRegion\" : [\n      [ 346, 2473 ],\n      [ 2512, 1092 ],\n      [ 7754, 2249 ],\n      [ 7827, 4722 ],\n      [ 8045, 5204 ],\n      [ 3822, 6681 ],\n      [ 8100, 7773 ],\n      [ 8082, 8063 ],\n      [ 36, 8127 ],\n      [ 55, 5332 ],\n      [ 419, 5300 ]\n   ],\n   \"EventSeq\" : 56,\n   \"Extension\" : {\n      \"EventLongID\" : \"0220190506205857002629900000\"\n   },\n   \"FrameSequence\" : 19821373,\n   \"GroupID\" : 56,\n   \"IndexInGroup\" : 0,\n   \"LocaleTime\" : \"2019-05-06 20:59:00\",\n   \"Mark\" : 0,\n   \"Name\" : \"Voorkant\",\n   \"Object\" : {\n      \"Action\" : \"Appear\",\n      \"BoundingBox\" : [ 1744, 4040, 2080, 4440 ],\n      \"Center\" : [ 1912, 4240 ],\n      \"Confidence\" : 0,\n      \"FrameSequence\" : 0,\n      \"LowerBodyColor\" : [ 0, 0, 0, 0 ],\n      \"MainColor\" : [ 0, 0, 0, 0 ],\n      \"ObjectID\" : 1460,\n      \"ObjectType\" : \"Human\",\n      \"RelativeID\" : 0,\n      \"Source\" : 0.0,\n      \"Speed\" : 0,\n      \"SpeedTypeInternal\" : 0\n   },\n   \"PTS\" : 43742164310.0,\n   \"RuleId\" : 1,\n   \"Sequence\" : 0,\n   \"Source\" : 55530608.0,\n   \"Track\" : null,\n   \"UTC\" : 1557176337,\n   \"UTCMS\" : 525\n}\n"} %}

{{ my_test_json.data }}

Then the result is:

{
   "Action" : "Appear",
   "Class" : "Normal",
   "CountInGroup" : 1,
   "DetectRegion" : [
      [ 346, 2473 ],
      [ 2512, 1092 ],
      [ 7754, 2249 ],
      [ 7827, 4722 ],
      [ 8045, 5204 ],
      [ 3822, 6681 ],
      [ 8100, 7773 ],
      [ 8082, 8063 ],
      [ 36, 8127 ],
      [ 55, 5332 ],
      [ 419, 5300 ]
   ],
   "EventSeq" : 56,
   "Extension" : {
      "EventLongID" : "0220190506205857002629900000"
   },
   "FrameSequence" : 19821373,
   "GroupID" : 56,
   "IndexInGroup" : 0,
   "LocaleTime" : "2019-05-06 20:59:00",
   "Mark" : 0,
   "Name" : "Voorkant",
   "Object" : {
      "Action" : "Appear",
      "BoundingBox" : [ 1744, 4040, 2080, 4440 ],
      "Center" : [ 1912, 4240 ],
      "Confidence" : 0,
      "FrameSequence" : 0,
      "LowerBodyColor" : [ 0, 0, 0, 0 ],
      "MainColor" : [ 0, 0, 0, 0 ],
      "ObjectID" : 1460,
      "ObjectType" : "Human",
      "RelativeID" : 0,
      "Source" : 0.0,
      "Speed" : 0,
      "SpeedTypeInternal" : 0
   },
   "PTS" : 43742164310.0,
   "RuleId" : 1,
   "Sequence" : 0,
   "Source" : 55530608.0,
   "Track" : null,
   "UTC" : 1557176337,
   "UTCMS" : 525
}

This however does not provide any result:

{{ my_test_json.data.Name }}

Yes, I see. The problem is that my_test_json.data is not parsed to JSON object, but to string instead. I’ve updated app, please update and let me know if the problem is resolved.

Great work, I have problem getting AudioAnomaly: input abnormal to work even when i see on Onvif Device Manager that there are events. Anyone got Audio Detected to work?

DahuaMQTT:
  class: DahuaMQTT
  module: dahua_mqtt
  cameras:

    - host: 192.168.1.113
      port: 80
      user: !secret living_room_camera_username
      pass: !secret living_room_camera_passwd
      topic: cameras/living_room/
      events: VideoMotion,CrossLineDetection,CrossRegionDetection,LeftDetection,TakenAwayDetection,FaceDetection,AudioMutation,AudioAnomaly,VideoBlind,LeftDetection,MoveDetection

eventCode can be any one of the standard codes defined in DHIIF, or “All”.
All means all kinds of the eventcode.
eventcode include:
VideoMotion: motion detection event
VideoLoss: video loss detection event
VideoBlind: video blind detection event.
AlarmLocal: alarm detection event.
CrossLineDetection: tripwire event
CrossRegionDetection: intrusion event
LeftDetection: abandoned object detection
TakenAwayDetection: missing object detection
VideoAbnormalDetection: scene change event
FaceDetection: face detect event
AudioMutation: intensity change
AudioAnomaly: input abnormal
VideoUnFocus: defocus detect event
WanderDetection: loitering detection event
RioterDetection: People Gathering event
ParkingDetection: parking detection event
MoveDetection: fast moving event
StorageNotExist: storage not exist event.
StorageFailure: storage failure event.
StorageLowSpace: storage low space event.
AlarmOutput: alarm output event.
MDResult: motion detection data reporting event. The motion detect window contains 18 rows
and 22 columns. The event info contains motion detect data with mask of every row.
HeatImagingTemper: temperature alarm event

Yes, has been resolved. Thanks!

A great way to troubleshot is
use the command that makes a log

wget 'http://USERNAME:PASSWORD@IP/cgi-bin/eventManager.cgi?action=attach&codes=[VideoMotion,VideoLoss,VideoBlind,AlarmLocal,CrossLineDetection,CrossRegionDetection,LeftDetection,TakenAwayDetection,VideoAbnormalDetection,FaceDetection,AudioMutation,AudioAnomaly,VideoUnFocus,WanderDetection,RioterDetection,ParkingDetection,MoveDetection,MDResult]' -O events.log

I get an error with that command. I’m using username=admin & password=somethingsomething

Error Invalid Authority!

Does anyone have this working with a Dahua NVR?
I have just stumbled across this by accident whilst looking for something else.

I have actually setup MotionEye JUST to capture motion detection events, hitting a webhook which in turn sets a binary sensor in HA - but it would be great to remove the extra complexity and load of running motioneye!

Read few post above, there was someone, who had issues with NVR, but it was solved and working.

Sorry - I shuld have clarified.
I have it working, and I am getting data posted to MQTT

Am i correct to assume the index value relates to the camera channel ?
ie index 0 is D1 and so on (it seems to be the case!)

Assuming that is the case, i made a change to add the index to the topic

ie cameras/1/VideoMotion gets hit when the payload is {“code”: “VideoMotion”, “action”: “Start”, “index”: “1”}

To me that makes it easier to have a simple on/off binary sensor for movement in HA.

just wondering, does the script at the top allow you to differentiate between multiple different events on the same camera.

like can i have an automation for motion and a different automation for face detection?