Gmail php script scene trigger

*** Maybe this is not the right forum, admins, feel free to move it ***

Summary:
I created a php script that lets you trigger a certain scene when a specific word is on the subject of an unreaded email, after procesing the email, it marks ALL emails in gmail as readed, waiting for the next unread message.

What is needed:
Vera
Linux server (you can also use a DD-wrt router or, as I do, a Netgear Readynas) with PHP and cron.
A Zwave door sensor

Long description:
I own a Visonic Powermaster 10 alarm panel, which, as of today, I’ve been unable to make it work with the visonic plugin. I just wanted the plugin to turn off any device not needed when the alarm is on (Not at home) such as Nas storage, home cinema standby, and ensure I didn’t left any lights on.

The good thing is that my Alarm provider sends me an email telling me that the alarm has been armed/disarmed and by whom.

As I said before, the script reads the unread emails in your gmail, and if a certain criteria is matched, it triggers a scene in VERA.

Script code:

#!/usr/bin/php <?php $hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; $username = 'USERNAME@gmail.com'; $password = 'PASSWORD';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ’ . imap_la$

/* grab emails */
$emails = imap_search($inbox,‘UNSEEN’);

/* if emails are returned, cycle through each… */
if($emails) {

/* begin output var */
$output = ‘’;

/* put the newest emails on top */
rsort($emails);

/* for every email… */
foreach($emails as $email_number) {

$overview = imap_fetch_overview($inbox,$email_number,0);

$estado = $overview[0]->subject;
$findme = ‘DESACTIVADO’;
$pos = strpos($estado, $findme);

if ($pos !== false) {
echo “DESACTIVADA”;
} else {
echo “ACTIVADA”;
exec(“wget --spider ‘http://192.168.1.201:49451/data_request?id=lu_action&output_format=xml&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=45’'”);
}

}

}

imap_setflag_full($inbox, ‘1:*’,‘\seen’);

imap_close($inbox, CL_EXPUNGE);
?>

When “ACTIVADA” is on the email subject, it triggers scene 45 on my vera IP 192.168.1.201

Make it executable:

chmod +x script.php

Add it to cron

crontab -e

Add * * * * * /pathtoyourscript/script.php

That will make your script run every minute. If you want it to run every 15 minutes, it would be “15 * * * * /pathtoyourscript/script.php”

Back to vera, you need to tell that scene what to do, in my case, turn off everything not needed when I leave home, and turn on a virtual switch named “Alarm armed”
Make a combination switch, and in the configuration tab add “virtual switch status” → “Alarm armed” is “On” and another “security sensor” → “door” is “Tripped”.
Switch is on when “2” or more watched items are true.

Make another scene to turn on whatever devices you want on when you arrive, and trigger it with the combination switch you just created, “virtual switch status changes” → “swtch turns on”. Remember that in that scene you have to turn off the “Alarm armed” virtual switch to reset the combination switch until you receive the next activation email.

You are done.

Hi,

Thank you so much for sharing this… I have a readynas too, so i will be testing this as soon as I can.

A couple of questions for you if that is ok.

  1. How would the script need to change if I wanted to have the check/trigger to be either i) an email from a particular sender or ii) the message body has a particular word or sentance within it ?

And

  1. do you know how the URL could be dynamically updated based on some information in the mail e.g the subject line of an email includes a code which is the scene number, which in turn is then used as part of the Vera invoke/ exec wget–spider URL that’s eventually used ?