How do I get an incoming email(gmail) to trigger an action?


#1

1) Give a description of the problem
(PUT YOUR INFO HERE)

2) What is the expected behaviour?
(PUT YOUR INFO HERE)

3) What is happening/not happening?
(PUT YOUR INFO HERE)

4) Post a Green Snapshot of the pistonimage
(UPLOAD YOUR IMAGE HERE)

5) Attach logs after turning logging level to Full
(PASTE YOUR LOGS HERE THEN HIGHLIGHT ALL OF THE LOGS AND CLICK ON THE </> ICON TO FORMAT THEM CORRECTLY)

REMOVE BELOW AFTER READING
If a solution is found for your question then please mark the post as the solution.


#2

I want an action when a specific email arrives.


#3

The IFThisThenThat service offers that.

  • Gmail is received
  • IFTTT sees it, and takes action
    (actions can be in webCoRE or elsewhere)

(Note: the Gmail trigger may not be instant, but can be acceptable for certain applications)


#4

IFTTT does not support that trigger anymore.


#5

My apologies. I have not used that trigger for awhile.

I use Tasker on my Android to achieve this.


#6

You can recreate IFTTT functionality using Google Apps Script. The method below includes sending a command to webCoRE via $args, so that you can use one script for multiple commands.

  1. Import this example piston:

  1. Log into the Gmail account you want to receive emails at, go to settings and create a filter that matches your needs, i.e.:

  1. Set the filter to apply the label ‘Need To Process’

  1. Go to https://script.google.com/ and click ‘New Project’

  2. Delete the empty ‘my function’ lines, clearing the script editor to blank.

  3. Paste in the following code and enter the external URL for the piston created above, leaving the quotation marks untouched:

function processEmails() {
  var label = GmailApp.getUserLabelByName("Need To Process");
  var Processed = GmailApp.getUserLabelByName("Processed");
  var threads = label.getThreads();
  if (threads ==""){return};
  var message = threads[0].getMessages()[0];
  for (var i = threads.length - 1; i >= 0; i--) {
    var command = message.getPlainBody().slice(0, -2);
    var url = "ENTER YOUR PISTON EXTERNAL URL HERE"+"?command="+command;
    var options = {
      "method": "post",
      "followRedirects" : false,
      "muteHttpExceptions": true
    };
    UrlFetchApp.fetch(url, options);
    threads[i].removeLabel(label).refresh();
    threads[i].addLabel(Processed);
  }
}
  1. Save and click the image button. You will be asked to authorise the script the first time you do this.

  2. Click on the image button, then click ‘add trigger’.

  3. Enter the details as follows and save, most will be pre-populated for you:

That’s it, now if you receive an email that matches your filter, with ‘light1on’ or ‘light1off’ in the body, your piston will respond accordingly. You can add more command cases to the piston as desired i.e. ‘light2on’.

The ‘Need To Process’ label is replaced with ‘Processed’ once the script has run successfully.

Can take up to 1 minute to trigger, as 1 minute is the shortest time driven trigger allowed in Google Script.

Enjoy.


Email as a Trigger
#7

Hello Robin,

Thanks for the detailed instructions! I’m learning as I go so I’m going to give your solution a try. Would you mind if I came back with questions?

Thanks
John


#8

sure


#9

Hello Robin,

Let me start with, I’m a new to this…

Now to where I am at. I successfully setup the Google Scripts and Webcore according to the instructions (at least I believe so)

What i can prove out:

The email script runs at g scripts, Need to process -> processed when an email notification arrives…

In webcore my ST hub and virtual sensor are listed.

I’m trying to break this down for troubleshooting purposes.

Questions,
How can I prove that the ‘connection’ between the the gscripts and webcore is working? Is there a way to test and confirm a connection? Is there a trigger i can execute to activate the webcore piston?

and

Is there a way can I test that the webcore to smarthings integration is working properly? Is there a ‘trigger’ I can initiate to confirm the integration between webcore & ST?

My thought is to break this out into segments then test from end-to end.

Hope that’s clear :slight_smile:

Look forward to your reply.

Thanks
John


#10

If the piston has been successfully triggered by the G-Script, you will see the ‘quick facts’ section of your piston update:

image

Send yourself an email meeting the set filter criteria, or tag an email as ‘Need to Process’. That said it not really a ‘connection’… Google just makes a basic web request to your piston URL.

Just use the external piston URL in a browser.

There isn’t really such a thing as webcore<>ST integration, pistons are saved and run on the ST cloud, thus no integration required. The dashboard is the only integration needed, and that’s only to edit pistons.

You can make a test piston with a single action, no conditions, and use the ‘test’ button.

Ultimately, if an incoming email is triggering a piston and giving you the correct result, the chain of events is working and no further breakdown is required.


#11

Hello Robin,

Thank for your explanation! I’m getting close…

Ive been able to follow your recommendations above with some success.

  1. I can confirm that the “Last Executed” time changes in webcore accordingly when an email is received that meets the filter criteria in gmail.

That tells me that the gscripts and webcore piece is working properly. Yet the virtual sensor in ST does not seem to change status.

I’m going to try creating the test piston you suggested. Will keep you posted.

Thanks - John


#12

Post your piston with the virtual sensor command, might be something obvious.


#13

Here you go

//
/*Virtual Sensor 2 */
/
/
/* Author : RW /
/
Created : 9/14/2020, 9:49:29 AM /
/
Modified : 9/14/2020, 9:50:57 AM /
/
Build : 1 // UI version : v0.3.110.20191009 /
/**************************************************************/
define
string lastCommandReceived; /
null */
end define;
execute

switch ({$args.command})
case ‘light1on’:
with
Gove Sensors
do

Turn on;

end with;

case ‘light1off’:

with
Gove Sensors
do

Turn off;

end with;

end switch;

Set variable {lastCommandReceived} = {$args.command};

end execute;


#14

Please use the green snapshot button when sharing pistons.

Looks like webcore is being activated but not receiving a command to respond to, i.e. ‘light1on’ or ‘light1off’.

Make sure you are including the appropriate command in the body of your trigger emails.


#15

Hey Robin-
Truly appreciate you working with me on this! I got it to work! I can now activate the ‘non-smart’ sensor which sends an email and activates a virtual switch within the ST hub! Do you have any guidance on what i need to do to backup or save the gcripts or webcore piston? Would hate to lose what i have now. :slight_smile:
Thanks again,
John


#16

For the Google side of things, it’s all in the cloud so no need to backup.

webCoRE has a ‘backup pistons’ option on the dashboard which you should be using on a regular basis.


#17

Thanks much Robin! I’ll be sure to check in from time to time and will definitely reach back if I get stuck on my next automation.

Have a great weekend!
John