# \[Solved\] IFTTT event triggered twice, expected once for once published event

**URL:** <https://community.particle.io/t/solved-ifttt-event-triggered-twice-expected-once-for-once-published-event/15756>\
**Category:** Integrations\
**Created:** [September 20, 2015, 6:29am UTC](https://community.particle.io/t/solved-ifttt-event-triggered-twice-expected-once-for-once-published-event/15756 "2015-09-20T06:29:05Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![mallawa](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mallawa](https://community.particle.io/u/mallawa)\
**Post date:** [April 12, 2016, 5:11pm UTC](https://community.particle.io/t/solved-ifttt-event-triggered-twice-expected-once-for-once-published-event/15756/21 "2016-04-12T17:11:56Z")

</div>

```cpp
int calibrationTime = 30;        

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 35000;  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = D6; //the digital pin connected to the PIR sensor's output
int ledPin = D7;
int testLed = D5;

/////////////////////////////
//SETUP
void setup(){
  //Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(testLed,OUTPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  //Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      //Serial.print(".");
      delay(1000);
      }
    //Serial.println(" done");
    //Serial.println("SENSOR ACTIVE");
    delay(50);
  }

////////////////////////////
//LOOP
void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
       //delay(500);
       
       
       
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         //Serial.println("---");
         //Serial.print("motion detected at ");
         //Serial.print(millis()/1000);
        // Serial.println(" sec"); 
         //delay(50);
          Spark.publish("occupied");
      
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(ledPin, LOW); 
       //digitalWrite(testLed, LOW );
       //the led visualizes the sensors output pin state

       if(takeLowTime){
        lowIn = millis(); //save the time of the transition from high to LOW
        takeLowTime = false; //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           //Serial.print("motion ended at "); //output
           //Serial.print((millis() - pause)/1000);
           //Serial.println(" sec");
           //delay(50);
           digitalWrite(testLed, HIGH);
           Spark.publish("unoccupied"); // Unoccupied 
           }
       }
  }

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [April 12, 2016, 8:52pm UTC](https://community.particle.io/t/solved-ifttt-event-triggered-twice-expected-once-for-once-published-event/15756/22 "2016-04-12T20:52:20Z")

</div>

Does it publish `occupied` and `unoccupied` in one go, or what do you mean with _“publishes twice”_

BTW there is no debounce (with some combinations of your flags) between the HIGH and the LOW branch for `pirPin` since you do read twice, you might get HIGH **_and_** LOW in the same interation of `loop()`.  
Rather use `if(digitalRead(pirPin)) { highBranch } else { lowBranch }`

---

<div class="post-metadata">

**Author:** ![mallawa](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mallawa](https://community.particle.io/u/mallawa)\
**Post date:** [April 12, 2016, 9:34pm UTC](https://community.particle.io/t/solved-ifttt-event-triggered-twice-expected-once-for-once-published-event/15756/23 "2016-04-12T21:34:55Z")

</div>

No. I would get 2 notifications instantly for each "occupied and unoccupied in two different states.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [April 13, 2016, 12:11pm UTC](https://community.particle.io/t/solved-ifttt-event-triggered-twice-expected-once-for-once-published-event/15756/24 "2016-04-13T12:11:54Z")

</div>

I’ll have to try that on my devices then.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [April 14, 2016, 12:45pm UTC](https://community.particle.io/t/solved-ifttt-event-triggered-twice-expected-once-for-once-published-event/15756/25 "2016-04-14T12:45:17Z")

</div>

At the moment I’ve just tested with a button pulling high instead of a PIR sensor, but I can’t reproduce your multi-publish

(basically your own code, just minor tweaks and uncommented the Serial.debug() statements

```cpp
// https://community.particle.io/t/solved-ifttt-event-triggered-twice-expected-once-for-once-published-event/15756/20

int calibrationTime = 2; // was 30

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 5000; // was 35000  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = D6; //the digital pin connected to the PIR sensor's output
int ledPin = D7;
int testLed = D5;

void setup()
{
  Serial.begin(115200);
  pinMode(pirPin, INPUT_PULLDOWN);
  pinMode(ledPin, OUTPUT);
  pinMode(testLed,OUTPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
  for(int i = 0; i < calibrationTime; i++)
  {
    Serial.print(".");
    delay(1000);
  }
  Serial.println(" done");
  Serial.println("SENSOR ACTIVE");
  delay(50);
}

void loop()
{
  if(digitalRead(pirPin))
  {
    digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
    //delay(500);
    if(lockLow)
    {  
      //makes sure we wait for a transition to LOW before any further output is made:
      lockLow = false;            
      Serial.println("---");
      Serial.print("motion detected at ");
      Serial.print(millis()/1000);
      Serial.println(" sec"); 
      //delay(50);
      Particle.publish("occupied");
    }         
    takeLowTime = true;
  }
  else
  {       
    digitalWrite(ledPin, LOW); 
    //digitalWrite(testLed, LOW );
    //the led visualizes the sensors output pin state

    if(takeLowTime)
    {
      lowIn = millis(); //save the time of the transition from high to LOW
      takeLowTime = false; //make sure this is only done at the start of a LOW phase
    }
    //if the sensor is low for more than the given pause, 
    //we assume that no more motion is going to happen
    if(!lockLow && millis() - lowIn > pause)
    {  
      //makes sure this block of code is only executed again after 
      //a new motion sequence has been detected
      lockLow = true;                        
      Serial.print("motion ended at "); //output
      Serial.print((millis() - pause)/1000);
      Serial.println(" sec");
      //delay(50);
      digitalWrite(testLed, HIGH);
      Particle.publish("unoccupied"); // Unoccupied 
    }
  }
}

```

[Previous page](https://community.particle.io/t/solved-ifttt-event-triggered-twice-expected-once-for-once-published-event/15756.md?page=1)
