Dahua IPC to MQTT App

For a moment I connected esp8266 espeasy to receive alarm out triggers from my cameras.
The only pro - is that not working (for actual moment) appdaemon solution slows down much my Hassio pi: from Config check to restart time.
To much packages maybe…

Appdaemon normally has a very low footprint.
do you have anything else in appdaemon then this app?
it also has normally NO impact on the starting time from home assistant.
so maybe you have something else going on.

Appdaemon Hello only) my problem is: full re-install of every appdaemon (python and system) package after Hassio restart

hmm, thats not as it should be, but can be hassio problem.

after that all required parts are installed you could try if you could delete the stuff in the requirements.txt
it is installed, so it shouldnt be neccesary to have it there anymore.

Sounds good, but no info on “cameras/1/VideoMotion” topic…
Log is like I quoted before
Need to ask for news @xbmcnut

if you dont get anything on mqtt, you probably dont get anything from the camera.

i dont have experience with such camera, but the app starts a loop then continiously seems to check some urls. (which by the way makes a heavy load on the RPI and your network)

i see this url
“http://{host}:{port}/cgi-bin/eventManager.cgi?action=attach&codes=%5B{events}%5D”

so you could see what data you get on that url.

Just another idea; my Hassio is SLL protected “outside” by duckdns etc , does it make any sense to trying to connect to cameras in local net(I mean cam 80 port is blocked by my router from outside requests). Maybe I should make routing ports for cam 80 port or so, and in this case it is too complicated and unsecure (some Dahua models have problems with security)
I am real dumb in docker, python and so on ,( however having few hundreds of automatisations, devices and sensors)) maybe I want to realize impossible scenario…
I can not understand clearly how script is receiving data from camera, if I could try it in local net by Get or something request, I could be more “seeing”. Now feel myself as blind… due to lack of knowledges. Good luck to others))
I am now far away from cams - could check http request by tomorrow. Thanks for! [Bought 3 bottles Scicilian wine, bought not enough…]

your camera should not be reachable from the outside at all.
hassio is local and you cam is local and appdaemon is local, so they need to connect local, and you access only 1 thing from the outside (hassio or better a proxy server)

the script uses curl to get information from this url
“http://{host}:{port}/cgi-bin/eventManager.cgi?action=attach&codes=%5B{events}%5D”
where host, port and events are replaced.

so replace host, port and events and use the url in a browser, to see if you get any result.

and indeed you need to be on your homenetwork to get such stuff wotrking correctly.

Need an example for {events}. VideoMotion??

And… if I delete all this stuff from appdaemon config ( “log_level”: “info”,
“system_packages”: [
“libcurl”,
“python3-dev”,
“curl-dev”,
“gcc”
],
“python_packages”: [
“pip==19.0.2”,
“pycurl”)
Could it be “still enabled” in my setting?? I do not understand the physics of such setting present/not present in config… Deleted from so daemon config = still working and available packages?

All this “asspain” is only for my beloved scenario in ex-domoticz : when people are outside the door area, door is colored to purple by LED… so it means I need better wait to go with garbage outside without pants or similar… suffer much now. Everybody saw me with garbage and without pants for last months…

any event that is in the list of events you got.

i dont know. but its very well possible because the packages are installed.
just backup the file and try.

lol.
you could always put on pants to avoid that :wink:
or maybe look out a window?

I’ve added VideoMotion now but I still don’t see events in MQTT.fx. That URL returns nothing so not quite sure what is going on. Might have to wait to see if someone who has it working has got some clues.

Event URL makes long-poll responses, so it is not making a lot requests and not utilizing cpu much. By provided logs I guess that wrong credentials / auth mode is configured. Could you please double check?

I’ve got some success with two cameras! No wire cross events but I’m getting start and stop payloads from VideoMotion so it’s a start. I’m wondering if I need to run through the Dahua calibration process (which is not well documented) before the ‘IVS’ features start working?

I worked as a product specialist in the video surveillance industry for 15yrs so I’m guessing I should be able to figure it out! No pressure. :roll_eyes:

image

I started seeing “empty reply from server” which was new so I was hopeful it would start working.

The CrossLineDetection does work here with my dahua camera. Did you draw the rule inside the camera gui?

In AppDaemon logs i can see the content of the CrossLineDetection.

2019-02-14 09:12:44.443190 INFO DahuaEvents: [192.168.1.xxx] code: CrossLineDetection, action Start, data: {'Class': 'Normal', 'CountInGroup': 1, 'DetectLine': [[2658, 4729], [6243, 2333]], 'Direction': 'LeftToRight', 'EventSeq': 1, 'FrameSequence': 11840702, 'GroupID': 1, 'IndexInGroup': 0, 'Mark': 0, 'Name': 'Visitor', 'Object': {'Action': 'Appear', 'BoundingBox': [1952, 144, 5760, 8208], 'Center': [3856, 4176], 'Confidence': 0, 'FrameSequence': 0, 'MainColor': [0, 0, 0, 0], 'ObjectID': 2670, 'ObjectType': 'Vehicle', 'RelativeID': 0, 'Source': 0.0, 'Speed': 0, 'SpeedTypeInternal': 0}, 'PTS': 43146937590.0, 'RuleId': 2, 'Sequence': 0, 'Source': 44149144.0, 'Track': None, 'UTC': 1550135563, 'UTCMS': 794}

I see the OnConnect and OnDisconnect in your logs. I had this in the beginning, but i don’t know what fixed this.

Yes, thank you. All working now. My test camera was lagging real-time by about 30s which a reboot fixed. I also noted that the publish topic should ideally have a / at the end to separate the different detection methods into their own topics? e.g. cameras/1/.

I am now seeing CrossLineDetection events in the logs as you do. The settings in the camera require quite some tweaking to get them working correctly as placing the wirecross line near the edges on the scene is not ideal. The ideal location is directly in the middle of the field-of-view, especially if you expecting detection in both directions.

2019-02-16 12:43:28.409244 INFO DahuaMQTT: [10.0.1.14] Parse Event ({'code': 'CrossLineDetection', 'action': 'Start', 'index': '0'})
2019-02-16 12:43:28.413163 INFO DahuaMQTT: [10.0.1.14] Alarm triggered: {'code': 'CrossLineDetection', 'action': 'Start', 'index': '0'}
2019-02-16 12:43:30.940902 INFO DahuaMQTT: [10.0.1.14] Parse Event ({'code': 'CrossLineDetection', 'action': 'Stop', 'index': '0'})
2019-02-16 12:43:30.944696 INFO DahuaMQTT: [10.0.1.14] Alarm triggered: {'code': 'CrossLineDetection', 'action': 'Stop', 'index': '0'}

Ok I have everything working here right now. The python script is publishing MQTT messages and I am able to trigger automations based on these.

My issue however is that I am connecting to a NVR, to which 3 cameras have been connected. I only have one “camera” configured in the apps.yaml configuration, which is the NVR. The messages from the Dahua NVR contain a lot of information:

{'code': 'CrossLineDetection', 'action': 'Start', 'index': '0', 'data': '{\n   "Class" : "Normal",\n   "CountInGroup" : 1,\n   "DetectLine" : [\n      [ 910, 2868 ],\n      [ 2912, 6708 ]\n   ],\n   "Direction" : "RightToLeft",\n   "EventSeq" : 21,\n   "Extension" : {\n      "EventLongID" : "0220190412042750000689900000"\n   },\n   "FrameSequence" : 19556443,\n   "GroupID" : 21,\n   "IndexInGroup" : 0,\n   "LocaleTime" : "2019-04-12 04:27:50",\n   "Mark" : 0,\n   "Name" : "Rule3",\n   "Object" : {\n      "Action" : "Appear",\n      "BoundingBox" : [ 1664, 0, 4736, 6496 ],\n      "Center" : [ 3200, 3248 ],\n      "Confidence" : 0,\n      "FrameSequence" : 0,\n      "LowerBodyColor" : [ 0, 0, 0, 0 ],\n      "MainColor" : [ 0, 0, 0, 0 ],\n      "ObjectID" : 928,\n      "ObjectType" : "Human",\n      "RelativeID" : 0,\n      "Source" : 0.0,\n      "Speed" : 0,\n      "SpeedTypeInternal" : 0\n   },\n   "PTS" : 43731566620.0,\n   "RuleId" : 4,\n   "Sequence" : 0,\n   "Source" : 54746768.0,\n   "Track" : null,\n   "UTC" : 1555043270,\n   "UTCMS" : 753\n}\n'}

E.g. the rule ID is in there.

As I only have one camera configured, I only have one MQTT topic. The MQTT message payload only is “start”. I really would like to have more information, such as the channel ID or the rule ID, in order to create more specific automations.

Any suggestions to achieve that?

Maybe appending data field JSON to payload would help? Would you be able to parse JSON then?

I’ve updates app. Now it sends two MQTT topics:
First MQTT topic will be: cameras/1/<event>, ex: cameras/1/VideoMotion and payload will be action: Start or Stop
Second MQTT topic will be: cameras/1, ex: cameras/1 and payload will be data received from camera in JSON format for you to parse.

1 Like

I have dahua IPC-HFW1300S and IPC-HFW1320S cameras.
I have a message with the IPC-HFW1300S camera -
2019-04-15 00: 33: 12.029320 INFO DahuaMQTT: [192.168.1.100] OnDisconnect (Success [])

However, with IPC-HFW1320S. seems to be ok -
2019-04-15 00: 42: 50.970780 INFO DahuaMQTT: [192.168.1.103] Publishing MQTT. topic = cameras / 1, payload = {"code": "VideoMotion", "action": "Start", "index": "0"}

The settings are the same - can it be a matter of firmware?

How can I run an automation based on motion detection? Can someone show me an example of an automation code? I would like to start the switch after motion detection.

Personally, I’ve created binary sensors:

    - platform: mqtt
      name: motion_cam01
      state_topic: cameras/cam01
      payload_on: Start
      payload_off: Stop
      value_template: '{{ value_json.action }}'
      device_class: motion

And then you can create automation on binary_sensor.motion_cam01 on / off state.