*3.5. Algorithm*

The general algorithm as well as all the steps described in the previous subsections are described in Algorithms 1–5.

### **Algorithm 1** Main algorithm

```
1: sensorId,sensorState,sensorTimestamp
                                            ← getSensorEvent()
2: if sensorState == On then
3: sensorsList
                   ← updateSensorsList(sensorID,sensorTimestamp,On)
4: if goIn(sensorsList,sensorID) == True then
5: for each DRSR in DRSRList do
6: roomentrance
                           ← addNewPerson()
 7: end for
8: else
9: if goOut(sensorsList,sensorId) == True then
10: for each DRSR in DRSRList do
11: roomentrance
                              ← zeroPerson()
12: end for
13: else
14: if overlap(sensorsList,sensorId) == True then
15: DRSR
                         ← ref resh(sensorsList,sensorId, time())
16: else
17: DRSR
                         ← update(sensorsList,sensorId,sensorTimestamp)
18: end if
19: end if
20: end if
21: else
22: if sensorState == Off then
23: sensorsList
                       ← updateSensorsList(sensorID,sensorTimestamp,Off)
24: DRSR
                  ← update(sensorsList,sensorId,sensorTimestamp)
25: DRSR
                  ← rssDecay(sensorTimestamp)
26: else
27: DRSR
                  ← ref resh(sensorsList, All, time())
28: end if
29: end if
```
### **Algorithm 2** Function goIn


### **Algorithm 3** Function goOut


### **Algorithm 4** Function update

1: *de f update*(*sensorsList*,*sensorId*, *time*) : *DRSR* 2: **if** *dilemma*(*sensorsList*, *time*, *DAG*) == *True* **then** 3: *DRSR* ← *addNewDRSR*(*sensorsList*, *time*) 4: **else** 5: **if** *roomsTrans f er*(*sensorsList*, *time*, *DAG*) == *True* **then** 6: *DRSR* ← *moveDRSR*(*sensorsList*, *time*) 7: **else** 8: *DRSR* ← *ref resh*(*sensorsList*,*sensorId*, *time*) 9: **end if** 10: **end if**

### **Algorithm 5** Function refresh

```
1: de f ref resh(sensorsList,sensorId, time) : DRSR
2: DELTA = time − lastTime
3: if sensorId! = ALL then
4: DRSR ← rssArise(sensorsList,sensorId, time)
5: else
6: if DELTA ≥ INTERVAL then
7: lastTime = time
8: DRSR ← rssDecay(DELTA)
9: DRSR ← resize()
10: DRSR ← merge()
11: DRSR ← f usion()
12: DRSR ← delete(numDRSR)
13: end if
14:endif
```
### *3.6. Algorithm Coordinator*

Two further important algorithm steps are introduced: the two-stage process and the restart mechanism. The former is needed to balance the accuracy and stability of our algorithm; the latter is used to avoid an accumulated error for long-time running.

There are two challenges that our algorithm has to face: the first one is that the initial number of people and the initial status of the house are unknown because the smart home system may start at any time; the second challenge is that sensors in the smart home cannot distinguish multiple persons. For example, if two people are in the same room, they are regarded as one person, because sensors cannot see the difference with respect to the case of a single person. In this condition, the algorithm would estimate fewer people. To solve the two problems above, the algorithm works in two different stages: refresh stage and stable stage.


A parameter called *Refresh Stage Duration* is set to switch the algorithm from the refresh stage to a stable stage. The diagram of our two-stage process is shown in Figure 7.

Because smart home systems need to run for months, our proposed algorithm also needs to run for a long time. To avoid accumulated errors for the house status and people number estimation, the algorithm needs to be restarted regularly. The length of the Refresh Stage Duration has been tuned on the field, and the results are shown in the experimental section.
