## 2 Materials and Methods
### Algorithm  
The proposed algorithm adapts the sensor network to the person's living conditions. This adaptability operation saves energy. The nodes present adaptive hierarchies learned based on the person's way of life. The sensors adapt their hierarchy IDs to prioritize those uses of the house. These hierarchies are learned over three days, and then the sensors adapt the network topology so that devices with higher hierarchies have more continuous measurements of the person or place in the house. Additionally, the algorithm has a time of 4 h in which if it does not detect activity, the sensors enter a sleeping mode. This period is not taken into account if it is during the night. This sleeping mode saves energy in the nodes when people leave the house or are traveling.  
Algorithm 1 details the process of optimizing energy in the network, basing the savings on the routing protocol's operation and the nodes' hierarchies. The value is the parameter that each sensor measures. This proposed algorithm saves energy in network control packets and, therefore, in the network overheads. This is because the formed groups (clusters) change their management of the routing protocol to a reactive or proactive nature depending on the node activity. Sensors have a higher hierarchy when their change threshold exceeds a specific steady-state value. This means that the sensor has more activity or is used more in that area than others. In each zone of the house, there is at least one motion sensor. Therefore the algorithm can detect the most continuous presence in each zone of the person's home. In addition to hierarchies, clusters of nodes are created to share energy costs. Clustering promotes topology maintenance and efficient power routing by transferring a significant portion of the communication overhead to the cluster head. The nodes with a higher hierarchy create a cluster, and with this, the routing protocol acts reactively to keep the nodes more alert. The lower hierarchy clusters are kept in sleep mode, and the routing protocol changes to a proactive nature to have more spaced control-packet exchange times.  
```
Start Require: coordinatornodesstarts; Per each modedo: Set Hierarchy = 0; Set allowed_value; Set day -1; while time <= end_time do: Set avg_net_vector(D <... average met per day;  Set std_dev_met <- standard deviation met;  if day > 3 for i - 0; i < length(avg_ind_vector[]); i++  avg_met <- average metric value;  if avg_met >= allowed_value  Hierarchy++;  end if  end if  day++;  end while  end do  Nodes with same Hierarchy for=cluster;  per each ID_cluster do:  if ID_cluster_i > ID_cluster_j  Set nodes from ID_cluster_i inactive mode;  Set nodes from ID_cluster_i with reactive  routing protocol;  Set nodes from ID_cluster_j in sleeping mode;  Routing protocol;  end if  end do
```  
**Algorithm 1** Pseudocode of the algorithm.  
Figure 4 shows the general scheme of the operation of the proposed algorithm. Initially, the nodes are distributed throughout the house, and the person moves freely within it. All sensors require three days to obtain an average value of their measurement metric. Whenthe person spends more time in the same place in the house, the sensors in that area increase their hierarchy and create a cluster. The other sensors remain in their current hierarchy. Sensors with a higher hierarchy are put into active mode and their routing protocol works reactively. The other sensors are put into sleep mode for specific periods and the routing protocol works in proactive mode.