Skip to content

Repository files navigation

Home Assistant Dahua Integration

The Dahua Home Assistant integration allows you to integrate your Dahua cameras, doorbells, NVRs, DVRs in Home Assistant. It's also confirmed to work with some Lorex cameras and Amcrest devices.

Supports motion events, alarm events (and others), enabling/disabling motion detection, switches for infrared, illuminator (white light), security lights (red/blue flashers), sirens, doorbell button press events, and more.

Also exposes several services to enable/disable motion detection or set the text overlay on the video.

NOTE: Using the switch to turn on/off the infrared light will disable the "auto" mode. Use the service to enable auto mode again (or the camera UI).

Why not use the Amcrest integration already provided by Home Assistant? The Amcrest integration is missing features that this integration provides and I want an integration that is branded as Dahua. Amcrest are rebranded Dahua cams. With this integration living outside of HA, it can be developed faster and released more often. HA has release schedules and rigerous review processes which I'm not ready for while developing this integration. Once this integration is mature I'd like to move it into HA directly.

Installation

If you want live-streaming, make sure to add the following to your config.yaml:

ffmpeg:

See ffmpeg and stream.

HACS install

To install with HACS:

  1. Click on HACS in the Home Assistant menu
  2. Click on Integrations
  3. Click the EXPLORE & ADD REPOSITORIES button
  4. Search for Dahua
  5. Click the INSTALL THIS REPOSITORY IN HACS button
  6. Restart Home Assistant
  7. Configure the camera by going to Configurations -> Integrations -> ADD INTERATIONS button, search for Dahua and configure the camera.

Manual install

To manually install:

# Download a copy of this repository
$ wget https://github.com/rroller/dahua/archive/dahua-main.zip

# Unzip the archive
$ unzip dahua-main.zip

# Move the dahua directory into your custom_components directory in your Home Assistant install
$ mv dahua-main/custom_components/dahua <home-assistant-install-directory>/config/custom_components/

⚠️ After executing one of the above installation methods, restart Home Assistant. Also clear your browser cache before proceeding to the next step, as the integration may not be visible otherwise.

Setup

  1. Now the integration is added to HACS and available in the normal HA integration installation, so...
  2. In the HA left menu, click Configuration
  3. Click Integrations
  4. Click ADD INTEGRATION
  5. Type Dahua and select it
  6. Enter the details:
    1. Username: Your camera's username
    2. Password: Your camera's password
    3. Address: Your camera's address, typically just the IP address
    4. Port: Your camera's HTTP port. Default is 80
    5. RTSP Port: Your camera's RTSP port, default is 554. Used to live stream your camera in HA
    6. Events: The integration will keep a connection open to the camera to capture motion events, alarm events, etc. You can select which events you want to monitor and report in HA. If no events are selected then the connection will no be created. If you want a specific event that's not listed here open an issue and I'll add it.

NOTE: All streams will be added, even if not enabled in the camera. Just remove the ones you don't want.

Dahua Setup

Known supported cameras

This integration should word with most Dahua cameras and doorbells. It has been tested with very old and very new Dahua cameras.

Doorbells will have a binary sensor that captures the doorbell pressed event.

  • Please let me know if you've tested with additional cameras

These devices are confirmed as working:

Dahua cameras

Dahua cameras

Series 2 Megapixels 4 Megapixels 5 Megapixels 6 Megapixels 8 Megapixels
Consumer Series
A26
1-Series
HFW1230S HFW1431S1-S4
HDBW1230E-S2 HFW1435S-W
HFW1435S-W-S2
HDBW1431EP-S-0360B
2-/3-Series
HDW2831T-ZS-S2 HDW2431TP-AS HDW3549HP-AS-PV HDW3849HP-AS-PV
HDBW2231FP-AS-0280B-S2 HDBW2431R-ZS HFW3549T1-AS-PV-0280B-S3
HFW2449S-S-IL
HFW3441E-AS-S2
4-/5-Series
HDW4231EM-ASE HFW4433F-ZSA HDW4631EM-ASE HDW5831R-ZE
HDBW4231F-AS HDBW5421E-Z
HDW4233C-A T5442T-ZE
HDBW4239R-ASE T5442TM-AS
B5442E-Z4E
B54IR-ASE
HDBW4239RP-ASE
PTZ Series
SD1A203T-GN
SD42212S-HN
Thermal Series
TPC-BF2221P-B3F4
6-/7-Series
HDPW7564N-SP
Panoramic Series
EW5531-AS

Other brand cameras

Brand 2 Megapixels 4 Megapixels 5 Megapixels 8 Megapixels
Amcrest
Amcrest IP5M-T1179E Amcrest IPC-Color4K-T
IMOU
IMOU IPC-A26Z / Ranger Pro Z IMOU DB61i
IMOU IPC-C26E-V2 *
IMOU IPC-K22A / Cube PoE-322A
Lorex
Lorex E891AB
Lorex LNB8005-C
Lorex LNE8964AB

* partial support

Doorbell cameras

Brand 2 Megapixels 4 Megapixels 5 Megapixels 8 Megapixels
Amcrest
Amcrest AD110 Amcrest AD410
Dahua
DHI-VTO2202F-P
DHI-VTO2211G-P
DHI-VTO3311Q-WP
IMOU
IMOU C26EP-V2 IMOU IPC-K46 IMOU DB61i

Known Issues

  • IPC-D2B20-ZS doesn't work. Needs a wrapper, 7, 8

  • Versions between 0.9.84 and 0.9.92 could leave the illuminator switched on in a profile you are not using. In that window the illuminator wrote to the wrong day/night profile on some cameras (#605, #582); which versions affected you depends on the camera, and 0.9.93 fixed the write for both kinds. It does not undo what the earlier versions wrote. If you turned the illuminator on during that window, Lighting_V2[<channel>][0][0].Mode may still be Manual on the Day profile.

    While the camera is in General mode this does nothing at all. But if the camera is ever switched to Day/Night profile management, the light comes on when that profile becomes active, looking as though it did so by itself. Home Assistant will report it as on and can turn it off — but only once you notice it.

    The integration deliberately does not correct this for you, because doing so would mean writing to a profile you are not currently using, on every camera, unprompted. To check for it and clear it by hand, see Curl/HTTP commands.

Events

Events are streamed from the device and fired on the Home Assistant event bus.

Here's example event data:

{
    "event_type": "dahua_event_received",
    "data": {
        "name": "Cam13",
        "Code": "VideoMotion",
        "action": "Start",
        "index": "0",
        "data": {
            "Id": [
                0
            ],
            "RegionName": [
                "Region1"
            ],
            "SmartMotionEnable": false
        },
        "DeviceName": "Cam13"
    },
    "origin": "LOCAL",
    "time_fired": "2021-06-30T04:00:28.605290+00:00",
    "context": {
        "id": "199542fe3f404f2a0a81031ee495bdd1",
        "parent_id": null,
        "user_id": null
    }
}

And here's how you configure and event trigger in an automation:

platform: event
event_type: dahua_event_received
event_data:
  name: Cam13
  Code: VideoMotion
  action: Start

And that's it! You can enable debug logging (See at the end of this readme) to print out events to the Home Assisant log as they fire. That can help you understand the events. Or you can HA and open Developer Tools -> Events -> and under "Listen to events" enter dahua_event_received and then click "Start Listening" and wait for events to fire (you might need to walk in front of your cam to make motion events fire, or press a button, etc)

Example Code Events

Code Description
BackKeyLight Unit Events, See Below States
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
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

BackKeyLight States

State Description
0 OK, No Call/Ring
1, 2 Call/Ring
4 Voice message
5 Call answered from VTH
6 Call not answered
7 VTH calling VTO
8 Unlock
9 Unlock failed
11 Device rebooted

Services and Entities

Note for ease of use, the integration tries to determine if your device supports certain services and entities and will conditionally add them. That is sometimes hard to determine, so where it is unclear the entity is added anyway — an extra entity you can disable is friendlier than a flow complicated enough to be wrong. The "door open state" on doorbells is an example: not every doorbell has one.

Where the device reports the answer plainly, the entity is only created if it is real. Smart Motion Detection is the clearest case: the device lists the channels that support it, so channels that are absent from that list get no switch rather than one that reads off forever and silently discards anything written to it.

Services

Service Parameters Description
camera.enable_motion_detection Enables motion detection
camera.disable_motion_detection Disabled motion detection
dahua.set_infrared_mode target: camera.cam13_main
mode: Auto, On, Off
brightness: 0 - 100 inclusive
Sets the infrared mode. Useful to set the mode back to Auto
dahua.goto_preset_position target: camera.cam13_main
position: 1 - 10 inclusive
Go to a preset position
dahua.set_video_profile_mode target: camera.cam13_main
mode: Day, Night
Sets the video profile mode to day or night
dahua.set_focus_zoom target: camera.cam13_main
focus: The focus level, e.g.: 0.81 0 - 1 inclusive
zoom: The zoom level, e.g.: 0.72 0 - 1 inclusive
Sets the focus and zoom level
dahua.set_channel_title target: camera.cam13_main
channel: The camera channel, e.g.: 0
text1: The text 1
text2: The text 2
Sets the channel title
dahua.set_text_overlay target: camera.cam13_main
channel: The camera channel, e.g.: 0
group: The group, used to apply multiple of text as an overly, e.g.: 1
text1: The text 1
text3: The text 3
text4: The text 4
text2: The text 2
Sets the text overlay on the video
dahua.set_custom_overlay target: camera.cam13_main
channel: The camera channel, e.g.: 0
group: The group, used to apply multiple of text as an overly, e.g.: 0
text1: The text 1
text2: The text 2
Sets the custom overlay on the video
dahua.enable_channel_title target: camera.cam13_main
channel: The camera channel, e.g.: 0
enabled: True to enable, False to disable
Enables or disables the channel title overlay on the video
dahua.enable_time_overlay target: camera.cam13_main
channel: The camera channel, e.g.: 0
enabled: True to enable, False to disable
Enables or disables the time overlay on the video
dahua.enable_text_overlay target: camera.cam13_main
channel: The camera channel, e.g.: 0
group: The group, used to apply multiple of text as an overly, e.g.: 0
enabled: True to enable, False to disable
Enables or disables the text overlay on the video
dahua.enable_custom_overlay target: camera.cam13_main
channel: The camera channel, e.g.: 0
group: The group, used to apply multiple of text as an overly, e.g.: 0
enabled: True to enable, False to disable
Enables or disables the custom overlay on the video
dahua.set_privacy_masking target: camera.cam13_main
index: The mask index, e.g.: 0
enabled: True to enable, False to disable
Enables or disabled a privacy mask on the camera
dahua.set_record_mode target: camera.cam13_main
mode: Auto, On, Off
Sets the record mode. On is always on recording. Off is always off. Auto based on motion settings, etc.
dahua.enable_all_ivs_rules target: camera.cam13_main
channel: The camera channel, e.g.: 0
enabled: True to enable all IVS rules, False to disable all IVS rules
Enables or disables all IVS rules
dahua.enable_ivs_rule target: camera.cam13_main
channel: The camera channel, e.g.: 0
index: The rule index
enabled`: True to enable the IVS rule, False to disable the IVS rule
Enable or disable an IVS rule
dahua.vto_open_door target: camera.cam13_main
door_id: The door ID to open, e.g.: 1
Opens a door via a VTO
dahua.vto_cancel_call target: camera.cam13_main
Cancels a call on a VTO device (Doorbell)
dahua.set_video_in_day_night_mode target: camera.cam13_main
config_type: The config type: general, day, night
mode: The mode: Auto, Color, BlackWhite. Note Auto is also known as Brightness by Dahua
Set the camera's Day/Night Mode. For example, Color, BlackWhite, or Auto
dahua.reboot target: camera.cam13_main
Reboots the device

Camera

This will provide a normal HA camera entity (can take snapshots, etc)

Switches

Switch Description
Motion Enables or disables motion detection on the camera
Siren If the camera has a siren, will turn on the siren. Note, it seems sirens only stay on for 10 to 15 seconds before switching off
Event Notifications Enables or disables the device's event notifications
Smart Motion Detection If the device supports it, enables or disables smart motion detection (human and vehicle filtering rather than plain pixel motion). Only created on channels the device reports as supporting it
Disarming Linkage Newer firmwares introduce a "disarming" feature, accessible from the camera web UI under Event → One-click disarm / Disarming. When enabled, the disarm toggle suppresses the linkage actions configured in the Disarming section specifically, while leaving all other alarm linkage actions untouched. Detection remains fully active throughout. This allows one to turn it on/off.

Lights

Light Description
Infrared Turns on/off the infrared light. Using this switch will disable the "auto" mode. If you want to enable auto mode again then use the service to enable auto. When in auto, this switch will not report the on/off state.
Illuminator If the camera has one, turns on/off the illuminator light (white light). Using this switch will disable the "auto" mode. If you want to enable auto mode again then use the service to enable auto. When in auto, this switch will not report the on/off state.
Security If the camera has one, turns on/off the security light (red/blue flashing light). This light stays on for 10 to 15 seconds before the camera auto turns it off.

Binary Sensors

Sensor Description
Motion A sensor that turns on when the camera detects motion
Button Pressed A sensor that turns on when a doorbell button is pressed
Others A binary senor is created for evey event type selected when setting up the camera (Such as cross line, and face detection)

Sensors

Diagnostic sensors. Both report values already read during setup, so they cost no extra requests.

Sensor Description
Firmware Version The firmware the device reports. Also shown on the device page, but as a sensor it can be templated and compared — which is what makes "tell me when a camera is behind" possible
Serial Number The serial the device reports. On an NVR every channel reports the recorder's serial, because every channel is the same physical box
License Plate The last recognized license plate reported by the camera's ANPR/Traffic AI, including attributes for confidence, vehicle type, vehicle color, brand/logo, model/series, and direction

Buttons

Button Description
Reboot Reboots the device
Open Door On a VTO (doorbell), opens the door

Options

Open the integration, find the device and choose Configure. Options apply to that entry only, so on an NVR each channel is configured separately.

Option Default Description
Poll interval 30 seconds How often the device is asked for the state of its settings. Events do not use this — they arrive on a separate stream and are unaffected by a longer interval
Camera, Switch, Light, Select, Binary sensor, Button, Sensor on Which platforms this entry creates. These also stop the requests that exist only to feed a platform, so turning one off reduces how much the device is asked, not just how many entities you see
Auto-detect channel on Some firmwares number channels from 0 and others from 1. Turn this off if the detection gets it wrong on your camera
Enable NVR active deterrence controls off Adds Warning Light and Alarm entities for a camera behind an NVR. Off by default because whether the NVR relays these commands varies by model — on some it accepts them and does nothing

Reducing entries in your device's log

Dahua devices write a line to their own log for every login, and each request the integration makes is a separate HTTP call with its own authentication. Fewer calls therefore means fewer log entries. Three things help, in order of effect:

  1. Turn off platforms you do not use. If you only want the camera and motion events, switching off switch, light and select removes most of what a poll asks for. The two reads that happen on every poll — PTZ position and coaxial (siren/white light) status — belong to select and to light/switch respectively.
  2. Raise the poll interval to 120–180 seconds. Settings you change from the Dahua app will take that long to appear in Home Assistant; motion and other events are unaffected.
  3. Keep the integration up to date. Config reads are cached, channels of one NVR share a single read, and a device that stops answering is backed off rather than retried at the same rate.

This reduces the entries; it does not eliminate them. A device that is polled will log logins.

Local development

If you wish to work on this component, the easiest way is to follow HACS Dev Container README. In short:

  • Install Docker
  • Install Visual Studio Code
  • Install the devcontainer Visual Code plugin
  • Clone this repo and open it in Visual Studio Code
  • View -> Command Palette. Type Tasks: Run Task and select it, then click Run Home Assistant on port 9123
  • Open Home Assistant at http://localhost:9123

Debugging

Add to your configuration.yaml:

logger:
  default: info
  logs:
    custom_components.dahua: debug

Curl/HTTP commands

# Stream events
curl -s --digest -u admin:$DAHUA_PASSWORD  "http://192.168.1.203/cgi-bin/eventManager.cgi?action=attach&codes=[All]&heartbeat=5"

# List IVS rules
http://192.168.1.203/cgi-bin/configManager.cgi?action=getConfig&name=VideoAnalyseRule

# Enable/Disable IVS rules for [0][3] ... 0 is the channel, 3 is the rule index. Use the right index as required
http://192.168.1.203/cgi-bin/configManager.cgi?action=setConfig&VideoAnalyseRule[0][3].Enable=false

# Enable/disable Audio Linkage for an IVS rule
http://192.168.1.203/cgi-bin/configManager.cgi?action=setConfig&VideoAnalyseRule[0][3].EventHandler.VoiceEnable=false

# Read the lighting profiles, to check for an illuminator left on by 0.9.84-0.9.92 (see Known Issues).
# In Lighting_V2[a][b][c], a is the channel, b the profile (0 Day, 1 Night, 2 the one General mode uses)
# and c the light. The light order is NOT the same on every model, so read the
# LightType lines in the output before writing anything: on many cameras light 0
# is the white illuminator, but some report 0 as InfraredLight and the white
# light at 1. Use whichever index says LightType=WhiteLight.
http://192.168.1.203/cgi-bin/configManager.cgi?action=getConfig&name=Lighting_V2

# Clear it. Only run this if the Mode above is Manual on a profile the camera is
# not using, and change the last index to whichever one is the WhiteLight above.
http://192.168.1.203/cgi-bin/configManager.cgi?action=setConfig&Lighting_V2[0][0][0].Mode=Off

References and thanks

About

Dahua Camera and Doorbell Home Assistant Integration

Topics

Resources

Stars

561 stars

Watchers

21 watching

Forks

Releases

Used by

Contributors

Languages