Autor Thema: Send Email based on events  (Gelesen 6884 mal)

Eucalyptus30

  • Gast
Send Email based on events
« am: 05 August 2013, 22:04:32 »
Hi,

I'm the king of the noob. I have an RFXtrx433 USB since few weeks.

With the RFXtrx433 I have 3 temperature sensors. (Inside/Outside/in the fridge :-) )

FHEM run on a Sheevaplug.

Currently the 3 sensors are well detected, I even figure out how to draw the 3 temperatures on a graph (Not that easy when you don't know how FHEM works!)


I'm now stuck on stupid things!
I just want that FHEM send me an email like that:
If Inside > Outside -> Open the window!
If Inside < Outside -> Close the window!
Or if fridge > 4°C -> Fridge Alert!

I think FHEM fit perfectly with my needs, the only problem is... German. I can't find full example in English or French..

THGR132N_1 is the inside sensor.
THGR132N_2 is the outside sensor.
THGR132N_4 is the fridge sensor.

(libmime-lite-html-perl, libnet-smtp-ssl-perl, libemail-mime-encodings-perl, libmime-base64-urlsafe-perl, libauthen-sasl-perl are installed)

Do you have an idea of what the config of fhem.cfg might be?

Thanks and sorry for my bad English


Offline rudolfkoenig

  • Administrator
  • Hero Member
  • *****
  • Beiträge: 25329
Aw: Send Email based on events
« Antwort #1 am: 06 August 2013, 09:03:44 »
Note:
- I did not test this, so take it as a general idea
- I do not know the RFXTRX, so the reading names below may be different, please check before testing.
- there are solutions for this problem already described in the Wiki/Forum, but I don't know them, so probably my solution can be streamlined


How I would do it:

Split the task in two parts, the first concerning the inside/outside temperature, the second the fridge:

1.st part, the inout temperature.

define a dummy which prevents the sending of more than one mail for a problem. You have to reset it manually on the FHEM frontend in order to receive another mail.

define dummy mailsent_inout
attr mailsent_inout setList yes no
attr mailsent_inout webCmd yes:no


Define the notify first in FHEMWEB by entering a minimal one (define ntfy_inout notify X Y) in the command-line, then go to its detail window and click DEF. This way you can avoid the \ at the line-end and the ;; instead of the ;

define ntfy_inout notify THGR132N_1:temperature.*|THGR132N_1:temperature.* {
  my $in  = ReadingsVal("THGR132N_1", "temperature", 15);
  my $out = ReadingsVal("THGR132N_2", "temperature", 15);
  my $txt = "";
  if($in > $out) {
    $txt = "Open the window";
  }
  if($out > $in) {
    $txt = "Close the window";
  }
  if($txt ne "" && Value("mailsent_inout") ne "yes") {
    system "send_me_mail $txt &"
  }
}


send_me_mail is a shellscript, located in one of the directories of the PATH environment variable:

#!/bin/sh

FRM=me@my.email.address
TO="me@my.email.address"
OUT=/tmp/sm.$$
DT=`date`

cat > $OUT << EOF
From: Fhem  <fhem@my.email.address>
To:$TO
Subject: $*

$DT
EOF

/usr/bin/esmtp -f $FRM -N failure,delay -R hdrs -- $TO < $OUT
rm -f $OUT


You have to change the email adresses above to something sensible. The text is kept short, as I am using this method to send me an SMS via an EMAIL to SMS gateway. You can enter multiple email addresses in the TO variable separated by spaces.
I choose esmtp, as it is (in my opinion) easy to configure: You need an entry like the following in your ~/.esmtprc:

identity me@my.email.address
        hostname smtp.provider.com:25
        helo=my.email.address
        username "me@my.email.address"
        password "secret"
        starttls enabled
        default


you probably should install esmpt first, test this last shell-script first from the UNIX command line, and proceed then with the notify above.

The second part (the fridge) is left as an execrcise to the reader :)

Offline fiedel

  • Hero Member
  • *****
  • Beiträge: 1876
Aw: Send Email based on events
« Antwort #2 am: 06 August 2013, 09:05:16 »
Hi and welcome Mister King! ;o)

So let´s try it step by step:
At first i would suggest you to setup SendEmail. For your Sheva Plug the best way is to do it the same way as shown for Raspberry Pi. If finished, please try to send yourself an email from the FHEM- Input- Line like this:

{DebianMail('email@email.domain','Subject','Text')}

If this works fine, we are ready for the next step... ;o)
If you upload your actual FHEM.CFG here, i try to give you an example how to compare the temperatures and send status- emails.

Bonne chance! ;o)

Frank
FeatureLevel: 6.1 auf Wyse N03D ; Deb. 11 ; Perl: v5.14.2 ; IO: HM-MOD-RPI-PCB + VCCU|CUL 868 V 1.66|LinkUSBi |TEK603
HM: SEC-SCO|SCI-3-FM|LC-SW4-PCB|ES-PMSW1-PL|RC-4-2|SEN-MDIR-O|SEC-WDS-2
CUL: HMS100TF|FS20 S4A-2 ; OWDevice: DS18S20|DS2401|DS2406|DS2423

Eucalyptus30

  • Gast
Aw: Send Email based on events
« Antwort #3 am: 07 August 2013, 21:57:43 »
Thanks rudolfkoenig for your help

I have donne that :

define mailsent_inout dummy
attr mailsent_inout setList yes no
attr mailsent_inout webCmd yes:no

define ntfy_inout notify THGR132N_1:temperature.*|THGR132N_2:temperature.* { my $in  = ReadingsVal("THGR132N_1", "temperature", 15);;  my $out = ReadingsVal("THGR132N_2", "temperature", 15);;  my $txt = "";;  if($in > $out) {    $txt = "Ouvre la fenetre";;  } ;; if($out > $in) {    $txt = "Ferme la Fenetre";;  };;  if($txt ne "" && Value("mailsent_inout") ne "yes") {    system "/usr/bin/php5 /home/nico/SendMail.php 'Info Temperature' '$txt <ul>    <li>Temperature Interieure : $in °C</li>    <li>Temperature Exterieure : $out °C</li></ul>' &" ;; }}


I use PHP instead of Esmtp. (It's easier for me)

Is it possible to use "dummy" and store for example le last type of event if it's in < out or in > out. The idea is to send an email only if it's not the same than the last email sended.

An idea for change a Dummy value from perl ?

Offline fiedel

  • Hero Member
  • *****
  • Beiträge: 1876
Aw: Send Email based on events
« Antwort #4 am: 08 August 2013, 07:25:51 »
Zitat

Is it possible to use "dummy" and store for example le last type of event if it's in < out or in > out. The idea is to send an email only if it's not the same than the last email sended.

An idea for change a Dummy value from perl ?


...
set dummy outin
...
set dummy inout

...notify...

 if (OldValue("dummy") ne (Value("dummy"))
...

"ne" compares for "not equal" ; equal is "eq"
FeatureLevel: 6.1 auf Wyse N03D ; Deb. 11 ; Perl: v5.14.2 ; IO: HM-MOD-RPI-PCB + VCCU|CUL 868 V 1.66|LinkUSBi |TEK603
HM: SEC-SCO|SCI-3-FM|LC-SW4-PCB|ES-PMSW1-PL|RC-4-2|SEN-MDIR-O|SEC-WDS-2
CUL: HMS100TF|FS20 S4A-2 ; OWDevice: DS18S20|DS2401|DS2406|DS2423

Offline rudolfkoenig

  • Administrator
  • Hero Member
  • *****
  • Beiträge: 25329
Aw: Send Email based on events
« Antwort #5 am: 08 August 2013, 08:28:23 »
> The idea is to send an email only if it's not the same than the last email sended.

Sorry, I prepared everything for exactly the same reason, but forgot to set the variable in the code above. The "system" line in the notify should be replaced with:

system "send_me_mail $txt &";
fhem("set mailsent_inout yes");

Eucalyptus30

  • Gast
Aw: Send Email based on events
« Antwort #6 am: 10 August 2013, 11:45:13 »
big thanks for your help !

But i still have few questions :-)

- Is-it possible to write perl in multi-line (in fhem.cfg) because i have an error when i do that ?
- Is-it possible to execute fhem command (like fhem("set mailsent_inout yes"); ) but from the shell or a PHP program (any other program) ?

thanks :-)

Offline rudolfkoenig

  • Administrator
  • Hero Member
  • *****
  • Beiträge: 25329
Aw: Send Email based on events
« Antwort #7 am: 10 August 2013, 12:41:27 »
>  - Is-it possible to write perl in multi-line (in fhem.cfg) because i have an error when i do that ?

Yes, but I do not recommend it.
If you are editing the fhem.cfg directly, then you have to end each line (up to the last) with \
This is not necessary if editing the device definition (DEF) in the FHEMWEB detail view, as written above. I recommend defining a function in a copy of 99_Utils.pm (99_myUtils.pm) and calling this function in the notify/at/etc

>  - Is-it possible to execute fhem command (like fhem("set mailsent_inout yes"); ) but from the shell or a PHP program (any other program) ?

Yes, e.g.
perl fhem.pl fhemhost:fhemport "set mailsent_inout yes"
or
echo "set mailsent_inout yes" | nc fhemhost fhemport

 

decade-submarginal