How To: Setup Email Alerts on BrewPi

Homebrew Talk - Beer, Wine, Mead, & Cider Brewing Discussion Forum

Help Support Homebrew Talk - Beer, Wine, Mead, & Cider Brewing Discussion Forum:

This site may earn a commission from merchant affiliate links, including eBay, Amazon, and others.

LostPhoenix

Member
Joined
May 20, 2014
Messages
10
Reaction score
4
Hello everyone,

I recently setup a BrewPi using FuzzyWuzzy's post, and have been pretty happy with it. However, I was worried about if one of the relays failed. So I wrote a simple bash script to email me if the fermentation temp gets too high far the set point. I thought some of you might be interested in it.

First thing we need to do is setup the rPi to send emails. I decided to use SMTP Mail.

First connect to your rPi via SSH.

Loading Packages
Code:
sudo apt-get install ssmtp 
sudo apt-get install mailutils
sudo apt-get install mpack

This will take a few minutes. Answer Y to any prompts.

Setting up Default SMTP Settings
Code:
sudo nano /etc/ssmtp/ssmtp.conf

Make the following changes to the fields. If you can't find one of the fields add it to the bottom. I use GMail for this. If you don't use gmail you may need a different STMP address.
Code:
[email protected]
AuthPass=userpass
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES

When you are finished, press control+o to save. Then control+x to close nano.

Let's test to make sure this is setup properly
In the terminal type paste this
Code:
echo "sample text" | mail -s "Subject" [email protected]

This should take a few seconds to execute, but you should get the email pretty much instantly if you're sending to a gmail account.

Great! Email Works, let's setup alerts
Let's use nano again to setup the Bash Script we're going to use.
Code:
nano /home/pi/brewCheck.sh

Now we will paste the contents of my bash script into nano.
Code:
#!/bin/bash

#Config
[email protected] #Set to your email address
AlertAt=3.5 #Threshold for alert. Difference in degree of wort from set temp before alert

function jsonValue() {
KEY=$1
num=$2
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/\042'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}

curl --data "messageType=refreshControlVariables&message=" http://127.0.0.1/socketmessage.php
Diff=$(curl --data "messageType=getControlVariables" http://127.0.0.1/socketmessage.php | jsonValue beerDiff 1)

if [[ $Diff > $AlertAt ]]; then
  echo "BrewPi Needs Help;  BeerDiff is $Diff" | mail -s "BrewPi Needs Help" $YourEmail
fi

Once again press control+O to save, and control+X to close.

Almost done! Now we just need make the script executable and setup the cronjob!

Don't forget to make the script executable!
Code:
chmod +x brewCheck.sh

Lastly, Setup the cronjob!
I have my script checking every 15 minutes. If you want to have yours check more frequently just adjust the */15 to */Number_of_Minutes_between_checks

First let's open crontab
Code:
crontab -e

Scroll down to the very bottom and add this line
Code:
*/15 * * * * /home/pi/brewCheck.sh >/dev/null 2>&1

Press control+O to save, and control+X to close.

crontab should report
Code:
crontab: installing new crontab

Congrats! You will now receive email alerts if your wort gets beyond the temp threshold!

If there are any other circumstances you might want an alter for please let me know. I am new to the fermentation chamber game, and would be happy to update the script.
 
this is great, i had tried to follow other automated email process and couldn't get any to work, this seems to do the trick perfectly!

Just a quick issue i found, if using gmail and you have app passwords activated, the password you use is the one that is generated and not your normal login (that had me confused for a bit as kept getting this error.

"send-mail: Authorization failed (535 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 j10sm6152775wjn.23 - gsmtp)"

In terms of notifications that following would be great for me.

- brewpi profile has started - sometimes i put my beer in the fridge and then have the brewpi kick in at midnight, theres no reason really other than weird OCD
- Notification when beer temp is set to change
- Notification when a fermentation profile has ended?

I'm not sure if any of these are possible to add in though?
 
this is great, i had tried to follow other automated email process and couldn't get any to work, this seems to do the trick perfectly!

Just a quick issue i found, if using gmail and you have app passwords activated, the password you use is the one that is generated and not your normal login (that had me confused for a bit as kept getting this error.

"send-mail: Authorization failed (535 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 j10sm6152775wjn.23 - gsmtp)"

In terms of notifications that following would be great for me.

- brewpi profile has started - sometimes i put my beer in the fridge and then have the brewpi kick in at midnight, theres no reason really other than weird OCD
- Notification when beer temp is set to change
- Notification when a fermentation profile has ended?

I'm not sure if any of these are possible to add in though?

Thanks for the heads up with app specific passwords. I didn't think to include that info.

I will explore what info I can get from the brewpi from the limited APIs for the other alerts. :)
 
LostPhoenix, i just got this working and agree with Mikmonken that it would helpful to get a text message when
-- start of profile, end of profile
-- major temp change in profile ( change of X degrees)
-- Alert of temp prob not reporting for beer temps

if you have any of these working can you please post.

Thanks,
Ptown's brewing
 
so.. it looks like this might have been write before the latest version as i would get the same value for the alert.. if you are going to add this to your Rpi use this code for the brewCheck.sh file...

#!/bin/bash

#Config
[email protected] #Set to your email address
AlertAt=3.5 #Threshold for alert. Difference in degree of wort from set temp before alert

function jsonValue() {
KEY=$1
num=$2
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/\042'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}

wget localhost/socketmessage.php --post-data="messageType=getControlVariables" -q -O -
Diff=$(wget localhost/socketmessage.php --post-data="messageType=getControlVariables" -q -O - | jsonValue beerDiff 1)

if [[ $Diff > $AlertAt ]]; then
echo "BrewPi Needs Help; BeerDiff is $Diff" | mail -s "BrewPi Needs Help" $YourEmail
fi
 
And... now thinking about the alerts to email. it would be nice to see if the Cooling or Heating has been on for more then X hours. this would be handy to see if something is not working well/right. will see if can get this going in the same file.. will update this thread if i get it working.

ParadiseBeer
 
so.. it looks like this might have been write before the latest version as i would get the same value for the alert.. if you are going to add this to your Rpi use this code for the brewCheck.sh file...

fi

This is true: I forgot to come back and update the post with the new code. They changed the socketmessage variable request. I will update the main post this weekend.
 
LostPhoenix, i just got this working and agree with Mikmonken that it would helpful to get a text message when
-- start of profile, end of profile
-- major temp change in profile ( change of X degrees)
-- Alert of temp prob not reporting for beer temps

if you have any of these working can you please post.

Thanks,
Ptown's brewing

There isn't much of an API on the official brewpi distro. This means there is a lot of overhead and data storage involved in keeping track of most of those things. I think the easiest way to do it would be to read the brewpi database and look for specific ranges between data points. I will look into it when I have some free time!
 
There isn't much of an API on the official brewpi distro. This means there is a lot of overhead and data storage involved in keeping track of most of those things. I think the easiest way to do it would be to read the brewpi database and look for specific ranges between data points. I will look into it when I have some free time!

LostPhoenix: does this script still work?

I have checked that the email is working, which it is, and have completed the rest of the instructions... is there any way to test if it works without messing with what is currently fermenting?
 
I modified LostPhoenix's script to send notifications using Pushover instead of email and included some extra info. I also made an upstart script instead of using crontab since I have my brewpi on my ubuntu server that I use for other things and didn't want to clutter my syslog too much. Not sure how useful this will be for everyone but I figure why not.

Code:
#!/bin/bash

#Config
userkey=YourUserKey #Pushover Userkey
apikey=YourAPIKey #Pushover API Key
AlertAt=3.5 #Threshold for alert. Difference in degree of wort from set temp before alert
interval=$((15*60)) #Check every x seconds

function jsonValue() {
Diff=$(echo $8 | sed 's/^\(.*\),$/\1/')
Diff=$(python -c "print $float($Diff) * -1")
AbsDiff=$(echo $8 | sed 's/^-\(.*\),$/\1/')
}

while :
do
  curl -k --data "messageType=refreshControlVariables&message=" https://127.0.0.1/brewpi/socketmessage.php
  json=$(curl -k --data "messageType=getControlVariables" https://127.0.0.1/brewpi/socketmessage.php)
  jsonValue $json beerDiff 1

  beerSet=$(curl -k --data "messageType=getBeer" https://127.0.0.1/brewpi/socketmessage.php)
  curBeer=$(python -c "print float($Diff) + float($beerSet)")

  if [[ $AbsDiff > $AlertAt ]]; then
    curl -s \
    --form-string "token=$apikey" \
    --form-string "user=$userkey" \
    --form-string "title=WARNING" \
    --form-string "message=Beertemp is currently $AbsDiff°F off which is greater than the maximum deviance allowed of $AlertAt°F. Please take measures to ensure everything is ok. Current Temp: $curBeer. Set Temp: $beerSet" \
    --form-string "priority=2" \
    --form-string "retry=30" \
    --form-string "expire=3600" \
    https://api.pushover.net/1/messages.json
  fi
  sleep $interval
done

And here's the upstart conf file:
Code:
# brewpi

description "brewpi"
author "brewpi"

start on filesystem and static-network-up
stop on runlevel [016]

respawn
respawn limit 5 30

env script=/home/brewpi/./brewpi_check.sh #location of script

exec start-stop-daemon -S -x $script

It would be awesome if brewpi had some sort of api but as it is now it would require a decent amount of modifying the brewpi script to get much more functionality from it.
 
Is there a way to get to this to check every 15 minutes but not email me every minutes when there's a problem. Had to turn my phone off at work the other day lol
 
I modified LostPhoenix's script to send notifications using Pushover instead of email and included some extra info. I also made an upstart script instead of using crontab since I have my brewpi on my ubuntu server that I use for other things and didn't want to clutter my syslog too much. Not sure how useful this will be for everyone but I figure why not.

Code:
#!/bin/bash

#Config
userkey=YourUserKey #Pushover Userkey
apikey=YourAPIKey #Pushover API Key
AlertAt=3.5 #Threshold for alert. Difference in degree of wort from set temp before alert
interval=$((15*60)) #Check every x seconds

function jsonValue() {
Diff=$(echo $8 | sed 's/^\(.*\),$/\1/')
Diff=$(python -c "print $float($Diff) * -1")
AbsDiff=$(echo $8 | sed 's/^-\(.*\),$/\1/')
}

while :
do
  curl -k --data "messageType=refreshControlVariables&message=" https://127.0.0.1/brewpi/socketmessage.php
  json=$(curl -k --data "messageType=getControlVariables" https://127.0.0.1/brewpi/socketmessage.php)
  jsonValue $json beerDiff 1

  beerSet=$(curl -k --data "messageType=getBeer" https://127.0.0.1/brewpi/socketmessage.php)
  curBeer=$(python -c "print float($Diff) + float($beerSet)")

  if [[ $AbsDiff > $AlertAt ]]; then
    curl -s \
    --form-string "token=$apikey" \
    --form-string "user=$userkey" \
    --form-string "title=WARNING" \
    --form-string "message=Beertemp is currently $AbsDiff°F off which is greater than the maximum deviance allowed of $AlertAt°F. Please take measures to ensure everything is ok. Current Temp: $curBeer. Set Temp: $beerSet" \
    --form-string "priority=2" \
    --form-string "retry=30" \
    --form-string "expire=3600" \
    https://api.pushover.net/1/messages.json
  fi
  sleep $interval
done

And here's the upstart conf file:
Code:
# brewpi

description "brewpi"
author "brewpi"

start on filesystem and static-network-up
stop on runlevel [016]

respawn
respawn limit 5 30

env script=/home/brewpi/./brewpi_check.sh #location of script

exec start-stop-daemon -S -x $script

It would be awesome if brewpi had some sort of api but as it is now it would require a decent amount of modifying the brewpi script to get much more functionality from it.

Hey this works great !!!! is there an option to add notification when the "open door" is enebled?
 
This looks great. I am new to programming an have some basic questions.

I assume that I need to cut and paste this latest Pushover script into Nano in the same way that the old email one was created. Is there a need to run the initial email setup using the sudo apt-get install ssmtp, mailutils and mpack commands?

Also where does the upstart conf file go and how is it created?

Thanks,

Paul
 
Back
Top