Dahua IPC to MQTT App

I tested just with pycurl in one line and removed/re-added container and in the log I get this error now…so I guess some progress but still throws error :slight_smile:

Collecting pycurl
Downloading pycurl-7.43.0.5.tar.gz (216 kB)
ERROR: Command errored out with exit status 1:

command: /usr/local/bin/python -c ‘import sys, setuptools, tokenize; sys.argv[0] = ‘"’"’/tmp/pip-install-y9w2q9v7/pycurl/setup.py’“'”‘; file=’“'”‘/tmp/pip-install-y9w2q9v7/pycurl/setup.py’“'”‘;f=getattr(tokenize, ‘"’“‘open’”’“‘, open)(file);code=f.read().replace(’”‘"’\r\n’“'”‘, ‘"’"’\n’“'”‘);f.close();exec(compile(code, file, ‘"’“‘exec’”’"‘))’ egg_info --egg-base /tmp/pip-install-y9w2q9v7/pycurl/pip-egg-info
cwd: /tmp/pip-install-y9w2q9v7/pycurl/
Complete output (22 lines):
Traceback (most recent call last):
File “/tmp/pip-install-y9w2q9v7/pycurl/setup.py”, line 233, in configure_unix
p = subprocess.Popen((self.curl_config(), ‘–version’),

File “/usr/local/lib/python3.8/subprocess.py”, line 854, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File “/usr/local/lib/python3.8/subprocess.py”, line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: ‘curl-config’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “”, line 1, in
File “/tmp/pip-install-y9w2q9v7/pycurl/setup.py”, line 961, in
ext = get_extension(sys.argv, split_extension_source=split_extension_source)
File “/tmp/pip-install-y9w2q9v7/pycurl/setup.py”, line 623, in get_extension
ext_config = ExtensionConfiguration(argv)
File “/tmp/pip-install-y9w2q9v7/pycurl/setup.py”, line 101, in init
self.configure()
File “/tmp/pip-install-y9w2q9v7/pycurl/setup.py”, line 238, in configure_unix
raise ConfigurationError(msg)
main.ConfigurationError: Could not run curl-config: [Errno 2] No such file or directory: ‘curl-config’

ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

Hi all,

Thank you so much for your work.
Everything starts normally appdaemon but I don’t see anything displayed in my Hass dashboard
I think I must be missing some configs, be forgiving I am new with home automation and Home assistant.
I put dahua_mqtt.py in / config / appdaemon / apps and in /config/configuration.yaml I put this code is that what you need to do or is something missing?

DahuaMQTT:
class: DahuaMQTT
module: dahua_mqtt
cameras:
- host: 192.168.1.20
port: 80
user: admin
pass: nvr5216
topic: cameras/1
events: CrossRegionDetection
- host: 192.168.1.20
port: 80
user: admin
pass: nvr5216
topic: cameras/2
events: CrossRegionDetection

screen_1

Thanks for your help

I forgot to post the Appdaemon log :grinning:

Log level is set to INFO
[cont-init.d] 01-log-level.sh: exited 0.
[cont-init.d] appdaemon.sh: executing…
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/armv7/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/armv7/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/edge/main/armv7/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/edge/community/armv7/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/edge/testing/armv7/APKINDEX.tar.gz
v3.11.5-51-g38120e013c [Index of /alpine/v3.11/main/]
v3.11.5-45-g7cf28d09bf [Index of /alpine/v3.11/community/]
v20200319-2083-g99308c8d8a [Index of /alpine/edge/main/]
v20200319-2088-g5df638ef62 [Index of /alpine/edge/community/]
v20200319-2089-g9b7db63898 [Index of /alpine/edge/testing/]
OK: 26287 distinct packages available
OK: 73 MiB in 34 packages
(1/2) Installing pkgconf (1.6.3-r0)
(2/2) Installing python3-dev (3.8.2-r0)
Executing busybox-1.31.1-r9.trigger
OK: 113 MiB in 36 packages
(1/4) Installing openssl-dev (1.1.1d-r3)
(2/4) Installing nghttp2-dev (1.40.0-r0)
(3/4) Installing zlib-dev (1.2.11-r3)
(4/4) Installing curl-dev (7.67.0-r0)
Executing busybox-1.31.1-r9.trigger
OK: 115 MiB in 40 packages
(1/9) Installing libstdc++ (9.2.0-r4)
(2/9) Installing binutils (2.33.1-r0)
(3/9) Installing gmp (6.1.2-r1)
(4/9) Installing isl (0.18-r0)
(5/9) Installing libgomp (9.2.0-r4)
(6/9) Installing libatomic (9.2.0-r4)
(7/9) Installing mpfr4 (4.0.2-r1)
(8/9) Installing mpc1 (1.1.0-r1)
(9/9) Installing gcc (9.2.0-r4)
Executing busybox-1.31.1-r9.trigger
OK: 167 MiB in 49 packages
OK: 167 MiB in 49 packages
(1/1) Installing make (4.2.1-r2)
Executing busybox-1.31.1-r9.trigger
OK: 167 MiB in 50 packages
(1/4) Upgrading musl (1.1.24-r0 → 1.1.24-r2)
(2/4) Installing musl-dev (1.1.24-r2)
(3/4) Installing libc-dev (0.7.2-r0)
(4/4) Installing g++ (9.2.0-r4)
Executing busybox-1.31.1-r9.trigger
OK: 226 MiB in 53 packages
Looking in links: https://wheels.hass.io/alpine-3.11/armv7/
Collecting pip==20.0.2
Using cached https://files.pythonhosted.org/packages/54/0c/d01aa759fdc501a58f431eb594a17495f15b88da142ce14b5845662c13f3/pip-20.0.2-py2.py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 19.2.3
Uninstalling pip-19.2.3:
Successfully uninstalled pip-19.2.3
Successfully installed pip-20.0.2
Looking in links: https://wheels.hass.io/alpine-3.11/armv7/
Collecting pycurl
Downloading pycurl-7.43.0.5.tar.gz (216 kB)
Installing collected packages: pycurl
Running setup.py install for pycurl: started
Running setup.py install for pycurl: finished with status ‘done’
Successfully installed pycurl-7.43.0.5
[cont-init.d] appdaemon.sh: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.
[10:48:18] INFO: Starting AppDaemon…
2020-04-14 10:48:22.631765 INFO AppDaemon: AppDaemon Version 4.0.3 starting
2020-04-14 10:48:22.632901 INFO AppDaemon: Python version is 3.8.2
2020-04-14 10:48:22.634060 INFO AppDaemon: Configuration read from: /config/appdaemon/appdaemon.yaml
2020-04-14 10:48:22.635147 INFO AppDaemon: Added log: AppDaemon
2020-04-14 10:48:22.636211 INFO AppDaemon: Added log: Error
2020-04-14 10:48:22.637170 INFO AppDaemon: Added log: Access
2020-04-14 10:48:22.638122 INFO AppDaemon: Added log: Diag
2020-04-14 10:48:22.708820 INFO AppDaemon: Loading Plugin HASS using class HassPlugin from module hassplugin
2020-04-14 10:48:22.783222 INFO HASS: HASS Plugin Initializing
2020-04-14 10:48:22.784921 INFO HASS: HASS Plugin initialization complete
2020-04-14 10:48:22.787575 INFO AppDaemon: Initializing HTTP
2020-04-14 10:48:22.789993 INFO AppDaemon: Using ‘ws’ for event stream
2020-04-14 10:48:22.845298 INFO AppDaemon: Starting API
2020-04-14 10:48:22.866388 INFO AppDaemon: Starting Admin Interface
2020-04-14 10:48:22.868628 INFO AppDaemon: Starting Dashboards
2020-04-14 10:48:22.916437 INFO HASS: Connected to Home Assistant 0.108.3
2020-04-14 10:48:23.009245 INFO AppDaemon: App ‘hello_world’ added
2020-04-14 10:48:23.011665 INFO AppDaemon: App ‘DahuaMQTT’ added
2020-04-14 10:48:23.015848 INFO AppDaemon: Found 2 total apps
2020-04-14 10:48:23.018405 INFO AppDaemon: Starting Apps with 2 workers and 2 pins
2020-04-14 10:48:23.024358 INFO AppDaemon: Running on port 5050
2020-04-14 10:48:23.095656 INFO HASS: Evaluating startup conditions
2020-04-14 10:48:23.137091 INFO AppDaemon: Got initial state from namespace default
2020-04-14 10:48:25.044254 INFO AppDaemon: Scheduler running in realtime
2020-04-14 10:48:25.060805 INFO AppDaemon: Adding /config/appdaemon/apps to module import path
2020-04-14 10:48:25.069340 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/hello.py
2020-04-14 10:48:25.089641 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/dahua_mqtt.py
2020-04-14 10:48:25.108853 INFO AppDaemon: Initializing app hello_world using class HelloWorld from module hello
2020-04-14 10:48:25.114163 INFO AppDaemon: Initializing app DahuaMQTT using class DahuaMQTT from module dahua_mqtt
2020-04-14 10:48:25.456002 INFO hello_world: Hello from AppDaemon
2020-04-14 10:48:25.460609 INFO hello_world: You are now ready to run Apps!
2020-04-14 10:48:25.472942 INFO DahuaMQTT: Starting thread
2020-04-14 10:48:25.482114 INFO AppDaemon: App initialization complete

i don’t use this app, but from the description in the inital post, it doesn’t say anything about seeing something in HA Dashboard. This is just an app that translates the camera data from Dahua events and translates them to MQTT.

Your config looks fine and there’s also no error in the logs you posted. For me it looks like it works.

Hi Burningstone,

Thank you for your reply.
And how can you consult these events which are sent to MQTT or verify that everything is going well?

Do you have MQTT set up in Home Assistant? If so, gO to Developer Tools -> MQTT and there enter # in the “Listen to a topic” box and press “START LISTENING”. You should now see the events there. In case you don’t have MQTT in Home Assistant, you should probably get an error in Home Assistant and/or AppDaemon.

Yes i have Mosquitto broker but i can’t see any events in Developer Tools

Do you see any error logs in AppDaemon or Home Assistant?

Yes i have this error:
2020-04-14 19:20:46 ERROR (MainThread) [homeassistant.components.hassio] Component error: DahuaMQTT - Integration ‘DahuaMQTT’ not found.

Thanks for your help

It appears that this integration doesn’t handle cameras connected through NVR, because it doesn’t allow me to input a channel. Any suggestions for how i can change the code to handle camera channels?

For latecomers to this discussion, AppDaemon 4.x doesn’t seem to accept a requirements.txt file (at least that’s what I’ve found on hassio when AD is installed from the Add-on store).

What it does accept is a YAML formatted specification, as per the example in the Configuration section of the add-on documentation (under the Documentation tab of the plug-in). This is added to the Configuration tab.

Mine now looks like this:

system_packages:
  - ffmpeg
  - libcurl
  - python3-dev
  - curl-dev
  - gcc
  - g++
python_packages:
  - pip>=19.0.2
  - pycurl
init_commands: []
log_level: info

Somehow broken (after months working ok), maybe some update.

ERROR: unsatisfiable constraints:
breaks: musl-dev-1.1.24-r10[musl=1.1.24-r10]
satisfies: world[musl=1.1.24-r9]

[21:23:47] FATAL: Failed installing package g++
[cont-init.d] appdaemon.sh: exited 1.

In case anyone is looking to use this with your recorder (DVR, XVR, NVR) not the camera itself change your apps.yaml to the following:

topic: cameras/1

to

topic: cameras

You should get JSON strings from any events triggered.

 

VideoMotion example:

{"code": "VideoMotion", "action": "Start", "index": "5", "data": {"SmartMotionEnable": false}}"

 

CrossRegionDetection example:

{"code": "CrossRegionDetection", "action": "Start", "index": "0", "data": {"Action": "Appear", "Class": "Normal", "CountInGroup": 1, "DetectRegion": [[1493, 1815], [1256, 4051], [1620, 4197], [5333, 2884], [4969, 308], [1547, 1815]], "EventSeq": 1160, "FrameSequence": 2942465, "GroupID": 0, "IndexInGroup": 0, "Mark": 0, "Name": "Rule4", "Object": {"Action": "Appear", "BoundingBox": [848, 2384, 1952, 4096], "Center": [1400, 3240], "Confidence": 0, "FrameSequence": 0, "ObjectID": 6500, "ObjectType": "Unknown", "RelativeID": 0, "Source": 0.0, "Speed": 0, "SpeedTypeInternal": 0}, "PTS": 62310.0, "RuleId": 3, "Sequence": 0, "Source": 0.0, "Track": null, "UTC": 1607268237, "UTCMS": 0}}

 

So we are interested mainly in the following:

  • “code”: Type of event

  • “action”: Start or Stop

  • “index”: Channel reporting the event. Channel 1 = (“index” = “0”), Channel 2 = (“index” = “1”) …

 

Example of a binary-sensor:

binary_sensor:
      - platform: mqtt
        name: Channel 1 Tripwire  **this can be changed to what ever you want**
        state_topic: cameras
        value_template: "{{ 'ON' if value_json.code == 'CrossLineDetection' and value_json.action == 'Start' and value_json.index == '0' else 'OFF'}}"
        device_class: motion

 

If you follow further down the CrossLineDetection string you will find:

"Name": "Rule4"

When you set an IVS rule on the recorder you can give it a unique name. This is where that name will appear. This will allow you set up multiple different triggers in the cameras view. This could be helpful to trigger lights based on where you are in a room.

 

You should be able add another

and

value with

value_json.data.0.name == 'your.rule.name'

Or, possibly, if all intelligent rules have unique names then just this string, VideoMotion will still need

"index"

to identify the channel.

 

I’ve yet to try the last value above. I believe there is a bracket that needs to be dealt with to get to the name field. I have little coding experience and most of this is Frankensteined together. So please correct any errors.

I did a full write up of the steps I followed on reddit:

What you have in your configuration.yaml should be in your apps.yaml that is stored in your config\appdaemon/apps folder

hello_world:
  module: hello
  class: HelloWorld
  
dahua_mqtt:
module: dahua_mqtt
class: DahuaMQTT

DahuaMQTT:
  class: DahuaMQTT
  module: dahua_mqtt
  cameras:
    - host: 192.168.0.221
      port: 80
      user: camerausername
      pass: camerapassword
      topic: cameras/driveway
      retain: true
      events: CrossRegionDetection,CrossLineDetection,VideoMotion,AlarmLocal,MoveDetection,MoveDetection,AlarmOutput,ParkingDetection

Hi, I’m planning to buy some cameras and having a look at the Dahua SMD Plus (Smart Motion Detection Plus) models as they use an AI chip to generate the events. Which would be more accurate than the standard motion detection alarms.
Would be great to be able to detect Persons and Cars and make automations in HA.
Unfortunately it seems the Dahua cameras are not directly integrated within HA. You can use the Amcrest integration, however it seems motion events are pretty limited.

This script sounds to be a great solution. So many thanks for all the work!
I suppose you can listen for any SMD Plus event?
Is this script working on a polling principle or is the event pushed when it occurs?
I suppose this script is not labor intensive for your HA instance, as it’s just sending some metadata?

Many thanks.

Hi, I cannot guarantee, that it would work with SMD Plus. If these cameras generating events like the standard ones, then I guess it’s possible to listen to any type of events. Listening for events is woking by long-polling pricinple, so there would not be labor / traffic intensive work going on.

I run this with dahuas with smd not smd+. The json output of the motion detection event has a line smart motion true/false, that you can filter by. I’m pretty sure when smd is enabled it will be the only motion event that will be triggered for that channel.

{
    "code": "VideoMotion",
    "action": "Start",
    "index": "2",
    "data": {
        "SmartMotionEnable": false
    }
}

To add I run analog cameras and log into the XVR. I have 12 cameras with multiple triggers per camera and it all runs great. With IVS rules you can name them and each can be a separate sensor.

@algirdasc this great I really appreciate the work you’ve done.

1 Like

Hi, recently started with HA and looking for a way to use the Dahua camera as motion sensor input. Now this looks what I need or al least give it a try. Only thing is that it is not clear to me how to implement this. I have the latest HA also App-deamon running and mqtt. So what steps to take to get this working?
Where goes what? Or where to find a instruction for dummies :slight_smile: Thanks.

The steps to get it running are the same. The index is 0 or 1 for a single camera

Great. That made it a bit more clear. But unfortunately I can not locate the /config/appdaemon directory. It is not there? I made the recommended yaml changes in the add on config. And restart it.


That is running.

Tried to get it working by creating a /config/appdaemon dir with all the required files in it. But it is not working. That was to expect since HA will not look into that dir I guess. So how to do this?