I'm trying to make a python program that tells my Raspberry Pi to turn on an electronic device for the hours from 4:00AM to 10:00AM. Can someone tell me how to set a variable "hour" to the current value for hour in time?
Asked
Active
Viewed 433 times
-2
-
here's [code example how to find out whether given time (e.g., '12:00` belongs to the given time interval (e.g., `07:00-22:00`)](http://stackoverflow.com/a/28553237/4279). As [@Benjamin Maurer said](http://stackoverflow.com/a/28882666/4279), it seems like a `cron` job. – jfs Mar 05 '15 at 16:52
1 Answers
2
With the datetime library.
from datetime import datetime
hour = datetime.now().hour
But you might want to try another solution. If you're running Linux on that Rpi (which I assume), you can just use the cron
daemon. That is responsible for launching script at certain times or intervals.
So you can make a "turn-on" and "turn-off" script and just run those with cron. Here some info about cron: https://help.ubuntu.com/community/CronHowto
You basically just have to edit the crontab (with ´crontab -e´ for your user) and add your scripts. Writing a shell script might even be shorter than a python script (depending on what you want to do).

Benjamin Maurer
- 3,602
- 5
- 28
- 49