Page 1 of 1

Low temp alarm by email

Posted: Fri Feb 11, 2011 3:29 am
by bobball
Meteohub on the NSLU2 lets me log in to check the temperature in my Michigan home if I'm out of town. But is it possible to add a low-temp alarm to the low-battery and failure to connect to a weather network (WU in my case)? I have a low-tech way to do something similar: an Oregon sensor in a second floor window, transmitting to my OS display in my neighbor's kitchen window. But an emailed low temp alarm would provide another layer of protection. So would replacing the NSLU2 with an Alix 3D3, which is my next purchase, to guard against power outages. The soldered-in circuit to restart the NSLU2 after power restoration is beyond my soldering skills. I thought I'd submitted this a few days ago, but I couldn't find it.

Re: Low temp alarm by email

Posted: Fri Feb 11, 2011 10:47 am
by rune
I'm using a Windows based application for all my 1-wire devices (http://www.beyondmeasure.se/) that also can read output files from Meteohub.
I've set it up so it send me e-mails & SMS if a sensor (Both weatherstation and 1-wire) is outside specific range. It can also switch on/off devices based on e.g. temperature, inout/output etc.

Re: Low temp alarm by email

Posted: Fri Feb 11, 2011 4:28 pm
by bobball
Doesn't that mean a computer running 24/7? My setup is Meteohub in an NSLU2 wired into the router, now sending warnings (low-battery, etc) to a laptop which is opened for various tasks several times a day. And I guess I should mention that it's a Mac. But it seems to me that the function I'm looking for is one that originates in Meteohub.

Re: Low temp alarm by email

Posted: Fri Feb 11, 2011 4:46 pm
by wfpost
you could write a simple and very small shell script and read the data of your sensor every minute with cron
and with a simple 'if ... then' send an email.
emails are easy to send with shell scripts.

Even html emails are possible.
Further help needed?

Re: Low temp alarm by email

Posted: Fri Feb 11, 2011 6:21 pm
by bobball
I'm afraid so; I've no experience with scripting. I can perform tasks from a command line only if instructions are precise and clear. I've managed to install Meteohub in an NSLU2, but it was many months ago, and I've forgotten how I did it. Likewise, in this Mac, I'm curious about making Weather Display work with Meteohub but never figured out how to introduce Weather Display and clientraw data to each other.

Re: Low temp alarm by email

Posted: Sat Feb 12, 2011 11:20 am
by wfpost
just gave it a try on my meteohub and it works well

Those few lines will do the trick.
Do you know how to open an ssh connection with WinSCP and create a shell script, make it executable and then add an entry with crontab?

Explanation
1. trigger is the value the alert gets fired. Hence, whenever the sensor value is less or equal than trigger the condition is true and an email will be initiated.

2. /data/graphs/emailalert.html is the local file processed and send via email
In my case it is a HTML-file containing the current sensor value.

3. email adresses in red below need to be adjusted with your settings. They should be the same as the already existing emails with meteohubs setup.

#!/bin/bash

sensor=$(wget -q -O - "$@" http://127.0.0.1/meteograph.cgi?text=actual_th0_temp_c | awk 'NR==1 { print $1 }')
trigger=0
true=$(echo "$sensor <= $trigger" | bc)
if [ $true = 1 ]; then
echo -n "date: ">/var/run/meteohub/email9.out; date >>/var/run/meteohub/email9.out; echo "to:receiver@email.com">>/var/run/meteohub/email9.out; echo "from: sender@email.com">>/var/run/meteohub/email9.out; echo "Content-Type: text/html">>/var/run/meteohub/email9.out; /srv/www/cgi-bin/stamp.cgi echo "subject: LOW TEMP ALERT - \%d/\%m/\%y - \%H:\%M">>/var/run/meteohub/email9.out; echo "" >>/var/run/meteohub/email9.out; /srv/www/meteohtml.cgi "/data/graphs/emailalert.html">>/var/run/meteohub/email9.out; cat /var/run/meteohub/email9.out | /opt/sbin/sendmail -C /home/meteohub/esmtprc email@email.com 2>&1 | /usr/bin/logger; rm /var/run/meteohub/email9.out
fi
Complete code to copy >>>

Code: Select all

#!/bin/bash

sensor=$(wget -q -O - "$@" http://127.0.0.1/meteograph.cgi?text=actual_th0_temp_c | awk 'NR==1 { print $1 }')

trigger=0
true=$(echo  "$sensor <= $trigger" | bc)
 if  [ $true = 1 ]; then 
    echo -n "date: ">/var/run/meteohub/email9.out; date >>/var/run/meteohub/email9.out; echo "to: receiver@email.com">>/var/run/meteohub/email9.out; echo "from: meteohub@email.com">>/var/run/meteohub/email9.out; echo "Content-Type: text/html">>/var/run/meteohub/email9.out; /srv/www/cgi-bin/stamp.cgi echo "subject: LOW TEMP ALERT - \%d/\%m/\%y - \%H:\%M">>/var/run/meteohub/email9.out; echo "" >>/var/run/meteohub/email9.out; /srv/www/meteohtml.cgi "/data/graphs/emailalert.html">>/var/run/meteohub/email9.out; cat /var/run/meteohub/email9.out | /opt/sbin/sendmail -C /home/meteohub/esmtprc email@email.com 2>&1 | /usr/bin/logger; rm /var/run/meteohub/email9.out
fi

Re: Low temp alarm by email

Posted: Sat Feb 12, 2011 12:23 pm
by Billy
Well done! :D

one remark

I had to adjust

wget http://127.0.0.1/meteograph.cgi?text=actual_th0_temp_c --output-document=alert.out

to my settings like:

wget http://192.168.YYY.XX/meteograph.cgi?te ... th0_temp_c

regards

Re: Low temp alarm by email

Posted: Sat Feb 12, 2011 1:28 pm
by wfpost
are you sure? :roll:
If parameter --output-document=alert.out is missing
wget does not save the sensor value in a file and the variable $sensor stays empty, because awk can´t read from this file.
wget needs to save a file named alert.out in /tmp because awk then reading from this file the value.
>>>
/tmp/alert.out


BTW: If you´re using GMAIL, you can even receive an SMS alert. Simply create specific subject: . Filter it with the function Gmail is offering and forward the email message to your mobile phone.
In that case you should send the email in plain text. Only delete this part in the shell script
echo "Content-Type: text/html">>/var/run/meteohub/email9.out;

Re: Low temp alarm by email

Posted: Sat Feb 12, 2011 3:06 pm
by Billy
You are right:
I just wanted to remark that I had to change the part
" http://127.0.0.1/ " to " http://192.168.yyy.xx/"
For sure "--output-document=alert.out" is necessary.

Regards

Re: Low temp alarm by email

Posted: Sun May 22, 2011 10:22 pm
by Hathor27
Good work, wfpost :D
wfpost wrote:just gave it a try on my meteohub and it works well

Those few lines will do the trick.
Do you know how to open an ssh connection with WinSCP and create a shell script, make it executable and then add an entry with crontab?

Explanation
1. trigger is the value the alert gets fired. Hence, whenever the sensor value is less or equal than trigger the condition is true and an email will be initiated.

2. /data/graphs/emailalert.html is the local file processed and send via email
In my case it is a HTML-file containing the current sensor value.

3. email adresses in red below need to be adjusted with your settings. They should be the same as the already existing emails with meteohubs setup.

#!/bin/bash

cd /tmp

wget http://127.0.0.1/meteograph.cgi?text=actual_th0_temp_c --output-document=alert.out
sensor=$(awk 'NR==1 { print $1 }' alert.out)

trigger=0
true=$(echo "$sensor <= $trigger" | bc)
if [ $true = 1 ]; then
echo -n "date: ">/var/run/meteohub/email9.out; date >>/var/run/meteohub/email9.out; echo "to:receiver@email.com">>/var/run/meteohub/email9.out; echo "from: sender@email.com">>/var/run/meteohub/email9.out; echo "Content-Type: text/html">>/var/run/meteohub/email9.out; /srv/www/cgi-bin/stamp.cgi echo "subject: LOW TEMP ALERT - \%d/\%m/\%y - \%H:\%M">>/var/run/meteohub/email9.out; echo "" >>/var/run/meteohub/email9.out; /srv/www/meteohtml.cgi "/data/graphs/emailalert.html">>/var/run/meteohub/email9.out; cat /var/run/meteohub/email9.out | /opt/sbin/sendmail -C /home/meteohub/esmtprc email@email.com 2>&1 | /usr/bin/logger; rm /var/run/meteohub/email9.out
fi
Complete code to copy >>>

Code: Select all

#!/bin/bash

cd /tmp

wget http://127.0.0.1/meteograph.cgi?text=actual_th0_temp_c --output-document=alert.out
sensor=$(awk 'NR==1  { print $1 }' alert.out)

trigger=0
true=$(echo  "$sensor <= $trigger" | bc)
 if  [ $true = 1 ]; then 
    echo -n "date: ">/var/run/meteohub/email9.out; date >>/var/run/meteohub/email9.out; echo "to: receiver@email.com">>/var/run/meteohub/email9.out; echo "from: meteohub@email.com">>/var/run/meteohub/email9.out; echo "Content-Type: text/html">>/var/run/meteohub/email9.out; /srv/www/cgi-bin/stamp.cgi echo "subject: LOW TEMP ALERT - \%d/\%m/\%y - \%H:\%M">>/var/run/meteohub/email9.out; echo "" >>/var/run/meteohub/email9.out; /srv/www/meteohtml.cgi "/data/graphs/emailalert.html">>/var/run/meteohub/email9.out; cat /var/run/meteohub/email9.out | /opt/sbin/sendmail -C /home/meteohub/esmtprc email@email.com 2>&1 | /usr/bin/logger; rm /var/run/meteohub/email9.out
fi
I'm not already that used with linux. Could you give me a short explanation on crontab?
And, once added, does it stay permanent after reboot of meteohub?

Thanks in advance

Re: Low temp alarm by email

Posted: Sun May 22, 2011 11:37 pm
by wfpost
yes it stays permanent, but the above script has one disadvantage:
whenever cron starts the shell script e.g. every five minutes and it triggers a temp limit of less than 5°C, it will send the email alert.

But: if five minutes later the temp is still less than 5°C than the alert is sent again, and so on.

Conclusio: If you want an alert getting fired only once when a certain level is reached, then it will not work.

But sleep would help pausing the script for a certain time, to hinder an alert getting send again.

while :
do
# wait 7200 seconds for next read
sleep 7200
done

Re: Low temp alarm by email

Posted: Sun May 22, 2011 11:50 pm
by Hathor27
Great, thanks!

Would you mind telling me briefly the syntax for setting up crontab for this script on the CLI?
Just crontab bla... ?
And how to modify or delete if changes need to be done?

Re: Low temp alarm by email

Posted: Mon May 23, 2011 12:11 am
by wfpost
crontab -e

if you made your changes
CTRL-X
Y(es)

--------------------
Syntax:
http://de.wikipedia.org/wiki/Cron


P.S. Create your own section, but not at the end, because meteohub is cutting all entries behind its section after an update or you press save in the GUI for upload control.