Categories
General

IFTTT automation using crontab on Debian / Raspberry Pi

We were planning an extended vacation and had concerns about leaving the house empty for so long. As usual you get family and friends to look in every so often but I decided that it would be nice to have some additional security to make the house look occupied.

After a bit of research I decided on Internet Enable D-Link DCS-5030L Camera and a set of Meross Smart Plugs. The D-LINK camera was position in such a way that the door could be monitored while the Meross Smart Plugs were connected to various lights strategically placed around the home.

I setup everything up and it all worked perfectly using the Mobile Apps that I installed onto my Motorola Moto G6 Play. It took a weekend away to realise the problem with this setup – If your mobile was not connected to the mobile network or a WIFI signal then all the automation provided by the Apps became worthless.

The D-LINK camera had a simple solution in that it had its own built in webserver. This meant that I could browse to the camera on my home network where I discovered some settings that would cause it to send a series of photo by email. These photos covered three seconds before and three seconds after any detected movement – which was perfect for monitoring anything dropping through the letterbox.

The Meross SmartPlugs had no such interface and it turned out that the Phone App was all that was controlling them. If you were out of coverage the moment a switch on/off event was scheduled then that event was missed causing either the lights to stay on or off until the next event.

As it happens the Meross Plugs stated that they were compatible with IFTTT so I decided to see if that could help the situation. It was straightforward to get IFTTT to control the plugs and so the problems was solved, or so I though… until I realised that IFTTT needed some external trigger and suddenly I was back to square one :- The phone could be out of coverage and thus unable to control the plugs using IFTTT.

It quickly became apparent that I would need some programmable server attached to my home broadband connection to control IFTTT autonomously. As I had an old unused Raspberry Pi (1st gen) I decided to see if there was anyway it could be used to control the switches via IFTTT.

IFTTT has a facility whereby the item (in my case plugs) can be controlled by posting a trigger event to the IFTTT server. I installed curl onto my Raspberry Pi:-

  • sudo apt update
  • sudo apt install curl

Curl allowed me to send a POST message to IFTTT directly from the command line:-

  • curl -X POST https://maker.ifttt.com/trigger/lights_on/with/key/{KEY}

Obviously “lights_on” and {KEY} would need to be updated with values that have been provided by IFTTT for your own application.

The next step was to get the Raspberry Pi to send those messages at the appropriate time. Fortunatly the Raspberry Pi runs Linux which has a featured called the crontab. The crontab allows you to schedule commands to be executed at specific times and dates.

# m h  dom mon dow   command
 36 17 * * * curl -X POST -X POST https://maker.ifttt.com/trigger/kitchen_on/with/key/{KEY}
 57 18 * * * curl -X POST -X POST https://maker.ifttt.com/trigger/kitchen_off/with/key/{KEY}
 58 18 * * * curl -X POST -X POST https://maker.ifttt.com/trigger/study_on/with/key/{KEY}
 24 19 * * * curl -X POST -X POST https://maker.ifttt.com/trigger/study_off/with/key/{KEY}
 25 19 * * * curl -X POST -X POST https://maker.ifttt.com/trigger/lounge_on/with/key/{KEY}
 30 21 * * * curl -X POST -X POST https://maker.ifttt.com/trigger/kitchen_on/with/key/{KEY}
 36 21 * * * curl -X POST -X POST https://maker.ifttt.com/trigger/kitchen_off/with/key/{KEY}
 36 21 * * * curl -X POST -X POST https://maker.ifttt.com/trigger/lounge_off/with/key/{KEY}
 37 21 * * * curl -X POST -X POST https://maker.ifttt.com/trigger/study_on/with/key/{KEY}
 17 23 * * * curl -X POST -X POST https://maker.ifttt.com/trigger/bedroom_on/with/key/{KEY}
 25 23 * * * curl -X POST -X POST https://maker.ifttt.com/trigger/study_off/with/key/{KEY}
 19 00 * * * curl -X POST -X POST https://maker.ifttt.com/trigger/bedroom_off/with/key/{KEY}

The first five columns in a crontab are used to indicate:- Minute past the hour, Hour in the day, Day of Month, Month and Day in Week. A ‘*’ character in any field means don’t care. Anything after the fifth column is regarded as the command that will be executed.

If you leave open a ssh port on the Raspberry Pi and configure your home router to forward any ssh connections to the Raspberry Pi then it will be possible to login and modify the schedule while away.

In my case this setup worked perfectly for the first five weeks and then everything stopped responding. I suspected the router had gone down so asked a family member to reset it. It turned out that a small water leak from the upstairs bathroom had come through the ceiling and soaked the router.

“But Mousie, thou art no thy-lane,
In proving foresight may be vain:
The best laid schemes o’ Mice an’ Men
          Gang aft agley,
An’ lea’e us nought but grief an’ pain,
          For promis’d joy”

from “To a Mouse” by Robert Burns