This is a guest post by DAKboard user Jason McLeod. We noticed Jason’s blog comment discussing how he had used his Nest Thermostat to turn his DAKboard off when he was away. This sounded like an awesome integration, so we asked Jason if he’d be willing to document the steps he took, and he graciously accepted! So without further delay, here is Jason’s post!
My name is Jason McLeod and I work as a Configuration Manger specializing in process automation. My wife actually requested some sort of screen in the kitchen to make it a bit easier to keep track of where she and the kids needed to be day to day. DAKboard was a good fit and gave her the information she needed. Plus I’d been looking for a project involving a Raspberry Pi so it was a good excuse. In the end it only took a single evening to get everything pulled together and working the way I wanted.
Then I got to thinking how to set the screen on and off when we were home, and remembered that the Nest Thermostat had that information.
Setup the Nest Python Bundle on the Raspberry Pi
Nest has a python bundle that allows you to pull information from your account.
Start by installing the Nest bundle.
sudo pip install python-nest
Then setup our script to tell us if Nest is set to Away.
Add the script, making sure to update the username and password for your Nest account.
#!/usr/bin/python import nest username = '' password = '' napi = nest.Nest(username, password) for structure in napi.structures: print str(structure.away).lower()
Mark our script as executable.
chmod +x /usr/local/bin/nest_away
Running nest_away should now say false when you’re home and true when your Nest is set to away.
Turn the Monitor On/Off
Now we need to do something with our Away status! Lets turn the monitor on and off based on the status.
Setup our script
#!/bin/bash last=$(cat /home/pi/.nest) now=$(/usr/local/bin/nest_away) if [ "$last" = "$now" ]; then exit 0 else echo $now > /home/pi/.nest if [ "$now" = "true" ]; then /home/pi/rpi-hdmi.sh off else /home/pi/rpi-hdmi.sh on fi fi
nest.sh stores the last run results so that when we are away or home, we’re not spamming turning the display on or off when it is already in that state. We write the true/false value into a hidden file in the pi user’s home directory /home/pi/.nest. When the script runs and finds that the values are different, it decides to turn on or off the display.
Now we just need to add a cron job to run our new script.
Adjust the times to suit your own needs.
# Turn HDMI Off (12:59am) 59 0 * * * /home/pi/rpi-hdmi.sh off # Turn HDMI On (6:30am) 30 6 * * * /home/pi/rpi-hdmi.sh on # Test if we are away every 5 minutes but only during the hours we would have the display on. */5 7-23 * * * /home/pi/nest.sh
Mobile Push Notifications (Optional)
Bonus: I wanted to know that the script was actually running when I was away, since I wasn’t at home I couldn’t check. So I setup a push notification to my iPhone using Prowl.
apt-get install python-prowl
You’ll need to get an API key from your Prowl account at www.prowlapp.com
Insert the script and update the key to yours.
import prowlpy prowl = prowlpy.Prowl("Your_API_Key") def str2bool(v): return v.lower() in ("yes", "true", "t", "1") with open('.nest', 'r') as nest: data=nest.read().replace('\n', '') if str2bool(data): prowl.add('Dashboard','Update','House is Away',None,None) else: prowl.add('Dashboard','Update','You are Home',None,None)
Modify nest.sh to perform the push notification. We’ll add the prowl script inside the part of the if statement where we turn the display on or off.
#!/bin/bash last=$(cat /home/pi/.nest) now=$(/usr/local/bin/nest_away) if [ "$last" = "$now" ]; then exit 0 else echo $now > /home/pi/.nest if [ "$now" = "true" ]; then /home/pi/rpi-hdmi.sh off else /home/pi/rpi-hdmi.sh on fi /usr/bin/python /home/pi/prowl.py fi