Function not working in Blynk_Write for IFTTT web hook garage door project


I am at early stages of creating code for my garage door project using a trigger location logic (through an IFTTT Webhook) and it is all working as i need, except for my last ‘if’ statement. It does not complete this part of the code.

My intention with this last ‘if’ statement is to count (in this case) 1 min when i get back home, before resetting all flags. (The intention of the flags are to separate the location instances such as already home, just got home from being out, just left home ect. which will enable different functions later down the track)

I think i know what is happening but Im not sure and would like some help in understanding and the other topics on the Blink_Write function don’t seem to fully answer my issues. The code will complete if i didn’t have the third ‘if’ statement question, but it won’t sit there long enough to see the time condition, then execute. Its like once the function is called it will run everything inside but can’t independently start running by itself again?

I suppose my questions are:

  1. Are you able to explain what is happening in this Blink_Write function, and why.
  2. Are you able to offer me another solution to run my last “if” statement.

thanks for your time!!

The code below is just the barebones ready to insert useful actions when it is ready. I also have more variables for this code in my global variable section, but i just didn’t show them here.

unsigned long currentMillis = millis();
int WebhookButton = param.asInt(); // Get value as integer

    if (WebhookButton == 1 && NotHomeFlag == 0){
       NotHomeFlag = 1;
       HomeFlag = 0;
       Blynk.notify("You have left home!!");
    if (WebhookButton == 0 && HomeFlag == 0){
       HomeFlag = 1;
       Blynk.notify("You have arrived home!!");
       ArriveHomeMillis = currentMillis;  

    if(WebhookButton == 0 && NotHomeFlag == 1 && TimePassed >=    ArriveHomeResetMillis){
       NotHomeFlag = 0;
       Blynk.notify("Been home for 1 min...");

What BLYNK_WRITE() function?

A BLYNK_WRITE() functions simply gets called each and every time a widget with the corresponding virtual pin
(or Bridge call, or API call) is triggered by a state change. Then anything in the function gets processed as requested.

The value of the state change can be determined via tha***() command…

BLYNK_WRITE(V0) {  Button Widget with value of 0 or 1
  int value = param.asInt(); // Get value as integer
  if (value == 1) {  // Button value  is 1
    // do ON stuff
  } else (  // Button value is 0
    // Do OFF stuff

This isn’t really a programming help desk forum… but…

Not quite sure what you are intending… However, I usually use if... else for nested conditions…

Why do you need both Home and NotHome flags? Either you are at home or you aren’t.
Using one flag will simplify the logic and as @Gunner said, if … else statements make life simpler in many cases.


Yes sorry - Do you have more explanation on the Blynk_write functions and how they work. the one here (and others similar) doesn’t quite explain it.

I know how to use the if, and if else statements, but like it tried to explain above there is something about the Blynk_Write function, functionality that will not allow my 3rd ‘if’ code to run because it won’t sit there and wait for 1 min? For example my 3rd ‘if’ statement would (and does) run if i didn’t have the time condition there. Thanks for your patience and entertaining my questions!

Look up at my edited post for a simple function example.

hi Pete, thanks for your reply.

My reasoning behind this is so i can reference what condition I’m in, and where i have come from. Eg - Now I’m home (home flag 1), and because i have just arrived home i have the NotHomeFlag set at 1. you are making me rethink this now, but still doesn’t help the problem at hand. I do understand how to use the if…else statements, but see my reply to Gunner - there is something happening inside the Blynk_write function that is stopping my 3rd statement.

I still don’t see your Blynk function… nor do I see a proper nested if process… that may be why it is not working.

Nothing to do with Blynk, it’s down to the logic in the if statement.
Where is the TimePassed value that’s used in your 3rd if statement coming from?
Your code snippet is using currentMillis as a variable, but comparing TimePassed in your if statement.

My solution for solving this type of problem is to throw in lots of serial.print statements just before each if statement to see what the current variable values are.


hi @Gunner @PeteKnight ,

I have tried using the else if statement, but it doesn’t make any difference. What seems to be happening is that the code inside my Blynk_Write function operates (after the webhook triggers it to operate) until it gets to my 3rd statement, and then (I think) it can’t continue because 1 min hasn’t passed (TimePassed >= ArriveHomeResetMillis). which makes sense because of this information:

I.e because i couldn’t compete that condition, it thinks there is no more code, and then everything inside my Blynk_write function doesn’t operate until the next time the webhook operates.

Hi @PeteKnight ,

you may be right, however how this particular Blynk function operates is effecting it because i have pulled out all of this code and experimented using other means to prove it works. My real code does have lots of serial prints in it, but i have left it out to declutter the example. See my last comment again to Gunner