Program for flipping cameras to day/night on Linux

biggen

Known around here
May 6, 2018
2,688
3,060
I got tired of fooling with my Dahua cameras scheduling profiles for Day/Night. I know that @bp2008 has a handy Windows program that accomplishes a similar purpose but I needed something for Linux. Thanks for writing that for the community by the way, @bp2008! I dug into your source to find out what HTTP API calls I needed for my cameras to switch between day/night. It seems there are different HTTP API calls for different cameras so its a bit trial and error before you find the right call you need.

There is a program I found called sunwait which is really really slick. It calculates sunrise/sunset from any GPS coordinates you give it. It can give you standard, civil, twilight, and astronomical sunrise/sunset. You can even customize further with sunwait by giving it angles below/above horizon you want the sun to be before running a command or even how many minutes before/after sunrise/sunset you need the sun to be. Anyway, after any of those conditions occurs, you can give it a command to execute. Commands go after the semicolon ( ; ). So here is a simple cron job that will flip my Dahua PC-T5442TM-AS to Night:

Bash:
0 16 * * * /location/of/sunwait/binary/sunwait wait set 40.19N 80.79W ; wget -O - --user username --password password "camera_ip:port/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=1" >/dev/null 2>&1

Basically cron kicks this of at 4pm (16:00). Sunwait runs, checks the sunset time for the given GPS coordinates, and then waits for sunset. Once sunset occurs it runs the command wget which connects to the camera with the supplied ip : port and username: password and flips my camera to night mode. Sunwait then exits after wget. A separate cron job is created to flip it back to day mode:

Bash:
0 4 * * * /location/of/sunwait/binary/sunwait wait rise 40.19N 80.79W ; wget -O - --user username --password password "camera_ip:port/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" >/dev/null 2>&1

Here cron runs sunwait at 4am, sunwait checks the sunrise time and enters into a wait, and then kicks off the wget command at sunrise to flip the camera back to day mode. Sunwait exits after the wget command. These HTTP commands are for the newer PC-T5442TM-AS 4MP cameras. Your HTTP command may be different since Dahua didn't adopt a standard for all their cameras to handle the switch between day/night.

So basically its just two cronjobs with the appropriate HTTP calls that flips the cameras back and forth. Wanted to put this out there. You can do all kinds of home automation with sunwait. Nice to not have to mess with setting a profile anymore. Its set and forget now.
 
Last edited:
Pre=packaged for you - Dahua day/night switch utility - DahuaSunriseSunset

Search is your friend.

Reading comprehension is your friend. Here is the 1st paragraph from my post above. I even bolded the important part for you:

I know that @bp2008 has a handy Windows program that accomplishes a similar purpose but I needed something for Linux. Thanks for writing that for the community by the way, @bp2008! I dug into your source to find out what HTTP API calls I needed for my cameras to switch between day/night. It seems there are different HTTP API calls for different cameras so its a bit trial and error before you find the right call you need.
 
  • Haha
Reactions: runraid
Yup, first sentence. That's what happens when you scan read, are tired and need a cup of coffee.
 
I got tired of fooling with my Dahua cameras scheduling profiles for Day/Night. I know that @bp2008 has a handy Windows program that accomplishes a similar purpose but I needed something for Linux. Thanks for writing that for the community by the way, @bp2008! I dug into your source to find out what HTTP API calls I needed for my cameras to switch between day/night. It seems there are different HTTP API calls for different cameras so its a bit trial and error before you find the right call you need.

There is a program I found called sunwait which is really really slick. It calculates sunrise/sunset from any GPS coordinates you give it. It can give you standard, civil, twilight, and astronomical sunrise/sunset. You can even customize further with sunwait by giving it angles below/above horizon you want the sun to be before running a command or even how many minutes before/after sunrise/sunset you need the sun to be. Anyway, after any of those conditions occurs, you can give it a command to execute. Commands go after the semicolon ( ; ). So here is a simple cron job that will flip my Dahua PC-T5442TM-AS to Night:

Bash:
0 16 * * * /location/of/sunwait/binary/sunwait wait set 40.19N 80.79W ; wget -O - --user username --password password "camera_ip:port/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=1" >/dev/null 2>&1

Basically cron kicks this of at 4pm (16:00). Sunwait runs, checks the sunset time for the given GPS coordinates, and then waits for sunset. Once sunset occurs it runs the command wget which connects to the camera with the supplied ip : port and username: password and flips my camera to night mode. Sunwait then exits after wget. A separate cron job is created to flip it back to day mode:

Bash:
0 4 * * * /location/of/sunwait/binary/sunwait wait rise 40.19N 80.79W ; wget -O - --user username --password password "camera_ip:port/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=0" >/dev/null 2>&1

Here cron runs sunwait at 4am, sunwait checks the sunrise time and enters into a wait, and then kicks off the wget command at sunrise to flip the camera back to day mode. Sunwait exits after the wget command. These HTTP commands are for the newer PC-T5442TM-AS 4MP cameras. Your HTTP command may be different since Dahua didn't adopt a standard for all their cameras to handle the switch between day/night.

So basically its just two cronjobs with the appropriate HTTP calls that flips the cameras back and forth. Wanted to put this out there. You can do all kinds of home automation with sunwait. Nice to not have to mess with setting a profile anymore. Its set and forget now.

Thanks, this is exactly what I need to run on my multi camera system. I'm new to rasp. pi and Linux though and not sure how I get the sunwait on to it? The link takes me to github, but from there do I save all the files then run makefile on my pi?

Also if I have multiple cameras, do I need to run a seperate cron job for each camera, or can I list each camera after wget, perhaps seperated by colons or something?

Thanks in advance for any help on this.
 
Thanks, this is exactly what I need to run on my multi camera system. I'm new to rasp. pi and Linux though and not sure how I get the sunwait on to it? The link takes me to github, but from there do I save all the files then run makefile on my pi?

Also if I have multiple cameras, do I need to run a seperate cron job for each camera, or can I list each camera after wget, perhaps seperated by colons or something?

Thanks in advance for any help on this.
You have to install git on the RPi first so do that with sudo apt install git then you need to clone the repo with git clone https://github.com/risacher/sunwait. That will pull the files down. Then change into the sunwait directory that was created. From here you have to compile the program with make. But you probably need to install development headers and such. So you can issue an sudo apt install make g++. After those install just run make and see if it compiles with no errors. Should only take about 5 seconds.

If everything goes well, that creates the binary sunwait in that directory. That is what you use in your crontab command line. Yes, if you have multiple cameras then you would have several entries in cron with one for each camera. Here is an example of what my crontab looks like:

Bash:
# LPR Cam Night Switch - 20 Minutes before Sunset
0 16 * * * /home/joe/sunwait/sunwait wait set offset +00:20 10.19N 25.79W ; wget -O - --user admin --password pass "10.200.200.12/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=1" >/dev/null 2>&1              

# Rear Cam Night Switch - Sunset
0 16 * * * /home/joe/sunwait/sunwait wait set 10.19N 25.79W ; wget -O - --user admin --password pass "10.200.200.6:65000/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=1" >/dev/null 2>&1                        

# Sidewalk Night Switch - 15 minutes after sunset
0 16 * * * /home/rodman/sunwait/sunwait wait set offset -00:15 10.19N 25.79W ; wget -O - --user admin --password pass "10.200.200.7:65001/cgi-bin/configManager.cgiaction=setConfig&VideoInMode[0].Config[0]=1" >/dev/null 2>&1
 
  • Like
Reactions: tech101 and MyDaHua
You have to install git on the RPi first so do that with sudo apt install git then you need to clone the repo with git clone https://github.com/risacher/sunwait. That will pull the files down. Then change into the sunwait directory that was created. From here you have to compile the program with make. But you probably need to install development headers and such. So you can issue an sudo apt install make g++. After those install just run make and see if it compiles with no errors. Should only take about 5 seconds.

If everything goes well, that creates the binary sunwait in that directory. That is what you use in your crontab command line. Yes, if you have multiple cameras then you would have several entries in cron with one for each camera. Here is an example of what my crontab looks like:

Bash:
# LPR Cam Night Switch - 20 Minutes before Sunset
0 16 * * * /home/joe/sunwait/sunwait wait set offset +00:20 10.19N 25.79W ; wget -O - --user admin --password pass "10.200.200.12/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=1" >/dev/null 2>&1             

# Rear Cam Night Switch - Sunset
0 16 * * * /home/joe/sunwait/sunwait wait set 10.19N 25.79W ; wget -O - --user admin --password pass "10.200.200.6:65000/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=1" >/dev/null 2>&1                       

# Sidewalk Night Switch - 15 minutes after sunset
0 16 * * * /home/rodman/sunwait/sunwait wait set offset -00:15 10.19N 25.79W ; wget -O - --user admin --password pass "10.200.200.7:65001/cgi-bin/configManager.cgiaction=setConfig&VideoInMode[0].Config[0]=1" >/dev/null 2>&1
Thanks again biggen for your help, much appreciated.

I didn't get any errors but when I attempt to run sunwait from terminal, it just says 'bash: sunwait: command not found'. After the make call which I had to do using sudo (make by itself gave a permission error when trying to create sunwait.o - "
Fatal error: can't create sunwait.o: Permission denied"), I seem to have an executable in that directory, along with sunwait.o, sunriset.o, and print.o which were created with the make command. So not sure why it doesn't run. I'm typing this just after using the make command, perhaps I'll reboot and see if that helps. I've copied all the terminal text/commands when trying to do this but I won't post yet unless I can't work out why it's not running. Already troubled you enough!
 
Update, I am wrong, the program does run, I just have to type the full path even when, in a terminal, I try to run sunwait from it's installed location. So I guess I have to update the path to include the directory. Thanks again biggen, you're a legend.
 
  • Like
Reactions: biggen
This great, many thanks. Been struggling to send an http request via home assistant but no luck as I have no programming skill. This is a super easy way to do it.
 
This great, many thanks. Been struggling to send an http request via home assistant but no luck as I have no programming skill. This is a super easy way to do it.
 
Frankly I wish that Dahua would build this functionality into their camera firmware. Switching between day and night profiles based on user entry of latitude and longitude is commonplace in all sorts of smart appliances. Perhaps @EMPIRETECANDY could pass this on to the engineers at Dahua as a feature request.
So can send me a summary for the requirement? Then i can check with dahua engineer if we can do it
 
So can send me a summary for the requirement? Then i can check with dahua engineer if we can do it

@EMPIRETECANDY , here is what is required:

(1) The camera has an optional setting that permits automatic switching between DAY and NIGHT profiles.

(2) To use this automatic switching feature, the user must enter the latitude and longitude of the camera location in decimal format, e.g. LAT = -40.446 , LONG = +79.982. With this information the camera can calculate whether it is day time or night time based on the clock.

(3) The camera will switch into DAY profile mode at sunrise, and switch into NIGHT profile mode at sunset.

(4) Finally, the user should have the option to enter an offset value for the DAY and NIGHT profiles. So as an example, an offset of 0 for the DAY profile would mean the camera would switch to DAY mode precisely at sunrise. An offset of -60 would mean that the camera would switch 60 minutes BEFORE sunrise. An offset of +60 would mean that the camera would switch 60 minutes AFTER sunrise.

(5) A maximum range of -120 to + 120 minutes (in increments of 1 minute) would be sufficient for the offset. Note that the user would need to enter two offset values, once for the DAY profile and one for the NIGHT profile.

In my opinion, this should be a standard feature for all Dahua cameras. Many smart home devices already incorporate this algorithm, so that the homeowner does not constantly have to adjust the settings as the times of sunrise and sunset change over the year.
 
Last edited:
@EMPIRETECANDY , here is what is required:

(1) The camera has an optional setting that permits automatic switching between DAY and NIGHT profiles.

(2) To use this automatic switching feature, the user must enter the latitude and longitude of the camera location in decimal format, e.g. LAT = -40.446 , LONG = +79.982. With this information the camera can calculate whether it is day time or night time based on the clock.

(3) The camera will switch into DAY profile mode at sunrise, and switch into NIGHT profile mode at sunset.

(4) Finally, the user should have the option to enter an offset value for the DAY and NIGHT profiles. So as an example, an offset of 0 for the DAY profile would mean the camera would switch to DAY mode precisely at sunrise. An offset of -60 would mean that the camera would switch 60 minutes BEFORE sunrise. An offset of +60 would mean that the camera would switch 60 minutes AFTER sunrise.

(5) A maximum range of -120 to + 120 minutes (in increments of 1 minute) would be sufficient for the offset. Note that the user would need to enter two offset values, once for the DAY profile and one for the NIGHT profile.

In my opinion, this should be a standard feature for all Dahua cameras. Many smart home devices already incorporate this algorithm, so that the homeowner does not constantly have to adjust the settings as the times of sunrise and sunset change over the year.

If the camera could be able to fetch the sunrise / sunset data based on longitude / latitude, wouldn't that mean that the camera needs in / outbound internet access? Because it needs to find the location on a map, and calculate the data for sunrise and sunset.
 
  • Like
Reactions: bigredfish
If the camera could be able to fetch the sunrise / sunset data based on longitude / latitude, wouldn't that mean that the camera needs in / outbound internet access? Because it needs to find the location on a map, and calculate the data for sunrise and sunset.
No, that's not necessary. The Earth's orbit is extremely predictable, and calculable. Given the time, date, latitude, and longitude, you can precisely calculate sunrise and sunset for any spot on the globe.

I have a couple of $30 "solar timers" that I use to turn on and off my outside lights that I bought more than ten years ago. You enter the latitude and longitude, enter the date and time, and the lights turn off and on at sunrise and sunset. No Internet connection is required. The only problem is that the clocks drift over time, and they have to be corrected every few months.

A Dahua camera will have the advantage of accurate date and time as long as it can contact a time server. But beyond that, no geolocation awareness is required, so long as you enter the latitude and longitude.
 
You have to install git on the RPi first so do that with sudo apt install git then you need to clone the repo with git clone https://github.com/risacher/sunwait. That will pull the files down. Then change into the sunwait directory that was created. From here you have to compile the program with make. But you probably need to install development headers and such. So you can issue an sudo apt install make g++. After those install just run make and see if it compiles with no errors. Should only take about 5 seconds.

If everything goes well, that creates the binary sunwait in that directory. That is what you use in your crontab command line. Yes, if you have multiple cameras then you would have several entries in cron with one for each camera. Here is an example of what my crontab looks like:

Bash:
# LPR Cam Night Switch - 20 Minutes before Sunset
0 16 * * * /home/joe/sunwait/sunwait wait set offset +00:20 10.19N 25.79W ; wget -O - --user admin --password pass "10.200.200.12/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=1" >/dev/null 2>&1            

# Rear Cam Night Switch - Sunset
0 16 * * * /home/joe/sunwait/sunwait wait set 10.19N 25.79W ; wget -O - --user admin --password pass "10.200.200.6:65000/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=1" >/dev/null 2>&1                      

# Sidewalk Night Switch - 15 minutes after sunset
0 16 * * * /home/rodman/sunwait/sunwait wait set offset -00:15 10.19N 25.79W ; wget -O - --user admin --password pass "10.200.200.7:65001/cgi-bin/configManager.cgiaction=setConfig&VideoInMode[0].Config[0]=1" >/dev/null 2>&1

any chance you have the code/crontab for setting focus?

is that 10.19N 25.79W generic location? google map put that in the middle of the atlantic, just want to make sure I'm using the same GPS number system between google map for my home and what sunwait wants
 
any chance you have the code/crontab for setting focus?

is that 10.19N 25.79W generic location? google map put that in the middle of the atlantic, just want to make sure I'm using the same GPS number system between google map for my home and what sunwait wants
Yeah, its generic. Just use your home address Lat/Long instead.

I don't have the settings for focus, but I've seen them posted in this forum before in the past. I just don't have a link for them.
 
  • Like
Reactions: tech101
Yeah, its generic. Just use your home address Lat/Long instead.

I don't have the settings for focus, but I've seen them posted in this forum before in the past. I just don't have a link for them.
Found the API... Tinkering to get it to work
 
  • Like
Reactions: tech101
Yeah, its generic. Just use your home address Lat/Long instead.

I don't have the settings for focus, but I've seen them posted in this forum before in the past. I just don't have a link for them.


ok i think I got crontab and wget all sorted... Many many thanks

In the camera web UI, do you set it to General? Full Time? Schedule? Day/Night
 
  • Like
Reactions: tech101