Day / Night switching linux / rpi / possibly synology

hd343

n3wb
Nov 8, 2022
1
3
USA
Hi everyone,

I hope this is helpful - I have an area that is too lit, even at night, for autoswitching but I still prefer Night Mode. I was forced to use a utility when I realized quickly that scheduling varies too much over the span of the year.
I don't have a windows machine that runs 24/7 but an RPi or existing linux machine would work great and save power comparatively. Synology likely fine with some additional libraries installed [not tested]

Download this program:
mfreeborn/heliocron: A command line application written in Rust capable of delaying execution of other programs for time periods relative to sunrise and sunset. (github.com)
The benefit of this program is that is requires no outside API call, all sunrise / sunset times are calculated within the program - therefore an internet connection is not necessary.
I would suggest downloading the pre-compiled binary for your machine and untarring the file - wget & tar.
Once downloaded & unzipped - test program with
./heliocron -l YOURLATITUDE -o YOURLONGITUDE report

I would probably move this program to your /usr/local/bin directory or either add whatever $PATH to your crontab to make life easier so it will run from any location. Test that after moving.

Create two bash scripts Night & Day, make them executable 'chmod +x sciptname.sh' - take whatever responsibility for file security you need (-RWX perms)

night.sh
Code:
#!/bin/bash
#NIGHTTIME
curl -s -g --digest 'http://USER:PASS@CAMIPADDRESS/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode=0'

day.sh
Code:
#!/bin/bash
#DAYTIME
curl -s -g --digest 'http://USER:PASS@CAMIPADDRESS/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode=3'

The [0] modifier is your stream ID & the Switch.Mode={3,0} is your NIGHT / DAY modes.
These scripts will trigger your cam to DAY or NIGHT mode (go into settings and set your cameras up via those settings how you prefer).

Now, enter your crontab and set up the program.

Code:
0 4 * * * heliocron --latitude 51.4769 --longitude -0.0005 wait --event civil_dawn && day.sh
0 16 * * * heliocron --latitude 51.4769 --longitude -0.0005 wait --event civil_dusk && night.sh

You can view the different options available on the github page for heliocron. It runs as a cronjob that then waits until --event, then the rest of your cronjob completes [bash scripts].
It is working quite well for me and I like not having to have an internet connection and the option for civil dusk & dawns. I have several 24/7 linux machines and this ended up being the simplest and most robust solution for me. I have home assistant but almost refuse to touch it... It has caused me too many headaches. I think most people will have easier access to a RPi and may be running DNS blockers or anything else handy and this would be an easy addition for camera controls.

Gotchas
1. make sure your scripts are executable!
2. set the cron times close to the events and after each other in series: ~0400 dawn >>> ~1600 dusk because I'm not sure if heliocron would fail trying to run two jobs on top of each other.
3. make sure your crontab can find your files $PATH! If you have any failures to run, please explicitly list the paths /home/user/night.sh in crontab for both heliocron & your bash scripts - this can save you lots of headaches.
4. test your bash scripts to make sure they work!
5. your stream id may be different [0] - I'm not sure

I hope someone else can use this simple-ish linux solution for hands-off camera switching!
 
Last edited:
This works great! Thank you for sharing another raspberry pi solution to switch between day and night modes.