SunCron

downloads: 1305 | type: gz | size: 19.88 kB
  • Released: Jan 23, 2010
  • Requires: Perl + DateTime perl module

What is this?

SunCron is a perl program that will generate cron rules based on the time when sun sets and/or rises. I made it to control the lighting in my apartment together with the nexa remote control I made. You can however run any command that doesn’t require user input.

Installation

Installation is very simple. Just download the archive and unpack it in a suitable folder, eg your home folder, then run make install. Now you’re ready to configure the package.

On a BUBBA, issue the command aptget install libdatetime-perl (a B1 requires you add the Debian repository).

Configuration

Configuration is done in the file /etc/default/suncron. It has two sections, Location and Rules.

Location contain information about your timezone and coordinates. These parameters are needed in order to get a correct time for sunset and sunrise. Available timezones are found in /usr/share/zoneinfo. Coordinates are entered in decimal degrees (WGS84).

Rules contain the rules that should be evaluated on each run. A rule has four parts;

  • a condition This is a clear text formula, eg ‘sunrise > 06:30′
  • a true entry This is the time to use if condition is true, eg ‘sunrise + 00:45′.
  • a false entry This is the time to use id condition is false.
  • a partial cron entry This is a cron entry without the first two fields (minutes and hours). The minute and hour field will come from the true or false entry.

Each part is separated with a semicolon (;).

Condition and true/false parts may contain the words sunrise and/or sunset (they will be replaced with current days time when sun rises or sets respectively). Say for instance you want to test if sun rises before 06:00 (6 am), then you can write sunrise < 06:00. You can also add or subtract times, like sunrise + 02:00 to get the time two hours after sunrise.

The cron part is a cron line without the first two fields (minutes and hours, these will be taken from the true/false part). I will not go into the format of cron rules. Use man cron to find out more.

Note that there exist two kinds of cron lines, one with a username and one without. SunCron uses the one with a username.

To sum it up, this is what a rule could look like:

sunrise > 07:00 ; 06:15 ; ; * * 1-4 root control_nexa send B 3 on

This is a rule I use to test if sun rises after 07:00. If it does, then I want to turn on my bed side light at 06:15. If it doesn't, then do nothing.

Here's another rule I use to turn on the lights in the living room window:

sunset < 17:00 ; 17:00 ; sunset ; * * 1-3 root control_nexa send B 5 on

This will test if sun sets before 17:00 (5 pm). If it does, then turn on the light at 17:00. If it doesn't, then turn on light when sun sets.

SunCron will not try to evaluate the cron part. This means if the cron rule has a day of week limit, the cron line will still be written to the cron file. This does not mean the command is executed though, since cron will know what to do. It's just that SunCron doesn't care about the cron limits, only limits in the condition.

Usage

The idea with this program is to let cron run it a couple of minutes after midnight. SunCron will parse all rules, generate new cron entries and write them (overwriting) to the cron file /etc/cron.d/suncron (unless overridden with commandline argument).

When you installed suncron, a script was made in /etc/cron.daily so suncron will execute daily by default. You should check when the daily jobs are run, some systems run them very late which means suncron is run after sunrise, in which case your rules based on sunrise may be a bit off (they will use previous days values).

Run suncron --man to get a detailed description of the supported command line arguments.

Comments are closed.