Combining a timer with a delay

My NodeMCU is connected to the Output of a house alarm. When ths house alarm is activated it pulls D1 high and the nodeMCU then alerts me via Blynk. Works perfectly.

However once or twice a day I'm getting a spurious alert. The nodeMCU D1 pin believes its been triggered. Previously I had a proper GSM module that would do the alarm reporting. That did not suffer from the same spurious alerts.

In an attempt to address this I adjusted my code to incorporate a 4 second delay, to test for the pin condition a second time before alerting. Of course this is nonsense as the timer is firing every second anyway. What can I do to addrss this spurious alerting? Anyone come across a similar problem?

Code:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

BlynkTimer timer;
char auth[] = "x"; //Auth code sent via Email
char ssid[] = "x"; //Wifi name
char pass[] = "x";  //Wifi Password
int flag=0;

void notifyOnFire()
{
  int pinState = digitalRead(D1);
  if (pinState==0 && flag==0) {
    delay(4000);
    int pinStateAgain = digitalRead(D1);
    if (pinStateAgain==0) {
        Serial.println("Alarm has gone off");
        Blynk.notify("House Alarm!!!");
        flag=1;
      }
  }
  else if (pinState==1)
  {
    flag=0;
  }
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(D1,INPUT_PULLUP);
  timer.setInterval(1000L,notifyOnFire); 
}

void loop()
{
  //Serial.println(WiFi.localIP());
  Blynk.run();
  timer.run();
}

First: Edit your post and insert code tags!

Second: Post complete code, your code doesn't compile!

Post links to the used libraries!

Could this be an issue of a "floating" digital pin?

I have just started a topic about this subject because I have the same problem (trying to trigger interrupt using a voltage pulse).
https://forum.arduino.cc/index.php?topic=699832.0

And as pylon said above please post your complete code with code tags!

Kind regards,

Zeb

pylon:
First: Edit your post and insert code tags!

Second: Post complete code, your code doesn't compile!

Post links to the used libraries!

Posted this orginally from my mobile. Could not find code tag button but should've guessed it. Anyway fixed now. Full code too.

Zeb, looking into your theory of a floating digital pin, someone has advised:

"You can avoid the problem by adding a large value resistor between the pin and GND."

Source: floating pin - Arduino digitalRead reading wrong - Electrical Engineering Stack Exchange

Something else that explains the theory (wow): https://www.mouser.com/blog/dont-leave-your-pins-floating

That is a generic solution to a generic problem. Typically you should use internal pull up resistors which can be enabled in software, with switches. But when a pin is connected to a sensor or other circuit, the input is typically not "floating" because the driving circuit has a low impedance.

So in order to foreshorten wild speculations, please post a wiring diagram and also provide some electrical information about the alarm system output that you are monitoring.