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

**URL:** https://discourse.nodered.org/t/how-to-make-a-counter-incoming-outgoing-on-two-sensors/5839
**Category:** General
**Created:** [15 December 2018 21:51 UTC](https://discourse.nodered.org/t/how-to-make-a-counter-incoming-outgoing-on-two-sensors/5839 "2018-12-15T21:51:06Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Sergii](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@Sergii](https://discourse.nodered.org/u/Sergii)
#### Post date: [15 December 2018 21:51 UTC](https://discourse.nodered.org/t/how-to-make-a-counter-incoming-outgoing-on-two-sensors/5839/1 "2018-12-15T21:51:07Z")

</div>

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**
>
> ```auto
> 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**
>
> ```auto
> 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**
>
> ```auto
> [
> {
> "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"
> ]
> ]
> }
> ]
> 
> ```

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [15 December 2018 22:01 UTC](https://discourse.nodered.org/t/how-to-make-a-counter-incoming-outgoing-on-two-sensors/5839/2 "2018-12-15T22:01:55Z")

</div>

please read this post on how to share your flow

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [16 December 2018 10:44 UTC](https://discourse.nodered.org/t/how-to-make-a-counter-incoming-outgoing-on-two-sensors/5839/3 "2018-12-16T10:44:34Z")

</div>

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](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](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.

---

<div class="post-metadata">

### Author: ![Sergii](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@Sergii](https://discourse.nodered.org/u/Sergii)
#### Post date: [16 December 2018 17:10 UTC](https://discourse.nodered.org/t/how-to-make-a-counter-incoming-outgoing-on-two-sensors/5839/4 "2018-12-16T17:10:19Z")

</div>

> [@ukmoose](#):
>
> Or you can use the `join` node which can join multiple messages together to then allow a function such as yours to work.

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

> [@ukmoose](#):
>
> You can either store one or both the messages in memory

I'll try to figure it out.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [16 December 2018 21:59 UTC](https://discourse.nodered.org/t/how-to-make-a-counter-incoming-outgoing-on-two-sensors/5839/5 "2018-12-16T21:59:44Z")

</div>

> [@Sergii](#):
>
> My code does not work already in the first condition `if (msg.input2 == "1" && !flag1)`

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

```auto
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..

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [17 December 2018 01:00 UTC](https://discourse.nodered.org/t/how-to-make-a-counter-incoming-outgoing-on-two-sensors/5839/6 "2018-12-17T01:00:25Z")

</div>

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