PIR People counter

Hello, I have two PIR sensors that I would like to use to count people coming in and out of door. The sensors are placed one in front of the other. So depending on if PIR sensor on pin 38 is tripped first then PIR sensor on pin 40 is tripped second, it will display a "1" on a dashboard. If the direction entering is reversed the the number will subtract and will move back to "0" on the dashboard. I am having issues with the flow of this and here are the outputs of the PIR sensors. Any ideas? I dont know if it should be a python code that does the work or can a flow handle this?

First PIR:
topic: "pi/38"
payload: 1
_msgid: "10ab8360.997c8d"

Second PIR:
topic: "pi/40"
payload: 1
_msgid: "cd61bb0.a8a0e48"


I am having issues with the flow of this

Post your flow (only the parts that matter).

Note that you will detect false readings, a PIR sensor will detect infrared, if someone turns around and turns back, you will get another reading while it is still 1 person.

What timing is needed? Will both inputs be 1 at the same time?

They sensors will be separate by about 8”-12”.

I was thinking of something like if one trips first void next input. That way I can capture the first input and not have to worry about the second.

Or have a python code that says something to the affect of if “front“ sensor is tripped then “back” is second; then print “increase”

Else if “back” sensor is tripped then “front” is second; then print “decrease”.

I am not python expert but kind of the idea.

This is a very simple use case which assumes the first sensor encountered determines the person has entered or departed. The trigger is set for ignore input for 2 seconds after the first event for either sensor is received.

  1. The blue buttons on the left can contain any payload (data) type. Your sensor trigger.
  2. First change nodes convert sensor trigger payload to string "Front" or "Back" respectively.
  3. Trigger event passes the first string received and disregards any other input for 2 seconds (can configure).
  4. The convert to count change node has two rules. Once assigns value of +1 if "Front" or a value of -1 if "Back".
  5. Calculate total and store is another change node that calculates persons in store and saves total in a flow context (Variable which is accessible within this flow only). This change node is using an expression to calculate. Could use a general function node to do the same thing.
  6. Total in store is yet another change node that retrieves the current in Store total and outputs.

There is an initialize button that resets inStore to 0. This button is set to automatically reset when flow is started (Node-red starts, Flow is deployed). This is needed to ensure the flow context "inStore" has a valid number.

The green nodes are debug nodes used so you can see results at various points in the flow.

[{"id":"32c36e6a.cc10d2","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"1c9783f5.c67d6c","type":"inject","z":"32c36e6a.cc10d2","name":"Front sensor","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":174,"y":72,"wires":[["7f95bce9.47ec34"]]},{"id":"6d024687.407618","type":"inject","z":"32c36e6a.cc10d2","name":"Back sensor","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":174,"y":168,"wires":[["d7db0117.44f22"]]},{"id":"47a13b3f.0b8484","type":"trigger","z":"32c36e6a.cc10d2","op1":"","op2":"","op1type":"pay","op2type":"nul","duration":"2","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":620,"y":128,"wires":[["8ea84cd.4f29ab","ee4017ca.0db978"]]},{"id":"8ea84cd.4f29ab","type":"debug","z":"32c36e6a.cc10d2","name":"which sensor first?","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":874,"y":80,"wires":[]},{"id":"ee4017ca.0db978","type":"change","z":"32c36e6a.cc10d2","name":"convert to count","rules":[{"t":"change","p":"payload","pt":"msg","from":"Front","fromt":"str","to":"1","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"Back","fromt":"str","to":"-1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":864,"y":128,"wires":[["efcc6326.384ca","fdf79205.5ae05"]]},{"id":"efcc6326.384ca","type":"debug","z":"32c36e6a.cc10d2","name":"incrementer / decrementer","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1200,"y":80,"wires":[]},{"id":"7f95bce9.47ec34","type":"change","z":"32c36e6a.cc10d2","name":"set to \"Front\"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Front","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":72,"wires":[["47a13b3f.0b8484"]]},{"id":"d7db0117.44f22","type":"change","z":"32c36e6a.cc10d2","name":"set to \"Back\"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Back","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":358,"y":168,"wires":[["47a13b3f.0b8484"]]},{"id":"e3c748d2.3e9e98","type":"debug","z":"32c36e6a.cc10d2","name":"Total in Store","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1534,"y":128,"wires":[]},{"id":"c8053db1.5c77e","type":"inject","z":"32c36e6a.cc10d2","name":"Initialize","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":156,"y":256,"wires":[["631048a.66f95b8"]]},{"id":"631048a.66f95b8","type":"change","z":"32c36e6a.cc10d2","name":"set inStore = 0","rules":[{"t":"set","p":"inStore","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":376,"y":256,"wires":[[]]},{"id":"fdf79205.5ae05","type":"change","z":"32c36e6a.cc10d2","name":"Calulate total  & store","rules":[{"t":"set","p":"inStore","pt":"flow","to":"$flowContext(\"inStore\") + msg.payload","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1124,"y":128,"wires":[["a8cfb807.90ea58"]]},{"id":"a8cfb807.90ea58","type":"change","z":"32c36e6a.cc10d2","name":"Total in store","rules":[{"t":"set","p":"payload","pt":"msg","to":"inStore","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1350,"y":128,"wires":[["e3c748d2.3e9e98"]]}]

Hello,Thank you for such quick response!
When trying to import what you have, I am getting this error.

SyntaxError: Unexpected token "}" in JSON at position 1010

Its the "}" at the end of wires":.

Do you know why I might be getting this error?

Its because @burtonboucher hasn't correctly posted the flow. In order to make code more readable / importable it is important to post it between two sets of three backticks - ``` - see this post for more details - How to share code or flow json

I'll correct it this time

@Becherer, it should be possible to import now.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.