Doing something at Sunrise/Sunset

A little skeleton script to do something based on sunrise/sunset.

You'd want to modify this to either hard code your home GPS coords, or pass them in from your GPS (gpsd, etc). You'll also need to modify it to perform the actual actions you want.

#!/usr/bin/perl

use strict;
use warnings;

use Astro::Sunrise;

my ($long,$lat) = qw (-842259  334359);  # Specify yours or get from GPS

my $now      = join(":", (localtime) [2,1]);
my $sun_rise = sun_rise($long, $lat);
my $sun_set  = sun_set($long, $lat );

print "Now $now, sunrise \@ $sun_rise, sunset at \@ $sun_set\n";

if ($now gt $sun_rise and $now lt $sun_set){
  print "It's daytime, do something!\n";
}else {
  print "It's nighttime!, do something\n";
}
Scroll to top