My brother had an interesting project. He loves his nest and he loves the control that he has of it with his android applications, but he wanted a bit more control. The auto-home feature in the Nest thermostat wasn’t cutting it for him. He decided (and rightly so) that if he had a computer to keep track of who was home and who wasn’t, he could better automate the control of the away/home feature of the Nest. So we sat down and worked on the issue.
We decided that using Tasker along with his and his wife’s cell phones we could have an accurate picture of who was and who wasn’t home. Then with a little bit of logic and programming, we could have a Pi set is home/away status properly.
This is accomplished with three pieces:
- A Python script running on a properly configured (configured as seen in my Preparing your Raspberry Pi Environment post) and secured (As seen in my Securing your Raspberry Pi post) Raspberry Pi to receives status updates from the cell phones.
- A scheduled task to check to see if everyone is away from the home, and if necessary, update the Nest status.
- Two Tasker tasks. One to update the server when the phone has left home, and one to update the server when the phone has returned home.
The Python Tracking Application.
This program is fairly simple. We are just going to create a web application that will allow the different cell phones to check in. For security sake, be sure to secure your server. Let’s look at the code and then I’ll explain it.
from flask import Flask from flask import request import os # Be sure to update this to your own key. key = "jkfdkfadskfdopafdsklfdop" app = Flask(__name__) @app.route("/checkin") def checkin(): if key == request.args.get('key',''): who = request.args.get('who','') doCheckIn(who) return "Thanks " + who else: return "Incorrect Key" @app.route("/checkout") def checkout(): if key == request.args.get('key',''): if 'who' in request.args: who = request.args.get('who','') doCheckOut(who) return "Thanks " + who else: return "Incorrect Key" def doCheckIn(who): file = open('/usr/share/nestStatus/' + who,'w') file.write("1") file.close() def doCheckOut(who): if os.path.isfile('/usr/share/nestStatus/' + who): os.remove('/usr/share/nestStatus/' + who)
The first two functions set up the paths for out application. The path ‘http://your.server.com/checkin’ and ‘http://your.server.com/checkout’ will allow your phone to check in and out. In order to do this, they must contain a couple of GET parameters. Firstly, a key. This is a secret key that we are using to secure your application (Read about securing your Pi for more info on this) and then a the second is the name of the person checking in or out. If the request passes these checks, the request is passed on to the doCheckIn/doCheckOut functions to either create or delete a locking file in your Pi’s /tmp directory. This is how we are going to track who is in the house.