## This article explains counting how many people entered and exited the area with several entrances/exits

### 1. Introduction

People counter devices that we use have two units. One is transmitter and the second is receiver. When somebody passes between these two units, it will increase Total Counter A & B values by the number of people that entered or exited the monitored area.

**Tip**: if you want to rectify negative values from counting people, please read the second part of this article.

Below, we instruct you how to implement the calculation for how many people are inside the monitored area (e.g. meeting room, breakfast area, lobby, etc.), simply by detracting the Total Counter A from Total Counter B.

But, it's not always very simple to calculate people, especially in the case with more doors, which can both be entrances and exits. Hence, we give you the two possible solutions that you can implement for counting People:

- Calculating the values directly from each People counter device (Total Counter A & B), so we don't have to use the intermediate „Currently IN“ calculation.
- This option is a bit faster to implement, but it doesn't show the Currently IN calculation for each device.

- Including „Currently IN“ virtual asset, where each device has its own calculation
- Once finished, there is a unique People Counter device in ALSO IoT Platform, which shows only the calculations for the monitored area.

**Note**: If you want to reset the Total Counter A & B every day, please reach out to our support so we can enable it for you.

### 2. Calculation without „Currently IN“ asset

**Note**: before implementing the code below, you have to create a NEW device in the IoT platform, to be able to present the results of counting people the easiest way possible.

To do that:

- Go to your Ground and click on the + NEW DEVICE button
- Name your device as you wish (e.g. People Counting)
- In this newly created device, create the Virtual Asset to present the result of calculations
- E.g. people_counter_meeting_room_a

- Now, go to the code below and apply the related Device ID to each device
- You can find the Device ID in Settings (upper right) > Authentication > Device ID

#### 2.1. Two people counters

device1 = allthingstalk.device.YOUR_DEVICE1_ID

device2 = allthingstalk.device.YOUR_DEVICE2_ID

deviceIN = allthingstalk.device.YOUR_DEVICEIN_ID

math1 = allthingstalk.math.basic.op

math2 = allthingstalk.math.basic.op

math3 = allthingstalk.math.basic.op

#Calculation for the 1st device

device1.Total_Counter_A -> math1.a

device1.Total_Counter_B -> math1.b

math1.a-b -> math3.a

#Calculation for the 2nd device

device2.Total_Counter_A -> math2.a

device2.Total_Counter_B -> math2.b

math2.a-b -> math3.b

#Final calculation for TWO devices

math3.a+b -> deviceIN.total_in_DEFINE_YOUR_AREA

#### 2.2 Calculation for 3 people counters

device1 = allthingstalk.device.YOUR_DEVICE1_ID

device2 = allthingstalk.device.YOUR_DEVICE2_ID

device3 = allthingstalk.device.YOUR_DEVICE3_ID

deviceIN = allthingstalk.device.YOUR_DEVICEIN_ID

math1 = allthingstalk.math.basic.op

math2 = allthingstalk.math.basic.op

math3 = allthingstalk.math.basic.op

math4 = allthingstalk.math.basic.op

#Calculation for the 1st device

device1.Total_Counter_A -> math1.a

device1.Total_Counter_B -> math1.b

math1.a-b -> math4.a

#Calculation for the 2nd device

device2.Total_Counter_A -> math2.a

device2.Total_Counter_B -> math2.b

math2.a-b -> math4.b

#Intermediate calculation for first two devices

math4.a+b -> math5.a

#Calculation for the 3rd device

Device3.Total_Counter_A -> math3.a

Device3.Total_Counter_B -> math3.b

Math3.a-b -> math5.b

#Final calculation for THREE devices

Math5.a+b -> deviceIN.total_in_DEFINE_YOUR_AREA

#### 2.3 Calculation for 4 people counters

device1 = allthingstalk.device.YOUR_DEVICE1_ID

device2 = allthingstalk.device.YOUR_DEVICE2_ID

device3 = allthingstalk.device.YOUR_DEVICE3_ID

device4 = allthingstalk.device.YOUR_DEVICE4_ID

deviceIN = allthingstalk.device.YOUR_DEVICEIN_ID

math1 = allthingstalk.math.basic.op

math2 = allthingstalk.math.basic.op

math3 = allthingstalk.math.basic.op

math4 = allthingstalk.math.basic.op

math5 = allthingstalk.math.basic.op

math6 = allthingstalk.math.basic.op

math7 = allthingstalk.math.basic.op

#Calculation for the 1st device

device1.Total_Counter_A -> math1.a

device1.Total_Counter_B -> math1.b

math1.a-b -> math5.a

#Calculation for the 2nd device

device2.Total_Counter_A -> math2.a

device2.Total_Counter_B -> math2.b

math2.a-b -> math5.b

#Intermediate calculation for first and second device

math5.a+b -> math7.a

#Calculation for the 3rd device

Device3.Total_Counter_A -> math3.a

Device3.Total_Counter_B -> math3.b

math3.a-b -> math6.a

#Calculation for the 4th device

Device4.Total_Counter_A -> math4.a

Device4.Total_Counter_B -> math4.b

math4.a-b -> math6.b

#Intermediate calculation for third and fourth device

Math6.a+b -> math7.b

#Final calculation for THREE devices

Math6.a+b -> deviceIN.total_in_DEFINE_YOUR_AREA

### 3. Calculation with „Currently IN“ asset

**If you want that every People counter has a calculation of the difference between people going IN and OUT**, follow these steps:

- Go to your People Counter device
- Create a
*Virtual*asset (type: integer). Name it „*currently_in*“ - Now, go to the
*Rules*(left sidebar) and hold SHIFT on your keyboard while pressing*+ NEW RULE* - Go to this Article and apply the desired custom Rule for your Calculation
- If you don't want to drop negative values for
*currently_in*, you can use the first rule. - Otherwise, you can use the 2nd rule to rectify negative values

- If you don't want to drop negative values for
- Once the rule is created, your asset
*currently_in*will be automatically updated with the next payload (data) coming from the People Counter device.

So, the code should be like the one below:

device = allthingstalk.device.YOUR_DEVICE_ID

math = allthingstalk.math.basic.op

device.Total_Counter_A -> math.a

device.Total_Counter_B -> math.b

math.a-b -> device.currently_in

**Important**: you have to create a virtual asset „currently_in“ for each of your People Counter device.

Next to it, you can apply the same logic from the 2nd section, where you can display the calculation results from your People Counter devices.

Hence, before implementing the code below, you have to create a NEW device in the IoT platform, to be able to present the results of counting people the easiest way possible. To do that:

- Go to your Ground and click on the + NEW DEVICE button
- Name your device as you wish (e.g. People Counting)
- In this newly created device, create the Virtual Asset to present the result of calculations
- e.g. people_counter_meeting_room_a

- Now, go to the code below and apply the related Device ID to each device
- You can find the Device ID in Settings (upper right) > Authentication > Device ID

#### 3.1. Two people counters

device1 = allthingstalk.device.YOUR_DEVICE1_ID

device2 = allthingstalk.device.YOUR_DEVICE2_ID

deviceIN = allthingstalk.device.YOUR_DEVICEIN_ID

math = allthingstalk.math.basic.op

#Final calculation for two devices

device1.currently_in -> math.a

device2.currently_in -> math.b

#Final calculation for TWO devices

math.a+b -> deviceIN.total_in_DEFINE_YOUR_AREA

#### 3.2 Calculation for 3 people counters

device1 = allthingstalk.device.YOUR_DEVICE1_ID

device2 = allthingstalk.device.YOUR_DEVICE2_ID

device3 = allthingstalk.device.YOUR_DEVICE3_ID

deviceIN = allthingstalk.device.YOUR_DEVICEIN_ID

math1 = allthingstalk.math.basic.op

math2 = allthingstalk.math.basic.op

#Calculation for the 1st and 2nd device

device1.currently_in -> math1.a

device2.currently_in -> math1.b

math1.a+b -> math2.a

#Calculation for the 3rd device

device3.currently_in -> math2.b

#Final calculation for TRHEE devices

math2.a+b -> deviceIN.total_in_DEFINE_YOUR_AREA

#### 3.3 Calculation for 4 people counters

device1 = allthingstalk.device.YOUR_DEVICE1_ID

device2 = allthingstalk.device.YOUR_DEVICE2_ID

device3 = allthingstalk.device.YOUR_DEVICE3_ID

device4 = allthingstalk.device.YOUR_DEVICE4_ID

deviceIN = allthingstalk.device.YOUR_DEVICEIN_ID

math1 = allthingstalk.math.basic.op

math2 = allthingstalk.math.basic.op

math3 = allthingstalk.math.basic.op

#Calculation for the 1st and 2nd device

device1.currently_in -> math1.a

device2.currently_in -> math1.b

math1.a+b -> math3.a

#Calculation for the 3rd and 4th device

Device3.currently_in -> math2.a

Device4.currently_in -> math2.b

Math2.a+b -> math3.b

#Final calculation for FOUR devices

Math3.a+b -> deviceIN.total_in_DEFINE_YOUR_AREA

### Related articles:

- How to create a calculation for Currently IN
- Quick Start Guide - SafeSpace+
- SafeSpace+ device: User Manual