Dahua IPC to MQTT App

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?

Researching this a bit further made me aware that I need to use Visual Studio to get this working? Right? I need to run that remotely since my HA system is on a Raspberrypi which can not run VS (tried to use the add-on but it says no). So Have VS running on my Macbook. Tried to get it connected to the HA machine but do not get any connection. (created the requested key). Tried to add a repo VSCode Remote which might help but that is only giving errors when trying to install in HA add-on.
How to get this all working? Why is HA so complicated? Any help would be appreciated thanks!

Vscode is not req, as an aside I have it running on a pi as an addon. You need to modify apps.yaml located /config/appdaemon/apps/apps.yaml

under hello world you would add

DahuaMQTT:
  class: DahuaMQTT
  module: dahua_mqtt
  cameras:
    - host: 192.168.4.108
      port: 80
      user: username
      pass: password
      retain: true
      topic: cameras
      events: CrossRegionDetection,CrossLineDetection,VideoMotion,LeftDetection

Programming is linear, there isn’t room for extra/miss placed characters. As code executes, once it hits something it doesn’t understand everything kinda halts.

Thanks for the feed back. I cant get the code editor to work. So, If I am on the server with ssh where can I find the mentioned files? I do not have any folder named /config as far as I can see? See my file-tree on the picture. the config folder in the tree I created manually… Will copy it over to the HomeAssitant folder.

What type of installation ha os? docker? vm? When you install the appdaemon addon it should create that folder. Is appdaemon running? what do the appdaemon logs say?

System.

The deamon is running I can open it.


2022-07-14 16:51:01.331254 DEBUG AppDaemon: get_state: thread.thread-0.None None True
2022-07-14 16:51:01.332558 DEBUG HASS: Utility
2022-07-14 16:51:01.333905 DEBUG AppDaemon: set_state(): sensor.appdaemon_uptime, {'state': '2 days, 3:07:35'}
2022-07-14 16:51:01.335583 DEBUG AppDaemon: parse_state: sensor.appdaemon_uptime, {'state': '2 days, 3:07:35'}
2022-07-14 16:51:01.337067 DEBUG AppDaemon: Old state: {'entity_id': 'sensor.appdaemon_uptime', 'state': '2 days, 3:07:34', 'last_changed': '2022-07-14T16:51:00+02:00', 'attributes': {}}
2022-07-14 16:51:01.338501 DEBUG AppDaemon: New state: {'entity_id': 'sensor.appdaemon_uptime', 'state': '2 days, 3:07:35', 'last_changed': '2022-07-14T16:51:01+02:00', 'attributes': {}}
2022-07-14 16:51:01.339758 DEBUG AppDaemon: sending event locally
2022-07-14 16:51:01.341526 DEBUG AppDaemon: Util loop compute time: 19.0ms, check_config()=8.0ms, other=11.0ms
2022-07-14 16:51:01.343343 DEBUG AppDaemon: Event type:state_changed:
2022-07-14 16:51:01.344810 DEBUG AppDaemon: {'entity_id': 'sensor.appdaemon_uptime', 'new_state': {'entity_id': 'sensor.appdaemon_uptime', 'state': '2 days, 3:07:35', 'last_changed': '2022-07-14T16:51:01+02:00', 'attributes': {}}, 'old_state': {'entity_id': 'sensor.appdaemon_uptime', 'state': '2 days, 3:07:34', 'last_changed': '2022-07-14T16:51:00+02:00', 'attributes': {}}}
2022-07-14 16:51:01.345947 DEBUG AppDaemon: process_event_callbacks() admin {'event_type': 'state_changed', 'data': {'entity_id': 'sensor.appdaemon_uptime', 'new_state': {'entity_id': 'sensor.appdaemon_uptime', 'state': '2 days, 3:07:35', 'last_changed': '2022-07-14T16:51:01+02:00', 'attributes': {}}, 'old_state': {'entity_id': 'sensor.appdaemon_uptime', 'state': '2 days, 3:07:34', 'last_changed': '2022-07-14T16:51:00+02:00', 'attributes': {}}}}
2022-07-14 16:51:01.348196 DEBUG AppDaemon: thread_async loop, args={'function': <bound method ADStream.process_event of <appdaemon.stream.adstream.ADStream object at 0xb51af340>>, 'args': ({'event_type': 'state_changed', 'data': {'entity_id': 'sensor.appdaemon_uptime', 'new_state': {'entity_id': 'sensor.appdaemon_uptime', 'state': '2 days, 3:07:3

The log is showing a lot of info :slight_smile:

Then search the ha folder for appdaemon or apps.yaml, it has to be in there somewhere already.

Well I found it. Not expected it there. Lets see how to progress from here. Thanks.