How to make a counter incoming\outgoing on two sensors?

Hi! Immediately apologize for my "google translate"...
System: Raspberry PI 3b, iobroker, Node-RED v0.18.4.
I have two sensors E18-D80NK, they react to the passage of man. Sensor signals are displayed in iobroker and are seen as "0" and "1".
If a person enters the room, the E18-D80NK No. 1 sensor (logical 1) is triggered first, then the E18-D80NK No. 2 (logical 1) is triggered, after which No. 1 changes to state 0, and then No. 2 becomes 0.
If a person leaves the room, sensor # 2 (logical 1) is triggered first, after sensor No. 1 (logical 1), after sensor No. 2 becomes "0", and then sensor No. 1 becomes "0". When there are no people, the sensors are in the "0" state.
Code for arduino looks like this:

for Arduino
bool flag1 = 0;
bool flag2 = 0;
bool state1 = 0;
bool state2 = 0;
int status1 = 0;
int status2 = 0;

void setup() {
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT); 
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  digitalWrite( 12 , LOW );
  digitalWrite( 13 , LOW );
}
void loop() {
  if (StatusDoor == HIGH) {             
    if (!digitalRead(2) && !flag1) {
      delay(5);
      flag1 = 1;
      if (flag2 == 0) {
        state1 = 1;
      }
    }
    if (!digitalRead(3) && !flag2) {
      delay(5);
      flag2 = 1;
      if (flag1 == 0) {
        state2 = 1;
      }
    }
    if (digitalRead(3) && digitalRead(2)) {
      if (!flag1 || !flag2) {
        state1 = 0;
        state2 = 0;
        flag1 = 0;
        flag2 = 0;
      }
      if (flag1 && state2) { 
        digitalWrite( 13 , HIGH );
        delay(pause);
        digitalWrite( 13 , LOW );
        flag1 = 0;
        flag2 = 0;
        state2 = 0;
      }
      if (flag2 && state1) { 
        digitalWrite( 12 , HIGH );
        delay(pause);
        digitalWrite( 12 , LOW );
        flag1 = 0;
        flag2 = 0;
        state1 = 0;
      }
    }
  }
}

I tried to port this code to Node-RED, but it fails.

function block
var flag1 = 0;
var flag2 = 0;
var state1 = 0;
var state2 = 0;
var status1 = 0;
var status2 = 0;

    if (msg.input2 == "1" && !flag1) 
    {
      //delay(5);
      flag1 = 1;
      if (flag2 === 0) 
      {
        state1 = 1;
      }
    }
    
    if (msg.input3 == "1" && !flag2) 
    {
      //delay(5);
      flag2 = 1;
      if (flag1 === 0) 
      {
        state2 = 1;
      }
      
    }
    
    if (msg.input2 == "1" && msg.input3 == "1") 
    {
      if (!flag1 || !flag2) 
      {
        state1 = 0;
        state2 = 0;
        flag1 = 0;
        flag2 = 0;
      }
    }  
      
    
      if (flag1 && state2) //Зашли
      { 
        msg.payload = 'input';
        flag1 = 0;
        flag2 = 0;
        state2 = 0;
      }
      if (flag2 && state1) //Вышли
      { 
        msg.payload = 'output';
        flag1 = 0;
        flag2 = 0;
        state1 = 0;
      }
 return msg;

My code does not work already in the first condition if (msg.input2 == "1" && !flag1)

Project
[
    {
        "id": "3172c720.c26258",
        "type": "change",
        "z": "c7702a6.9e748d8",
        "name": "",
        "rules": [
            {
                "t": "move",
                "p": "payload",
                "pt": "msg",
                "to": "input2",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 350,
        "y": 340,
        "wires": [
            [
                "cba91de1.77d11"
            ]
        ]
    },
    {
        "id": "29dcf680.08a0ea",
        "type": "debug",
        "z": "c7702a6.9e748d8",
        "name": "Mess",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "x": 690,
        "y": 360,
        "wires": []
    },
    {
        "id": "5fc34179.bd771",
        "type": "change",
        "z": "c7702a6.9e748d8",
        "name": "",
        "rules": [
            {
                "t": "move",
                "p": "payload",
                "pt": "msg",
                "to": "input3",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 350,
        "y": 380,
        "wires": [
            [
                "cba91de1.77d11"
            ]
        ]
    },
    {
        "id": "9172f332.9b303",
        "type": "inject",
        "z": "c7702a6.9e748d8",
        "name": "",
        "topic": "",
        "payload": "true",
        "payloadType": "bool",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 110,
        "y": 300,
        "wires": [
            [
                "3172c720.c26258"
            ]
        ]
    },
    {
        "id": "f73f4207.3eee2",
        "type": "inject",
        "z": "c7702a6.9e748d8",
        "name": "",
        "topic": "",
        "payload": "true",
        "payloadType": "bool",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 110,
        "y": 420,
        "wires": [
            [
                "5fc34179.bd771"
            ]
        ]
    },
    {
        "id": "9a8458da.2fcb88",
        "type": "inject",
        "z": "c7702a6.9e748d8",
        "name": "",
        "topic": "",
        "payload": "false",
        "payloadType": "bool",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 110,
        "y": 260,
        "wires": [
            [
                "3172c720.c26258"
            ]
        ]
    },
    {
        "id": "f21d7bdf.72f248",
        "type": "inject",
        "z": "c7702a6.9e748d8",
        "name": "",
        "topic": "",
        "payload": "false",
        "payloadType": "bool",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 110,
        "y": 460,
        "wires": [
            [
                "5fc34179.bd771"
            ]
        ]
    },
    {
        "id": "a0b4c896.2f6578",
        "type": "inject",
        "z": "c7702a6.9e748d8",
        "name": "",
        "topic": "",
        "payload": "true",
        "payloadType": "bool",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 110,
        "y": 360,
        "wires": [
            [
                "3172c720.c26258",
                "5fc34179.bd771"
            ]
        ]
    },
    {
        "id": "cba91de1.77d11",
        "type": "function",
        "z": "c7702a6.9e748d8",
        "name": "",
        "func": "var flag1 = 0;\nvar flag2 = 0;\nvar state1 = 0;\nvar state2 = 0;\nvar status1 = 0;\nvar status2 = 0;\n\n    if (msg.input2 == \"1\" && !flag1) \n    {\n      //delay(5);\n      flag1 = 1;\n      if (flag2 === 0) \n      {\n        state1 = 1;\n      }\n    }\n    \n    if (msg.input3 == \"1\" && !flag2) \n    {\n      //delay(5);\n      flag2 = 1;\n      if (flag1 === 0) \n      {\n        state2 = 1;\n      }\n      \n    }\n    \n    if (msg.input2 == \"1\" && msg.input3 == \"1\") \n    {\n      if (!flag1 || !flag2) \n      {\n        state1 = 0;\n        state2 = 0;\n        flag1 = 0;\n        flag2 = 0;\n      }\n    }  \n      \n    \n      if (flag1 && state2) //Зашли\n      { \n        msg.payload = 'input';\n        flag1 = 0;\n        flag2 = 0;\n        state2 = 0;\n      }\n      if (flag2 && state1) //Вышли\n      { \n        msg.payload = 'output';\n        flag1 = 0;\n        flag2 = 0;\n        state1 = 0;\n      }\n return msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 530,
        "y": 360,
        "wires": [
            [
                "29dcf680.08a0ea"
            ]
        ]
    }
]

please read this post on how to share your flow

I'd start by adding some debug nodes to your flow to watch messages.

In the same way you can do it within your function by using node.warn("Something happened you should know about");
https://nodered.org/docs/writing-functions#logging-events

One thing that you will find is that nodes in Node-RED generally have no memory of previous messages. They get a message do their thing and pass it on. So your flow where you expect the function to do something based on the contents of two messages isn't going to work.

The good news is that there is ways around this.
You can either store one or both the messages in memory (see https://nodered.org/docs/writing-functions#storing-data) so when a message comes in it uses the message and grabs the other data from the memory store.
Or you can use the join node which can join multiple messages together to then allow a function such as yours to work.

Have a play, if you get stuck again come back to this thread and post where you have got to.

But I do not need to combine messages, I need to work with each message simultaneously and individually.

I'll try to figure it out.

You can use node.warn() to help to debug code, so before that line you might put

node.warn("input2: "  + msg.input2 + "  flag1: " + flag1);

and a message will appear in the debug pane when that line is executed. Then you can see where it is going wrong..

You should also check that the precedence of the condition is what you think it is...