Using a Cron file to fire the lights

Since I also wanted to have lights turn on at a specific time and also turn off, it was time to create a cron job. In my case, I created lightson.py and lightsoff.py which send off the same calls that the local switch program triggers. Depending on which Pi you want to run the cron jobs, you have a slightly different set of actions, so might as well use the same logic as the local switch uses as it is doing the same thing. You could create a cron job on a independent machine, as well, the code would be similar to the slave Pi switch code to fire the lights.

Once we have created these programs, we need to create a simple cron file in the /etc/cron.d directory. The file specifies when the script is called and with what permissions. For example, here is the cron file code for lighton which turns the lights on at a specific time.

Cron files can have multiple jobs in them, and you can edit the file and the changes take effect without rebooting the machine. So, with one file, you can create a situation where the lights go on at sunset, off 4 hours later and then maybe you turn them off again at midnight, just in case someone turned them on with a local switch or web page call and forgot to turn them off.