Dusk to Dawn IFTTT Webhooks PHP Script - Ver 2.1
(Another donateWare script from MRE Software)

 

     I messed up and bought an Echo Dot which sparked an interest in home automation.  It started with controlling my entertainment center using a Logitech Harmony Hub and has reached the point of controlling the lights, fans, and other stuff.  One problem I had been thinking about for while looked solvable with IOT (Internet of Things) so I bought some Sonoff Basic switches to experiment with.

     The problem:  I had some outside lights positioned so far under the eaves of a barn roof that the photo controls never worked properly.  It was just too dark under there all the time and the lights stayed on far too long every day.  There was no practical way to use a remote photocell but all the lights were within range of my outdoor wifi access point.

     The solution:  I replaced the photo controls on each light with a Sonoff Basic.  They worked great but of course I encountered a second problem.  The eWelink app that controls the Sonoffs only offers timer and scheduling functions, no dusk to dawn control.  I did not like the idea of having to change the schedule every few weeks throughout the year as the length of day changed.

     The solution solution:  After some research I discovered the IFTTT websiteIFTTT is free (now $10/year for eWelink/Sonoff) and offers a "Webhooks" service that allows you to control eWelink devices like the Sonoff Basic.  Basically you send a trigger event to webhooks and the IFTTT applets you have defined switch your devices.  I tried using the Weather Underground sunrise/sunset trigger on IFTTT but wanted more control so I crafted my own script and used a cron job to execute it right here on the MRE Software web host.  I offer it here for others to use and experiment with.

 

  To use this:
* You need a host server that supports cron jobs and php. My host is running Linux.
* You need an IFTTT account. (free)
* You need an ewelink account. (free) (or any other IFTTT supported app)
* Build your applets on IFTTT to trigger on weblinks "dusk" or "dawn" and operate your switches or any other thing supported by IFTTT.
* Edit this script for your time zone, weblinks key, email address, latitude, longitude, $dawn_skew and $dusk_skew.
* If you get tired of the emails edit the email address to be just the word false with no quotes.
* Save the file as duskdawn.php and copy it to your server.
* Set up a cron job to run it every 5 minutes. Something like: */5 * * * * /usr/local/bin/php /home/youracct/public_html/duskdawn.php>/dev/null 2>&1

Notes:
Set your local time zone using the proper php time zone list as referenced in the script. Set the variable $key to your webhooks key as issued on IFTTT. You can get this from the webhooks documentation on their site. Set $Lat to your latitude in decimal format. Positive for north and negative for south. Set $Lon for your longitude. Positive for east and negative for west. Set $dawn_skew for the number of minutes after dawn that you want the "dawn" signal sent to webhooks. Set $dusk_skew for the number of minutes before dusk that you want the "dusk" signal sent to webhooks. Note that the "_skew" variables can be negative and fractions. If you want 6 minutes and 30 seconds use a value of 6.5. If you want the lights to come on 3 minutes after dusk set $dusk_skew to -3. You should also note the $repeat variable set to 3. By default this script sends "dawn" 4 times and "dusk" 4 times to webhooks. This was done because sometimes a switch can be offline or a service can be down and the signal is missed. $repeat allows the user to send multiple signals at 90 second intervals. In most cases this hurts nothing as a switch that is already on or off will just remain in that state on if it gets a second command. Feel free to change this to whatever value between 0 and a few that you desire. Zero will send the command only once, 1 will repeat it once more, and so on.

Windows server issue:
This script may time out on older versions of php on Windows servers. It uses the sleep() function to wait for dawn or dusk and the wait can be up to 5 minutes. Windows may count sleep time as execution time. This does not occur on Linux based hosts. If you run into this problem you may need to comment out the the sleep() lines of code. The script will still work but the resolution will be 5 minutes instead of 10 seconds and the repeat intervals won't work properly.

Miscellaneous:
The built in report functions can offer you insight into the times php calculates for the sun information. This can be run from a web browser and will return the results in a human readable form for tweaking and debugging. If you have basic knowledge of php feel free to tinker with the code.

You can also use this script to do many other things during the day by setting other trigger times and event strings. For example I have one light that is in a very dark area and I want to leave it on an extra 15 minutes in the mornings. I added this line between the dawn and dusk events.

$result = test_for($dawn + 900, "dawnPlus", false); // add 15 minutes for some lights

This adds 900 seconds (15 minutes) to $dawn and sends the dawnPlus string to webhooks. My IFTTT applet for that light responds to "dawnPlus" instead of "dawn" like all the other lights. The false parameter stops it from sending me an email when it triggers.

Pay particular attention to the USE AT YOUR OWN RISK notice! One thing I have learned about the Internet of Things is that a lot of Things have to work properly for the job to get done. Needless to say, don't use this stuff for any critical application!

-----------------------------------------------------------------------------------------------------------
DISCLAIMER:

THIS CODE SHOULD BE USED AT YOUR OWN RISK. IT IS PROVIDED AS IS. THE AUTHOR, THIS WEBSITE, NOR THE WEB HOST
ASSUMES ANY RESPONSIBILITY FOR THE SAFETY OR ACCURACY OF THIS INFORMATION. THERE IS NO WARRANTY EITHER
EXPRESSED OR IMPLIED THAT THIS CODE IS SAFE AND NO LIABILITY WILL BE BORNE BY ANY PARTY INVOLVED IN PREPARING
OR PUBLISHING THESE DOCUMENTS OR SOFTWARE. THIS SCRIPT SHOULD BE CONSIDERED EXPERIMENTAL AND IS OFFERED HERE
FOR INFORMATIONAL PURPOSES ONLY.

USE AT YOUR OWN RISK!
-----------------------------------------------------------------------------------------------------------

Copy and paste from the text box below or to download a complete package of files (with a free bonus script) for duskdawn.php

ENJOY!

 


Photocell mod

Sonoff Basic a in medicine bottle.

A little hardware hacking ... Just for fun:
In my original lights, which were under the eaves and out of the weather, the photocells were attached through a hole on the side so I used a short length of wire to replace them with Sonoff Basics.  I have a few other lights that use the twist plug photo controls so I purchased some tall profile "shorting caps", removed the shorting jumper, and modified them with a little hot glue to hold the circuit board from a Sonoff Basic.  No light fixture modification is required as the line, load, and neutral, are all on the twist plug.  Just build the cap and swap out with the original control.  Simple wifi controlled yard light and the cap has a cool green glow at night!

The lower picture utilizes a 50mm medicine bottle as a weatherproof enclosure for a Sonoff Basic.  Hey, it might look a little Red-Neck but it is waterproof and serviceable.  Put it up high on a light fixture and nobody will notice and it might even earn you some brownie points here in South Alerbamer.

 


 
 
If you find this code of value, donate a little to the cause.  Think of the time you just saved!
USE AT YOUR OWN RISK!

Questions / Comments?  Email me.

MRE Logo