Change Day Schedule on Dahua IP Camera via Dahua API

JwBayTech

n3wb
Joined
Jul 21, 2023
Messages
1
Reaction score
0
Location
United States
Hello there,

--- Below is code that works (obviously, I removed the IP address and authentication) ---

Command:
curl --digest -X GET "http://<ip>/cgi-bin/configManager.cgi?action=getConfig&name=VideoInMode"

Output:

table.VideoInMode[0].Config[0]=0
table.VideoInMode[0].Config[1]=1
table.VideoInMode[0].Mode=1
table.VideoInMode[0].TimeSection[0][0]=1 10:00:00-16:00:00
table.VideoInMode[0].TimeSection[0][1]=0 00:00:00-24:00:00
table.VideoInMode[0].TimeSection[0][2]=0 00:00:00-24:00:00
table.VideoInMode[0].TimeSection[0][3]=0 00:00:00-24:00:00
table.VideoInMode[0].TimeSection[0][4]=0 00:00:00-24:00:00

QUESTION

How can I modify the schedule shown in table.VideoInMode[0].TimeSection[0][0]=1 10:00:00-16:00:00 via api requests?


IMPORTANT: I have already confirmed that the following command works in changing mode, but nothing I've found gets me the ability to change the schedule itself.
curl --digest -X GET "http://<ip>/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Mode=2"

Thanks for any and all help!
 

prsmith777

Getting comfortable
Joined
Dec 23, 2019
Messages
269
Reaction score
389
Location
Colorado
It took me a lot of trial and error and testing but I finally found this to work on the newest Web5.0 with my newer PTZ cam. This method does not work for Web4.0

"http://<ip>/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].TimeSectionV2[0][0]=10:00:00-16:00:00 Day"

I found you have to adjust the Night settings as well to make it work correctly as such:

"http://<ip>/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].TimeSectionV2[0][1]=00:00:00-09:59:59 Night"
"http://<ip>/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].TimeSectionV2[0][2]=10:00:01-24:00:00 Night"

The first [ ] after "TimeSectionV2" is the month integer where January is 0 and so on.
The second [ ] denotes a new block on the schedule. I found they dont have to be in any order.

I am doing this under customized scene.

I adjust my schedule daily based on the sunrise and sunset using a customized script as you can see from the attached pic

schedule.jpg
 

Alaska Country

Getting comfortable
Joined
Jun 10, 2021
Messages
449
Reaction score
657
Location
Alaska
Appreciate your time and effort to come up with an API to switch Time Plan Settings. It would be nice if the HTTP API would address these issues, but the document does not.

Using the following PTZ for day/night operations - Web 5
Device Model: Dahua SD5A425XA-HNR (PTZ5A4M-25X)
Algorithm Version: 2.0.2
PTZ Version: V2.401.0000001.42.RHNCT_220811_42531
Web Version: V5.01.0.1314701.TA

The GET statement correctly reports the camera data for the VideoInMode. The Day API works correctly and changes the time plan settings as indicated for the PTZ.

Code:
http://admin:password@192.168.55.156/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].TimeSectionV2[0][0]=00:00:00-24:00:00 Day
Note: For my use in Hubitat (C7 Hub) , the ampersand has to be escaped. "^&" and the quote marks are not part of the escape. For a Window's BAT file, the ampersand does not have to be escaped, but the space next to the word Day needs to be escaped. ("%20" for the escape code - again no quotes)

The Night part of the API does not work. When changing the block from zero to one (TimeSectionV2[0][1]) the browser reports an error in FireFox. However, if the V2 is removed, then the block works, but changes a time section that is not part of the Time Plan Settings.

Workaround
In place of using three time blocks only one is used. It is set from 000:00:00 to 24:00:00. Thus the orange line will be solid orange for day and solid purple for night. Hubitat uses two different API to change from day to night and from night to day when LUX is at the correct level.

Day Switch
Code:
http://admin:password@192.168.55.156/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].TimeSectionV2[0][0]=00:00:00-24:00:00 Day
Night Switch
Code:
 http://admin:password@192.168.55.156/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].TimeSectionV2[0][0]=00:00:00-24:00:00 Night
The only difference is the word at the end. Day or Night in this example.

Other modes can be used such as Custom1 or Custom2. The end word in the API would be Custom or Custom1 in this case.

The application of changing one line of code could be applied to using Blue Iris PTZ settings to run the API as per instructions provided by pb2008.

Alternative using Blue Iris to set camera profiles
 

prsmith777

Getting comfortable
Joined
Dec 23, 2019
Messages
269
Reaction score
389
Location
Colorado
@Alaska Country I think I know why you are seeing the error with the Night settings. It's partly my fault because I forgot to add that you MUST first set the number of time blocks you want each month in the time settings manually. Once they are set, then the api commands work.

I assume you and most others would want to have three blocks as you can see in the above pic. What you need to do is create one manually then copy them to all the months. They will all look the same at that point. Now the api commands will adjust them correctly.

Also through much trial and error I discovered another bug that I forgot to mention. You MUST have the camera set to 24 hr mode, not 12 Hr. None of the time settings will work with 12 Hr setting. I passed this bug along to Andy and his response was to just set it to 24Hr.... sigh.
 

prsmith777

Getting comfortable
Joined
Dec 23, 2019
Messages
269
Reaction score
389
Location
Colorado
I am interested how you got the username: password authentication to work with a single http statement. I tried using Http directly through Blue Iris, then tried VB.net script using HomeSeer without any luck. Didn't think to escape the blank or the &. Nice! I ended up having to use a more complicated script run locally on the BI machine:

Code:
Imports System.Net
Sub Main( ByVal Parm as Object )

    Dim sunrise as Date
    Dim sunset as Date
    dim sunsetStr as string
    Dim sunriseStr as String
    Dim sunriseStr1 as String
    Dim sunsetStr1 as String
    Dim sunriseOffset as Double     'in minutes
    Dim sunsetOffset  as Double     'in minutes
    Dim monthInt as Integer
    Dim cgiparam As String
    Dim url As String
    Const host = "192.168.40.124"
    Const page As String = "cgi-bin/configManager.cgi"
    Const user As String = "admin"
    Const pass As String = "XXXXXXX"

    '=== adjust as required ===
    sunriseOffset = -10   '- before , + after
    sunsetOffset = 10
    '-===

    sunrise = hs.SunriseDt.AddMinutes(sunriseOffset)
    sunset = hs.SunsetDt.AddMinutes(sunsetOffset)
  
    sunriseStr = sunrise.ToString("HH:mm:ss")      
    sunsetStr = sunset.ToString("HH:mm:ss")

    sunriseStr1 = sunrise.AddSeconds(-1).ToString("HH:mm:ss")      
    sunsetStr1 = sunset.AddSeconds(1).ToString("HH:mm:ss")

    monthInt = DateTime.Now.Month - 1


    cgiparam  = "action=setConfig&VideoInMode[0].TimeSectionV2[" & monthInt & "][0]=" & sunriseStr & "-" & sunsetStr & " Day"
    url = String.Format("http://{0}/{1}?{2}", host, page, cgiparam)

    Dim client As New System.Net.WebClient
    client.Credentials = New NetworkCredential(user, pass)

    Dim resp As String
    Dim resp1 As String
    Dim resp2 As String

    Try
        resp = client.DownloadString(url)
    Catch ex As Exception
        resp = ex.Message
    End Try

    hs.waitsecs(5)

    cgiparam = "action=setConfig&VideoInMode[0].TimeSectionV2[" & monthInt & "][1]=00:00:00-" & sunriseStr1 & " Night"
    url = String.Format("http://{0}/{1}?{2}", host, page, cgiparam)


    Try
        resp1 = client.DownloadString(url)
    Catch ex As Exception
        resp1 = ex.Message
    End Try

    hs.waitsecs(5)

    cgiparam = "action=setConfig&VideoInMode[0].TimeSectionV2[" & monthInt & "][2]=" & sunsetStr1 & "-24:00:00 Night"
    url = String.Format("http://{0}/{1}?{2}", host, page, cgiparam)


    Try
        resp2 = client.DownloadString(url)
    Catch ex As Exception
        resp2 = ex.Message
    End Try

  If resp.contains("OK") and resp1.contains("OK") and resp2.contains("OK") then
    hs.writelog("Remote Script","balcony_ptz.vb OK")
  Else
    hs.writelog ("Error",  "Remote script balcony_ptz.vb failed")

End Sub
 

Alaska Country

Getting comfortable
Joined
Jun 10, 2021
Messages
449
Reaction score
657
Location
Alaska
Thanks for clarifying the issue on the Night settings. It appears that the API, for that call, is requiring some additional information in order to generate an additional time block as it bounces back an error. Looks like that is most likely undocumented in the API listings.

Yes, using three blocks, one for day and two for night is a good logical approach when using the Web-5 camera GUI. My take is to only use two and to extend that time for a full 24 hours. Thus it makes it simpler to switch from day to night and night to day mode. Under Working Mode have setup Day and Night modes under Customized Scene for camera settings. Self-Adaptive would be nice to also use, however there is no option for the selection of two different modes thus making it unusable for a day/night switch.

As a question, what happens if day is set from 06:00 to 13:00 with no other Time Plan Settings. Would assume that the camera would stay in Day mode and never change. i.e. non colored blanks showing in the camera GUI.

Glad you brought up the 24 hour time scenario . Always use time in a 24 hour format so it was not an issue. But it is interesting that Dahua is not concerned. As others have said, we are not the intended audience thus a fix is most unlikely.
 
Last edited:

Alaska Country

Getting comfortable
Joined
Jun 10, 2021
Messages
449
Reaction score
657
Location
Alaska
I am interested how you got the username: password authentication to work with a single http statement.
On this end, API calls are tested by placing the URL into FireFox on the same network and machine that is hosting BI. As listed in the posted code, the user and password are part of the URL. Have not had any issues with it not working. If the user and password are not part of the URL, then FF will pop up a screen asking for this data.

My usual scenario is to first test in a browser and if the API code works then port over to a HTTP GET statement as a Hubitat action with the hub on the same sub net as BI. Note: BI and the Hub are on a sub net that is isolated from the internet.

Your testing and API provided the answer that was needed to make the day/night mode switch operational. Again, thank you for providing that much needed API!!!
 

Alaska Country

Getting comfortable
Joined
Jun 10, 2021
Messages
449
Reaction score
657
Location
Alaska
This is in response to a message about the day/night setup....

The first step to determine if an API call will work, is to enter a test API URL for a specific camera using a browser on the same machine that is hosting BI. In my case, I use FireFox. Note that Web-4 and Web-5 (the blue one) often use different API calls for specific actions but not always.

If the API works and is a GET statement. then information will be displayed in the browser. If a SET statement (makes changes to a camera setting) then an "OK" will be displayed. However, an "OK" is not an indication that the action took place. Just that the code works and presented no errors.

If the API does not work, then "Error" or "Error Bad Request!" is displayed. Make the appropriate changes and try again.

A good place to start is with a simple API GET statement that retrieves camera system information.

Note: User Name, Password and camera IP address must be changed to match your camera. Often BI users will keep "admin" for the user name and only change the password.

Code:
http://admin:password@192.168.55.166/cgi-bin/magicBox.cgi?action=getSystemInfo
This appears to work on most Dahua cameras and will display 7 lines of data.

For setting the Time Plan Settings on a Dahua SD5A425XA-HNR (PTZ5A4M-25X) the following GET statement can be used to see if the camera data has changed. This has been tested on only this model and a specific FW version.

Code:
http://admin:password@192.168.55.166/cgi-bin/configManager.cgi?action=getConfig&name=VideoInMode
If successful, then there will be a page of data displayed as in post 1 of this thread.

For other Web-4 cameras the following code is being used to change from day/night and night/day modes with the camera being set for "Full-Time Day" under "Profile Management". Keep in mind, that the posted API's may not work for all FW versions and Dahua models.

Day Switch
Code:
http://admin:password@192.168.55.166/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions.NightOptions.SwitchMode=0
Night Switch
Code:
http://admin:password@192.168.55.166/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions.NightOptions.SwitchMode=3
The only difference between day and night modes is the number at the end of the URL. In this case a 0 for day and a 3 for night.
 

CCTVCam

Known around here
Joined
Sep 25, 2017
Messages
2,676
Reaction score
3,508
It took me a lot of trial and error and testing but I finally found this to work on the newest Web5.0 with my newer PTZ cam. This method does not work for Web4.0

"http://<ip>/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].TimeSectionV2[0][0]=10:00:00-16:00:00 Day"

I found you have to adjust the Night settings as well to make it work correctly as such:

"http://<ip>/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].TimeSectionV2[0][1]=00:00:00-09:59:59 Night"
"http://<ip>/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].TimeSectionV2[0][2]=10:00:01-24:00:00 Night"

The first [ ] after "TimeSectionV2" is the month integer where January is 0 and so on.
The second [ ] denotes a new block on the schedule. I found they dont have to be in any order.

I am doing this under customized scene.

I adjust my schedule daily based on the sunrise and sunset using a customized script as you can see from the attached pic

View attachment 168272
You don't want gaps in the schedule such as in July (evening), Aug-Dec.

With gaps it will go to a default and you'll probably find at dusk and dawn that the picture is too dark. Better to merge them all together so everything crosses over. If daylight is too dark at dusk and night to light, add a low illuminace section between the 2 with settings suitable for dawn / dusk.

Also on the nightime gaps Aug-Dec, you'll probably find you have no usable recording in those gaps, as the default setting probaby won't be suitable for really llow light.

I suggest filling the whole table with everything gap free. Note to Andy here, really difficult to do this as gaps open up really easily and so small they'r hard to see. A better way of configuring this is needed or some kind of auto ripple where gaps are automatically closed.
 

prsmith777

Getting comfortable
Joined
Dec 23, 2019
Messages
269
Reaction score
389
Location
Colorado
If you know vb.net take a look at my script. It creates three perfect blocks daily with no gaps and adjusts each according to sunrise and sunset.

that pic was posted in July
 
Top