Page 1 of 1

Trigger script on sunset and -rise

Posted: Sat Feb 16, 2019 2:02 pm
by bananaboat
Hi,
Saving energy and increase life of the IR leds do I want to turn the IR leds off during the day. I could just pull some list of the internet and run a cron script during certain hours of the day to see if it fit the correct time (so to speak).

But there is a script https://sourceforge.net/projects/sunwait4windows/ that does check these values for you.

So when doing a quick run down with: ./sunwait wait debug
I get this

Code: Select all

Debug: argv[1]: >wait<
Debug: argv[2]: >debug<
Debug: All output to use local timezone (nogmt).
Debug: Now   utcTm:  Sat Feb 16 13:55:12 2019 GMT
Debug: Now localTm:  Sat Feb 16 14:55:12 2019 CET
Debug: Now UTC bias (add to GMT to get CET) hours: 1.000000
Debug: UTC Bias (hours): 1.000000
Debug: Now: Days since 2000: 6986
Debug: Target  year set to: 119
Debug: Target   mon set to: 1
Debug: Target  mday set to: 16
Debug: Target   utcTm:  Sat Feb 16 00:00:00 2019 GMT
Debug: Target localTm:  Sat Feb 16 01:00:00 2019 CET
Debug: Target UTC bias (add to GMT to get CET) hours: 1.000000
Debug: Target: Days since 2000: 6986
Debug: Co-ordinates -  Latitude: 52.952308N
Debug: Co-ordinates - Longitude: 359.048052E
Debug: Twilight - Daylight
Debug: User specified offset (hours): 0.000000
Debug: Function selected: Wait
Debug: sunriset.cpp: Sun directly south: 12.299710 UTC, Dirunal Arc = 9.850984 hours
Debug: sunriset.cpp: Days since 2000: 6985
Debug: sunriset.cpp: Sun directly south: 12.299120 UTC, Dirunal Arc = 9.916388 hours
Debug: sunriset.cpp: Days since 2000: 6986
Debug: sunriset.cpp: Sun directly south: 12.298327 UTC, Dirunal Arc = 9.982130 hours
Debug: sunriset.cpp: Days since 2000: 6987
Debug: Wait reduced from 12014 to 10 seconds.
What would be the next step to call another script when the sun has actually set?

Using this command: ./sunwait list rise
# it will output this
08:20

But how do i get a script executed on 08:20?

Re: Trigger script on sunset and -rise

Posted: Sat Feb 16, 2019 2:34 pm
by B.Goode
From reading the developer's documentation here https://risacher.org/sunwait/, don't you simply run whatever command is appropriate once the scheduled wait time expires?

So something like:

Code: Select all

sunwait <arguments to control sunwait> ; command_to_run_after_waiting

Re: Trigger script on sunset and -rise

Posted: Sat Feb 16, 2019 2:45 pm
by DougieLawson
I run sunwait from a crontab twice per day.

Code: Select all

45 4 * * * root /usr/local/bin/sunwait civ up +0:00:00 51.200000N 1.130000W ; /usr/local/bin/sunny.sh light;
30 15 * * * root /usr/local/bin/sunwait civ down -0:00:00 51.200000N 1.130000W ; /usr/local/bin/sunny.sh dark;
That gets the sunny.sh shell script run at civil sunrise and sunset.

The sunny.sh script simply writes a file.

Code: Select all

#!/bin/bash

NOW=$(TZ=Europe/London date '+%T %d/%m')

if [ "$1" == "reboot" ]; then
  if [ ! -f /var/run/lightordark ];
  then
    echo $1' @ '$NOW > /var/run/lightordark
  fi
  else
  echo $1' @ '$NOW > /var/run/lightordark
fi
I also run that script from /etc/rc.local so I'll know if the Raspberry gets rebooted.

Re: Trigger script on sunset and -rise

Posted: Sat Feb 16, 2019 4:15 pm
by bananaboat
For some reason i don't have the script sitting in directory /usr/local/bin/
But in /home/pi/sunwait/0.8/

I downloaded the files, unpacked it there and did make. Should i have added some valuables during make?

Re: Trigger script on sunset and -rise

Posted: Sat Feb 16, 2019 4:27 pm
by DougieLawson
Everything in /usr/local/... is locally created.

Re: Trigger script on sunset and -rise

Posted: Mon Feb 18, 2019 10:06 am
by bananaboat
pi@raspberrypi:~ $ locate sunwait
/home/pi/sunwait
/home/pi/sunwait-20041208.tar
/home/pi/sunwait/0.8
/home/pi/sunwait/0.8/makefile
/home/pi/sunwait/0.8/print.cpp
/home/pi/sunwait/0.8/print.h
/home/pi/sunwait/0.8/print.o
/home/pi/sunwait/0.8/sunriset.cpp
/home/pi/sunwait/0.8/sunriset.h
/home/pi/sunwait/0.8/sunriset.o
/home/pi/sunwait/0.8/sunwait
/home/pi/sunwait/0.8/sunwait.cpp
/home/pi/sunwait/0.8/sunwait.h
/home/pi/sunwait/0.8/sunwait.o
/home/pi/sunwait0-8.tar

Re: Trigger script on sunset and -rise

Posted: Mon Feb 18, 2019 11:42 am
by B.Goode
Is the point that you want to invoke sunwait from Cron using a crontab entry like the one offered by @DougieLawson?

Perhaps there are 2 broad options:

Copy the sunwait executable to a location where it will be found by default by Cron.

Or
Modify the crontab entry so that the explicit path to sunwait points to the place where you have installed the sunwait executable.

Re: Trigger script on sunset and -rise

Posted: Mon Feb 18, 2019 3:49 pm
by DougieLawson
B.Goode wrote:
Mon Feb 18, 2019 11:42 am
Copy the sunwait executable to a location where it will be found by default by Cron.
That's what I did.
sudo cp /home/pi/sunwait /usr/local/bin

Re: Trigger script on sunset and -rise

Posted: Mon Feb 18, 2019 3:49 pm
by scruss
bananaboat wrote:
Mon Feb 18, 2019 10:06 am
pi@raspberrypi:~ $ locate sunwait
Did you compile it with make and install it with sudo make install?

I use the original Risacher sunwait, not the sunwait4windows version. The original does one thing well, the other … lots of things and who knows how well? I think it also changes the command format

Re: Trigger script on sunset and -rise

Posted: Tue Feb 19, 2019 11:45 am
by bananaboat
scruss wrote:
Mon Feb 18, 2019 3:49 pm
Did you compile it with make and install it with sudo make install?

I use the original Risacher sunwait, not the sunwait4windows version. The original does one thing well, the other … lots of things and who knows how well? I think it also changes the command format
I did do make on the sunwait4windows version.
sudo make install i did as well:
pi@raspberrypi:~/sunwait/0.8 $ sudo make install
make: *** No rule to make target 'install'. Stop.

Re: Trigger script on sunset and -rise

Posted: Mon Feb 25, 2019 12:55 pm
by bananaboat
DougieLawson wrote:
Sat Feb 16, 2019 2:45 pm

Code: Select all

45 4 * * * root /usr/local/bin/sunwait civ up +0:00:00 51.200000N 1.130000W ; /usr/local/bin/sunny.sh light;
30 15 * * * root /usr/local/bin/sunwait civ down -0:00:00 51.200000N 1.130000W ; /usr/local/bin/sunny.sh dark;
I have been testing for a while now. But for some mysterious reason am i able to run the command without trouble from prompt like this:

Code: Select all

 ./sunwait wait set offset +5:09:00 51.7547N, 51.8987E ; python /home/pi/relayOn.py;
But when i create a cron out of it, it does nothing:

Code: Select all

13 35 * * * /home/pi/sunwait/0.8/sunwait wait set offset +5:09:00 51.7547N, 51.8987E ; python /home/pi/relayOn.py;
After i created/edited the cron, i do a cron reload like so: /etc/init.d/cron reload

The /var/log/syslog doesn't show anything
What am i missing?

Re: Trigger script on sunset and -rise

Posted: Mon Feb 25, 2019 3:28 pm
by scruss
the comma: you're including a comma in the command line which shouldn't be there.

Also, please consider using crontab -e instead of editing system files. It manages changes for you reliably.Also, you're running these commands as root - do you really need to?

Re: Trigger script on sunset and -rise

Posted: Tue Feb 26, 2019 11:52 am
by bananaboat
scruss wrote:
Mon Feb 25, 2019 3:28 pm
the comma: you're including a comma in the command line which shouldn't be there.
The comma was indeed the bug, i guess i must have copied it from some tutorial.
Cheerz!

Ps. i do use crontab -e none root. Unless something doesn't work the way it should i turn to root.

Re: Trigger script on sunset and -rise

Posted: Sat Sep 07, 2019 3:22 pm
by etanol
Thanks for your quick reply.

I downgraded sunlit to the version you linked so now I have version 0.1

Code: Select all

$ sunwait -V
sunwait version 0.1
It has less options but I thought I have the syntax right when I for example put this in cron:

Code: Select all

03 17 * * * /usr/local/bin/sunwait civ down offset 03:16 59.375475N 18.038067E ; /usr/bin/tdtool -n 1 ;
The second command is no longer run directly at the time when cron supposed to run sunwait but now it never seems to run the second command at all. In syslog I can see that cron runs sunwait correctly.

Code: Select all

Sep  7 17:03:01 asporasp CRON[7734]: (pi) CMD (/usr/local/bin/sunwait civ down offset 03:16 59.375475N 18.038067E ; /usr/bin/tdtool -n 1 ;)
Any advice is much appreciated !

Re: Trigger script on sunset and -rise

Posted: Sat Sep 07, 2019 4:15 pm
by DougieLawson
I'm running it without any offset. Twice a day (04:45 is (hopefully) earlier than the earliest sunrise in summer, 15:30 is earlier than the earliest sunset in winter).

Code: Select all

45 4 * * * root /usr/local/bin/sunwait civ up +0:00:00 51.24N 1.13W ; /usr/local/bin/sunny.sh light;
30 15 * * * root /usr/local/bin/sunwait civ down -0:00:00 51.24N 1.13W ; /usr/local/bin/sunny.sh dark;
I also wrote a python program to do a similar thing

Code: Select all

#!/usr/bin/python3
from Sun import Sun
import math
import datetime
import time

coords = { 'latitude' : 51.24 , 'longitude' : -1.13 }

sun = Sun()
now = datetime.datetime.now()
nowU = time.mktime(now.timetuple())

dt_tmRise = sun.getSunriseTime( coords)['dt_tm']
riseU = time.mktime(dt_tmRise.timetuple())

dt_tmSet = sun.getSunsetTime( coords)['dt_tm']
setU = time.mktime(dt_tmSet.timetuple())
#print ("now:", now.strftime('%c'), " sunset vs now:",  SSMnow, " now vs sunrise:", SRMnow, " SR:", dt_tmRise.strftime('%c'), " SS:", dt_tmSet.strftime('%c'))
#print ("SR:", dt_tmRise.strftime('%c'), " SS:", dt_tmSet.strftime('%c'), "nowU:", nowU, " riseU", riseU, " setU", setU, riseU - nowU, setU - nowU)

print(dt_tmRise.strftime('%c'), dt_tmSet.strftime('%c'))

if ((riseU-nowU) < 300):
  print (now.strftime('%c'), "Less than five minutes until sunrise @ ", dt_tmRise.strftime('%c'))

if ((setU-nowU) < 300):
  print (now.strftime('%c'), "Less than five minutes until sunset @ ",dt_tmSet.strftime('%c'))

Re: Trigger script on sunset and -rise

Posted: Sat Sep 07, 2019 7:33 pm
by scruss
etanol wrote:
Sat Sep 07, 2019 3:22 pm
Thanks for your quick reply.
No problems. I wonder what happened to it, though? Or is it just me who can't see my post with the link to http://risacher.org/sunwait/sunwait-20041208.tar.gz ?
It has less options but I thought I have the syntax right when I for example put this in cron:

Code: Select all

03 17 * * * /usr/local/bin/sunwait civ down offset 03:16 59.375475N 18.038067E ; /usr/bin/tdtool -n 1 ;
Sunwait v0.1 doesn't use the "offset" syntax: it uses +HH:MM or -HH:MM.

So what you're asking it to do is, for a place just north of Stockholm, Sweden:
  1. have cron trigger the sunwait script at 17:03, which is currently before civil twilight ends but won't be in a little over two months. Did you mean at 03:17 (or "17 03" in cron-speak)?
  2. At civil twilight (when began at 20:13 this evening) wait for a further 3 h 16 minutes: so at 23:29 today, launch tdtool.
(all time should be local to you, if I remembered to use TZ=SWT-1SST correctly)

If you call sunwait after its trigger time it will wait until the next day. So on December 7, when your sunset is 15:49 and civil twilight ends at 16:43, sunwait will wait until the evening of December 8th to run that script. Probably not what you want.

Re: Trigger script on sunset and -rise

Posted: Sat Sep 07, 2019 8:59 pm
by etanol
Thank you for your replies and explanations, what really confused me is the differences in syntax between sunwait version 0.1 version 0.8 and it doesn’t make it easier that V0.1 does not say much about the syntax when asking for examples with sunwait -help…
”scrubs" wrote:Sunwait v0.1 doesn’t use the ”offset" syntax: it uses +HH:MM or -HH:MM.
Yes I had just discovered through DougieLawson answer that V0.1 does not use the offset command.
”scrubs" wrote: So what you're asking it to do is, for a place just north of Stockholm, Sweden:
have cron trigger the sunwait script at 17:03, which is currently before civil twilight ends but won't be in a little over two months. Did you mean at 03:17 (or "17 03" in cron-speak)?
Yes I meant 17:03 because I wanted to test it and see if it worked then, that is also the reason for the large offset. I know I have to run the cron command at ca 14:40 to have it run before sunset in december…
”scrubs" wrote:At civil twilight (when began at 20:13 this evening) wait for a further 3 h 16 minutes: so at 23:29 today, launch tdtool.
(all time should be local to you, if I remembered to use TZ=SWT-1SST correctly)
In V0.8 a positive offset is shifts the time towards noon, is the opposite in V0.1? So what I tried to do is have it wait until 17:05 (20:21-03:16 ) hence a delay of only two minutes from 17:03 (it was 17:01 when I edited the crontab).
”scrubs" wrote:If you call sunwait after its trigger time it will wait until the next day. So on December 7, when your sunset is 15:49 and civil twilight ends at 16:43, sunwait will wait until the evening of December 8th to run that script. Probably not what you want.
That makes sense :-)

However, I just tried this

Code: Select all

$ source/sunwait-v0.8/sunwait -wait civ down offset -02:22 59.375475N 18.038067E; date;
and at the expected moment it returned;

Code: Select all

lör  7 sep 2019 22:43:28 CEST
and the older version sunwait 0.1 that is in /usr/local/bin/ just sits and waits forever when I for example run

Code: Select all

sunwait civ down 02:32 59.375475N 18.038067E; date;
and

Code: Select all

sunwait civ down -02:32 59.375475N 18.038067E; date;
Isn’t that strange?

Re: Trigger script on sunset and -rise

Posted: Sat Sep 07, 2019 9:55 pm
by scruss
etanol wrote:
Sat Sep 07, 2019 8:59 pm
In V0.8 a positive offset is shifts the time towards noon, is the opposite in V0.1? So what I tried to do is have it wait until 17:05 (20:21-03:16 ) hence a delay of only two minutes from 17:03 (it was 17:01 when I edited the crontab).
A positive offset just adds a delay on top of the trigger time, a negative one makes it earlier. It doesn't know about noon. For instance, this crontab line has been turning my front light on an hour before sunset since about 2014:

Code: Select all

 03 00   *   *   *   /usr/local/bin/sunwait sun down -1:00:00 43.729N 79.292W; /usr/local/bin/heyu fon h1
and the older version sunwait 0.1 that is in /usr/local/bin/ just sits and waits forever when I for example run

Code: Select all

sunwait civ down 02:32 59.375475N 18.038067E; date;
and

Code: Select all

sunwait civ down -02:32 59.375475N 18.038067E; date;
Isn’t that strange?
I think the offset needs the + or - in front of it.

Also, I seem to remember that if you're past the trigger event time for the day, sunwait waits until the next day: it doesn't factor in the offset in any calculation. So if it was after 20:13 SST and I entered

Code: Select all

sunwait civ down +2:32 59.375475N 18.038067E; date
It would likely print the date at 8 sep 2019 22:42 (since your sunset's three minutes earlier tomorrow). That might be your "waiting forever".

If you use the -v option and see
Warning: event already passed for today, waiting till tomorrow.
in the output, you'll know you're in for a long wait.

I agree that the docs aren't great.

Re: Trigger script on sunset and -rise

Posted: Sun Sep 08, 2019 8:11 pm
by etanol
Thanks for the reply I finally got it working.
”scrubs” wrote:I think the offset needs the + or - in front of it.
Eureka! The + sign was all that was missing to get the offset to work in Sunwait v0.1! Without the + (or -) sign it ignores the offset completely and uses Offset = 0:00:00
scruss wrote:
etanol wrote: Sat Sep 07, 2019 9:59 pm
In V0.8 a positive offset is shifts the time towards noon, is the opposite in V0.1? So what I tried to do is have it wait until 17:05 (20:21-03:16 ) hence a delay of only two minutes from 17:03 (it was 17:01 when I edited the crontab).
A positive offset just adds a delay on top of the trigger time, a negative one makes it earlier. It doesn't know about noon. For instance, this crontab line has been turning my front light on an hour before sunset since about 2014:
So in v0.1 it actually is quite the opposite of Sunwait v0.8, not only a plus sign was not needed, a negative offset for sunset makes it later not earlier, and the format is [MM|HH:MM] (in stead of [±HH:MM:SS|±HH:MM] )

from Sunwait v0.8 -help

Code: Select all

Offset:
    offset [MM|HH:MM] Time interval (+ve towards noon) to adjust twilight calculation.