Tag Archives: shell script

Quick script to add things to crontab

Out of all the options I’ve found manually adding items to crontab in an editor is the best… despite all the typing.   I made a quick script to cut down on the typing.

#!/bin/bash
nano /etc/config/crontab
read -p “Press [Enter] if you’re sure…”
crontab /etc/config/crontab
/etc/init.d/crond.sh restart
echo “Confirm change”
crontab -l

It also allows me to avoid vi.   You must have nano installed for this to work or replace with your editor of choice… even vi.

Exit the editor without saving and Control-C out of the script at the pause if you messed up.

Get SMS alerts from your NEST

As soon as we got our dog the downsides of no central A/C and an antiquated open loop steam heating system with manual refill became apparent.   We already had a NEST.   It knows the house temperature – Its on the internet – Can’t it alert me to a problem?   Googling Nest and e-mail or SMS alerts brings you to a highly upvoted request to add this feature.   Most requests added horror stories of fail heating systems and frozen pipes.

After some more googling I found a post and Python script written by Scott Baker and lots of great comments about how to use the script.

I have an always on NAS, a QNAP TS-219+, that allowed SSH access.   After trying to remember my hay day of shell scripting in college I generate the below, which works!

#!/bin/bash

#
# By M. De Remer 2014
#
# All temperatures is Degrees F
#
# Inspired by the article by Scott Baker and comments at
# http://www.smbaker.com/a-python-api-for-the-nest-learning-thermostat
#
# Remember to make these file(s) executable ‘chmod +x nestalert.sh nest.py’
#
# More at http://www.bayarddesign.com
#
# Remember to add this to /etc/config/crontab
# Suggested entry /[path]/nestalert.sh > /[path]/nestalert.log
#
# Requirements
# – Python
# – nest.py
# – sendmail or similar
# – a NEST
# – an e-mail account or better yet a Email-to-SMS gateway like vtext.com
#
# Change these items to match your accounts
#
# Target E-mail, for multiple recipients add comma between addresses
EMAIL=”you@you.com, yournumber@vtext.com”
#
# Nest Login info
USERNAME=”you@you.com”
PASSWORD=”yourdogsbirthday”
#
# NAS or Nest e-mail from e-mail address
FROM=”NAS@you.com”
#
# Install path
#
NPATH=”/share/HDA_DATA/.qpkg/nest”
#
# Change these limits if you like. Remember the NEST allows for some margin, so set
# these limits 5 deg F or more from your lowest/highest NEST Setting
MIN=55
MAX=90
#
#
# Don’t change anything below here unless you know what you’re doing
#
echo “Nest Alert started at $(date)”
#
# Set rarely changed variables
tmp=”$NPATH/tmp.txt”
SUBJECT=”NEST ALERT”
MIMEVersion=”1.0″
CONTENTType=”text/html; charset=us-ascii”;
CTF=$(python $NPATH/nest.py curtemp –user $USERNAME –password $PASSWORD)
CTFR=$(echo $CTF | cut -d”.” -f1)
#
rm $tmp
#
if [[ -z $CTF ]]
then
echo “Alert: NEST has not responded or another error has occured”
log_tool –append ‘NestAlert ERROR’ -t 1
else
echo “Current Temperature is $CTFR”
#
if test $CTFR -gt $MIN
then
echo “OK, NEST tempature above $MIN”
else
echo “Alert: NEST tempature $CTFR is under $MIN”
BODY=”NEST ALERT: Temp is $CTFR”
echo “Sending alert e-mail”
echo -e “To: $EMAIL” > $tmp;
echo -e “From: $FROM” >> $tmp;
echo -e “Content-Type: $CONTENTType”>>$tmp;
echo -e “MIME-Version: $MIMEVersion”>>$tmp;
echo -e “Subject: $BODY”>>$tmp;
echo -e “Body: $BODY”>>$tmp;
touch $tmp && chmod 600 $tmp;
sendmail -t < $tmp
log_tool –append ‘NestAlert sent alert for low temp’ -t 0
fi
#
if test $CTFR -lt $MAX
then
echo “OK, NEST tempature below $MAX”
else
echo “Alert: NEST temperature $CTFR is over $MAX”
BODY=”NEST ALERT: Temp is $CTFR”
echo “Sending alert e-mail”
echo -e “To: $EMAIL” > $tmp;
echo -e “From: $FROM” >> $tmp;
echo -e “Content-Type: $CONTENTType”>>$tmp;
echo -e “MIME-Version: $MIMEVersion”>>$tmp;
echo -e “Subject: $BODY”>>$tmp;
echo -e “Body: $BODY”>>$tmp;
touch $tmp && chmod 600 $tmp;
sendmail -t < $tmp
log_tool –append ‘NestAlert sent alert for high temp’ -t 0
fi
fi
echo “Nest Alert Exiting”

A few caveats:   QNAP uses sendmail and log_tool.   You may need to use or install different programs on another brand, but the syntax should be the same or similar.

To test the script set one of the temperature limits near room temperature (i.e. MIN=75 while room temp is 68).

Now no more contant checking of the mobile NEST app or website during heat waves…