Raspberry Pi + IFTTT: Is There Power at Home?

Raspberry Pi + IFTTT: Is There Power at Home?Raspberry Pi, IFTTT, Dropbox, and a Cron Job will answer the question.

Maximilien MonteilBlockedUnblockFollowFollowingJan 6Photo by Louis Reed on UnsplashI live in a place where the power cuts regularly.

It usually only happens about twice a day and lasts only a few seconds, it’s become part of daily life here, you get used to it.

But recently, the power went out and stayed out for over 24 hours.

This has happened a few times before, and each time there’s nothing I can do about it.

I have to move to a coffee shop or similar to continue working hoping that by the time I return, the power will be too.

I’m sure a lot of people face a similar problem, maybe even more often than I do.

You’re out and you don’t know when you can come back home, hell you never know if the power went out while you were away.

I thus wanted to solve this problem with a solution that would let me know when the power is back or just check if it’s on.

Let’s get into it!ConsiderationsThe idea is that I need something I can plug in at home and communicate with.

Once the device is powered on it can check whether or not I asked about the power situation and answer.

A Raspberry Pi is the perfect, low-power option here, and it’s what I had on hand so we’ll go with that.

CommunicationNext, we need a way to talk to and receive answers from the Raspberry Pi.

Not only does it need to be as cheap as possible (preferably free), it must be able to store the message.

This is important because if the Pi is currently powered off, it has no way of receiving a message and I don’t want to be constantly pinging it until it does.

Here are a few options I considered:TwilioLets you send and receive SMS messages with their API.

It has a free tier but only until you run out of credit.

It also can’t store our messages.

GmailThis option lets us store our messages but it would require every person following this guide to create a new email address.

I don’t know if Google would mind, otherwise this is a valid option.

IFTTT + DropboxIFTTT offers a bunch of different applets and services that together allow us to both communicate with the Pi and store our messages, all for free (though there are some limitations).

This is what we’ll go with.

IFTTT SetupIf you don’t have one already, the first step is to create an account at IFTTTIFTTT helps your apps and devices work togetherIFTTT (if this, then that) is the easy, free way to get your apps and devices working together.


comIn order for us to have two-way communication with our Raspberry Pi, we’re going to need to create two IFTTT applets, a Sender and a Listener.

But before making these applets we have to connect our IFTTT account to a few services.

Head over to My Applets and go to the Services tab.

There, we’re going to add the SMS, Dropbox, and Webhooks services.

For an SMS free method, connect Google Assistant and Gmail (or even Facebook Messenger) instead of SMS.

Of course you can mix and match them as you wish.

I’ll explain what each does and how to set them up next.

Sender AppletFirst we will make the applet that allows us to send messages to our Raspberry Pi.

The messages won’t be sent directly, instead we want to store them somewhere the Pi can check once it goes online, for that we will use Dropbox.

Click on New Applet and then on the blue +thisSearch for the SMS service and click on it (you can instead add Google Assistant, Gmail, or any other service with you which you can send a message)We’ll be choosing the Send IFTTT any SMS optionClick on the blue +thatSearch for the Dropbox service and click on itChoose the Append to a text file optionFill it out as per the image, you can change the file name and location if you want but keep them in mindPerfect!.Now if you send an SMS to IFTTT (you can get the number in the settings of the SMS service) you should see a new file appear in your Dropbox at the path you gave.

Be aware that there is a limit to the number of SMS that IFTTT can send and receive (100 in US and Canada, 10 elsewhere per month).

Listener AppletNext we can make the second applet which will let our Raspberry Pi talk to us.

Just like before, create a new applet but for +this choose WebhooksClick on the Receive a web request optionGive your event a name, I went with there_is_powerFor +that I chose SMS as my messaging serviceYou can write whatever message you want to receive here or put {{Value1}} if you want our script to decide on the messageThat’s it for IFTTTWe now have a way to send a message that the Pi can read and a way for it to then communicate with us.

The next step is to enable the Dropbox API so that our Pi can check for a message.

Dropbox SetupFirst go to the Dropbox App Console and click Create appDevelopers – DropboxBuild your app on the DBX Platform.

Getting started is simple and quick.



comFill in the info as below, we’re choosing Dropbox API, Full Dropbox access, and naming our app isTherePowerThen, in the Dropbox console, click the Generate button under the Generate access token heading to obtain your token, save it for the next step.

Time to ScriptYup, that’s part of our scriptSince we’ll be working on a Raspberry Pi, the go to language is Python.

Clone the project from github to the home directory of your Pi and install it with these commands.

$ git clone https://github.


git$ pipenv installNote that you might need to install git, pip3, and pipenv if you don’t already have them, they’re aren’t obligatory but it makes the process a whole lot simpler.

$ sudo apt-get install python3-pip git# if you want to use pipenv$ pip3 install –user pipenvIf you’re not using pipenv, install Requests and Dropbox$ pip3 install requests dropboxThe Scriptimport osimport dropboximport requestsdef main(): folderPath = '/IFTTT/SMS/' fileName = 'is_there_power.

txt' print('Connecting to Dropbox.

') # Instantiate dropbox object dbx = dropbox.


environ['DBX_ACCESS_TOKEN']) print('Searching for message.

') searchResult = dbx.

files_search(folderPath, fileName, max_results=1) if len(searchResult.

matches) == 1: print('Message found! Sending reply.

') requests.



com/trigger/there_is_power/with/key/' + os.

environ['WEBHOOK_KEY'], data={'value1': 'Looks like there is power!'}) # cleanup print('Reply sent, cleaning up.

') dbx.

files_delete(folderPath + fileName) print('Done!') else: print('No message received.


')if __name__ == '__main__': main()First we import the required packages, here I bring in os because I placed sensitive variables in a .

env file in the same directory.

If you know you wont be sharing your version of the script, you can remove the import and replace every os.

environ[] with your actual information.

In our main function, we set our folder path and file name according to what we put in the Sender Applet, note that here we do need the file extension.

Next, we need to prepare our Dropbox object to allow us to interact with the API, this is where you use the Access token you got in the Dropbox Console.

With this Dropbox object authenticating us, we can check if a message was sent.

We do that by searching for our message file, if there is only one result it means a message was sent.

This is the reason we chose to append a line in the Sender Applet instead of creating a new file.

We don’t read the file but simply check for its existence.

If a message was sent, we then use our IFTTT Webhook URL to send a POST request.

The data we send will be the message that you receive when the Pi answers.

Take note if you changed the event name, it is used in this URL.

Finally, once we’ve sent our reply we can delete the file, this is like the Pi saying it has read your message.

With this we have the main setup for our script, but next, we want the Raspberry Pi to automatically check for messages at the right moments.

Checking for messages on bootThe first time we want the Pi to check for messages and answer is right when it boots up, when power returns.

The problem with checking right on boot is that the Pi might not yet be connected to your router and even then, it may be connected but not have WiFi access.

Let’s solve each of these.

Wait to Connect to RouterFirst let’s make the Pi run the script only once it has finished connecting to the router.

We will create a service using systemd to run our script at the right time.

pi ~$ sudo systemctl edit –force –full isTherePower.

serviceThis opens an editor, write the following.

[Unit]Description=If asked, tell the user there is powerAfter=network-online.

target[Service]Type=idleUser=piWorkingDirectory=/home/pi/isTherePower# If using pipenvExecStart=/home/pi/.

local/bin/pipenv run python /home/pi/isTherePower/isTherePower.

py# OtherwiseExecStart=/usr/bin/python3 /home/pi/isTherePower/isTherePower.


targetTo quit nano and save press CTRL + X then Y then press enter.

Now let’s check if our new service was added.

pi ~$ systemctl status isTherePower.

serviceIt should give you back your service’s name, description, and say it is inactive.

Next we can enable our service withpi ~$ sudo systemctl enable isTherePower.

serviceFinally we can test if it works (if you’ve sent a message to dropbox) by restarting the pi or running:pi ~$ sudo systemctl start isTherePower.






uk/2015/10/how-to-autorun-a-python-script-on-boot-using-systemd/Wait for WiFiWe’ve solved the first problem of when to run the script, now we need to make sure that when it runs, the Pi has access to the internet.

To do that we’ll add a new function that will keep trying to connect to the internet, once it does, we can go ahead with the rest of the program.

def wait_for_internet_connection(url='http://www.


com', timeout=3): print('Waiting for an internet connection.

') while True: try: requests.

head(url, timeout=timeout) print('Connection OK') return except requests.

ConnectionError: passI’m using www.


com here as a website that is certain to stay online for the foreseeable future.

You could also use www.



In the loop we issue a HEAD request because our goal isn’t to receive and download any HTML, that would just slow us down.

A HEAD request will just ping the URL and get the headers.

Of course don’t forget to call our new function before main.

if __name__ == '__main__': wait_for_internet_connection() main()With this, we have a program that will alert you whenever the Pi powers on and sees that you’ve sent a message asking about power.

You could totally stop here but I the Pi to be able to answer even when there was power, otherwise without an answer I might think there was an outage.

Checking for messages while powered onThere are many ways for the Pi to check if you’ve sent a message.

With our setup, that means watching when the is_there_power is created and/or changed.

Dropbox WebhooksThe Dropbox API offers its own Webhooks to alert you when a file or folder changes.

I think this would be the best option but you need to allow your Raspberry Pi to be accessible from the internet which my router and ISP do not allow.

Cron JobCron allows Linux to run commands or scripts periodically at a given date and time.

While not as slick as Webhooks, this allows us to check Dropbox every once in a while and see if you’ve sent a message.

Creating a Cron jobCron works by creating jobs.

Each is a single line in a file that will run the specified command at every given interval.

The syntax for a Cron job is as follows* * * * * command to execute- – – – -| | | | || | | | —– Day (0 – 7) (Sunday=0 or 7)| | | ——- Month (1 – 12)| | ——— Day of the month (1 – 31)| ———– Hour (0 – 23)————- Minute (0 – 59)If you want to learn more about Cron read this articleHow To Add Jobs To cron Under Linux or UNIX – nixCraftHow do I add cron job under Linux or UNIX like operating system?.Cron allows Linux and Unix users to run commands or…www.


bizFor our program, I think every 30 minutes is a reasonable time to check, worst case that amounts to 48 times a day.

It does mean that it can take up to 30 minutes for the Pi to answer.

It’s totally up to you to find a frequency you like.

To create (or edit) a Cron job enter:pi ~$ crontab -eYou might be prompted to choose an editor, nano is the easiest to use so go with that if you’re not sure.

At the end of the file, add the following line# if using pipenv*/30 * * * * cd /home/pi/isTherePower; /home/pi/.

local/bin/pipenv run python isTherePower.

py 2>&1# otherwise*/30 * * * * python3 /home/pi/isTherePower.

py 2>&1The 2>&1 at the end is to prevent Cron from sending you emails each time it runs a job.

If you also want to store the print statements of our program into a log add>> /path/to/file/log.

txtright after isTherePower.

py (including the >>), it will output the print statements to a log.

txt file at the path you write.

ConclusionAnd with that our little program is done!You’ll never be caught by surprise again by the power situation in your home.

We also got to work with a bunch of different services and technologies to create a solution to a problem that is still far too common.

ImprovementsPrevent the Cron job from running constantly in the background if you’re homeIf you can, use Dropbox WebhooksChange the text written in dropbox to also include the timeLet the Pi keep a log of when the power cutsUse Telegram for both messaging and storingTry out different IFTTT servicesThank you for reading!Leave any comments and suggestions you may have!.. More details

Leave a Reply