Dusk to Dawn Alexa, eWeLink Web, & IFTTT Webhooks PHP Script - Ver 2.3
(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 $??/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.

     Version 2.2 changes:  eWeLink Web allows you to create your own webhooks without the need for IFTTT.  Frankly, when IFTTT went to a paid subscription model I didn't buy it.  I still have a free account which allows one to have 2 applets but I now use just my eWeLink web account to control my Sonoff lights.  In this version of the script you can trigger either or both both types of webhooks.  See the comments at the beginning of the script.

-----------------------------------------------
     Version 2.3 changes ... A completely free solution:  There is now a way to do this with an Alexa skill and routine.  The skill is: Webhook Routine Trigger which can be found here: https://trigger.esp8266-server.de/  Once you install it in your Alexa account it creates a "doorbell" device.  Then you can log in to your alexa account at the trigger.esp8266-server.de page and rename the device as well as create more "doorbells".  The doorbell idea confused me a little at first but here is how it works.  When the webhook url is accessed by a browser or this script, it essentially rings the "doorbell".  You create a scene in Alexa that does things like turn on all the lights in your outdoor lights group of devices when the doorbell rings.  In other words, the webhook rings a virtual doorbell which triggers a scene that you have defined.  To use this with the script below you will need two "doorbell" urls (one for on and one for off) and then edit the Alexa script variables inside the script.  The mechanism for using the Alexa webhooks is exactly the same as it is for eWeLink.

And the best part:  This method will control any device from any manufacturer that Alexa can control!  I have devices from eWeLink and Tuya controlling various lights.  Just make them all part of your Alexa scene that the "doorbell" triggers and they will all work.  I created a device group called Outside Lights and added all the devices I wanted to control to that group.  This made the scenes simple as all that needs to be done is turn the group as a whole on and off.

I suspect this script could also be adapted for use with Google Home or other smart home platforms.  As long as a webhook can be created to trigger a scene then you should be good to go.  The PHP code below is well commented so hopefully any competent programmer can see the flow and modify it.
-----------------------------------------------

  To use this:
* You need a host server that supports cron jobs and php. My host is running Linux.
* You may need an IFTTT account, and/or an eWeLink web account ($10.00/year), and/or an Alexa/Amazon account with the Webhook Routine Trigger skill enabled.
* Edit this script for your time zone, weblinks key, eWeLink and Alexa webhook IDs, 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.

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