A Novel Dynamic Load Scheduling and Peak Shaving Control Scheme in Community Home Energy Management System Based Microgrids

Load scheduling and peak demand shaving are two critical aspects of the utility grid operation that help both the grid operators as well as end-users. This paper proposes a two-stage community home energy management system for microgrids. The first stage deals with the dynamic clustered community load scheduling scheme. Comparatively flatter power demand was attained using particle swarm optimization (PSO) incorporating user-defined constraints. The new arising or remaining peaks as a consequence of consumer constraints are catered to in the second stage. The second stage proposes a rule-based peak shaving management method for the photovoltaic (PV) systems that are connected to the grid and the battery energy storage systems. The proposed technique determines the dynamic demand and feed-in limits based on the estimations of the upcoming day’s load demand and PV power profiles. Also, the study presents an optimal rule-based management technique for peak shaving of utility grid power that sets the charge/discharge day ahead schedules of the battery. For peak energy minimization, PSO is used to calculate the optimal inputs needed for implementing the appropriate rule-based management strategy. MATLAB is used to test the proposed method for different PV power and load demand patterns, thus, achieving an average improvement of 8.5%.

The associate editor coordinating the review of this manuscript and approving it for publication was Elizete Maria Lourenco . Available PV and utility grid energy to charge battery (kWh). l a k DTL for device a of house k. P dem−lm , P fd−lm Demand and feed-in limits of the day (kW). P gd , E gd Utility grid power (kW) and energy (kWh). P PV , P bat , P LD PV, battery and load demand powers (kW). P a k Power consumption profile for device a of house k. p a k (τ ) Power consumption value for a th device of k th house, during τ th time slot.
p c Power consumption of community being optimized. SoC i , SoC f SoC at the start and end of the day. SoC l , SoC u Lower and upper limits of SoC. t a k ATS for device a of house k. th Threshold set for PUP at 2 kWh. x a k Device rating for device a of house k.

I. INTRODUCTION
Smart grids propose to achieve optimal electricity distribution and consumption in which the energy demand will incorporate all the smart appliances that produce and store electricity, thus, allowing the household consumer to fulfill the desired load requirements [1], [2]. Techniques such as demand response offer the potential residential consumer to shift their peak load to an off-peak period, thus, shrinking the overall peak to the average power [3]. Microgrids (MGs) which is a subsidiary concept of the smart grids, further expand this concept by employing various energy management techniques, to offer dual benefits for the power system [4]. Firstly, it can act as a single controllable energy asset to deliver grid-friendly power responses and various grid services [5]. Secondly, it can also coordinate distributed energy resources to provide a reliable and steady energy supply for local loads. MGs driven by distributed generators and power storage have become a substantial component of the electrical distribution system. They can address the growing demand for resilient and sustainable designs in commercial and residential dwellings [6].
Home energy management systems (HEMS) also warrant the steadiness and consistency of MGs [7]. HEMS commonly referred to the technique attributing to the usage of domestic appliances by residential consumers. HEMS plays an essential role in smart grid control system due to universal demand for electricity in residential sector [8]. Demand side management using load scheduling is one of the possible solutions to peak power demands in HEMS [9], [10]. Recent studies have shown the application of cluster-based load scheduling optimization strategies at the MG level [11], [12]. Yet they fail to consider consumer's preferences at appliance level. Integrated optimization of smart home appliances using grey wolf optimization (GWO) is proposed by T. Molla et al but for a small load of 5KW [13]. A modified GWO approach for a smart grid with integration of renewable energy sources is proposed by A. Kumar et al [14]. GWO based optimum energy management for grid connected microgrids is proposed in [15]. B. Papari et al propose crow search algorithm (CSA) based optimal energy management framework for hybrid microgrids [16]. Authors have considered various kinds of renewable energy sources, batteries, distributed generators and loads. In all these metaheuristic computationbased approaches, authors have not discussed controllable appliances with their user preferences involved. Considering the strengths of GWO and CSA, Waseem et al. utilize Grey Wolf and Crow Search Optimization (GWCSO) algorithm to reduce peak to average ratio (PAR) and electricity cost (EC) [17]. But the proposed technique considers only the HVAC loads for scheduling which limits the scope of GWCSO algorithm. The models suggested no mechanism to handle large data with various community types. Aziz et al. presents a power scheduling methodology for a large population [18], [19]. They utilized static clustering-based approach for handling community based residential consumers. However, the technique is based on the assumption of homogeneous consumption. This means that all appliances in the entire population have the same properties and belong to a similar class of consumers. Ayesha et al presents non-homogeneous load scheduling scheme with dynamic appliance clustering for a large population [10]. Despite, having some improvements in PAR and EC, there are still some peaks remaining in the load profile. Since, load scheduling has certain limitations i.e., it can only alter the demand to a specific limit in accordance with the user constraint before it has a negative impact on the system operation whilst becoming an issue for the user comfort. In order to address this limitation, one of the possible solutions for further modifying demand profiles is the use of energy storage system-based algorithm such as peak shaving [20]. Additionally, the existing schemes in literature haven't incorporated weather-based fluctuations in consumer behavior whilst incorporating user preferences. The literature review suggests that load scheduling and peak shaving be applied in a more practical scenario with change in consumer preferences with weather conditions. Peak shaving is known to be a critical application that helps grid operators as well as end users. Peak shaving is employed by grid operators to maintain a balance between supply and demand, resulting in a higher load factor and more cost-effective generator operation. It also improves the MG's and utility grid's system efficiency and power dependability [21]. Peak shaving, can help consumers save money on their electricity bills by moving peak consumption from a high-price period to a low-price period. It also provides end consumers with better power quality and reliability [22]. Peak shaving can be obtained by utilizing grid-connected battery energy storage (BES) systems [23]. The BES is a versatile method for absorbing and storing excess renewable energy source electricity and delivering it as needed [24]. BES is utilized to decrease utility grid power demand and increase PV energy utilization to increase the MG system's self-consumption [25]. For peak shaving using BES systems the charge/discharge schedules are controlled using a variety of methodologies, including genetic and rule-based algorithms, dynamic programming, and so on [26], [27]. Rule-based methods execute instructions by employing an initial set of data and rules based on if-then statements [28]. In comparison to other approaches, these algorithms have straightforward implementation and development that cannot generate optimized solutions. In [29], [30], and [31], rulebased peak shaving techniques are contrasted to optimization methods. Various optimization-based techniques incorporate demand and feed in limits. In the case of peak shaving, the demand limit (feed-in limit) is the maximum amount of power that can be extracted from (injected into) the electrical grid. A set demand limit is discussed in [32] and [33] for peak shaving using the battery controller. The feed-in limit, however, is not discussed. Reference [34] considers flexible daily management as well as effective PV energy consumption for peak shaving applications for a fixed demand ceiling. In [35], only the dynamic feed-in restriction is taken into account for peak shaving, ignoring the demand limit. In [36], peak shaving utilizing BES optimum schedules with a dynamic demand restriction is explored. However, feed in limitation is not discussed. In [37], both feed-in and demand powers are discussed while preserving flexible daily management. They also provide an effective rule-based strategy for peak shaving management, that is used to determine the ideal inputs for the suggested rule-based peak shaving management but for a single house [37]. The literature review suggests that a peak shaving algorithm be explored with application to a community-based architecture with a large number of households and/or resources.
Based on the above highlighted limitations of load scheduling and peak shaving in energy management system, a twostage dynamic clustered community-based home energy management system is proposed to address them. Stage-I focuses on load scheduling algorithm with application to a community architecture. While, to cater remaining peaks in the modified load profile, an optimal peak shaving algorithm with day-to-day energy management scheme is applied in the Stage-II. The proposed scheme achieves improved performance for community architecture in MGs. To make the model more relatable, closer to real world, and practical, the proposed model is focused on the community-based architecture utilizing non-homogenous loads analysis i.e., lower (LCS), middle (MCS), upper-middle (UMCS), and high class (HCS) consumer. The load is non-homogeneous due to the non-identical features of consumer products and various user preferences from different classes. It implements a demand response-based technique in load scheduling of smart devices (SDs) based on user preferences. It also takes into account many types of consumable appliances that are frequently used in homes. Each class has its own set of PV installations to consider. To account for seasonal fluctuations in consumer behavior, different usage parameters for SDs in summers and winters are examined in the study, as shown in Table 1.
Following are the main contributions in this article: • The study employs community consideration. In existing literature, only small data set of houses has been used. Additionally, no classes have been considered for such techniques. This work considers generation of winters and summers load profiles by acquiring people's preferences in different weather conditions.
• A two-stage control technique is proposed. Initially, load scheduling is implemented on the device level where the user preference-based profiles are generated. In the second stage, an optimal rule based peak shaving algorithm by involving BES and PV systems is applied. Peak shaving involves both dynamic demand and dayto-day feed-in limits.
• While taking flexible daily management into account, a rule-based control algorithm is proposed. It provides battery charge/discharge schedules for peak shaving of utility grid power. Limited feed-in powers and utility grid demand correspond to the day's feed-in limits and demand.
• For minimizing energy consumption from the utility grid, the ideal inputs necessary for suggested rule-based peak shaving management are derived using the PSO algorithm.
• The suggested optimal peak shaving control scheme is put to the test on the system under consideration.
Comparisons of qualitative and quantitative data with previous work are offered. The remainder of this article is structured in the following manner. The considered system is described in Section II. Stage I-Dynamic HEMS based load scheduling control scheme is presented in Section III. Stage II-Optimal peak shaving is presented in Section IV. The BES operational modes are discussed in Section IV-B. The proposed method of determining inputs is explained in Section IV-C. The suggested rule-based peak shaving control approach is discussed in Section IV-D. The determination of optimal inputs is explained in Section IV-E. The results and conclusions are presented in Sections V and VI, respectively.

II. SYSTEM DESCRIPTION
The main aim of demand response based HEMS is to reduce PAR and EC for the benefit of utility as well as customers. A MG compatible community based system architecture is proposed in this paper. The proposed scheme is applied to a community within one of the MG connected to many others. It is assumed that grid or electricity supply company communicates demand response related tasks to the substations. They then spread the word to their respective communities. The structure of the community-based scheme for HEMS utilization in smart grid is shown in Fig. 1. A two stage demand response based EMS is proposed in this paper where the first stage deals with dynamic load scheduling (DLS) followed by the second stage of dynamic demand and feed in limits based optimal peak shaving (OPS).
The model uses distinct user preferences from different classes, so the load is non-homogeneous. The two types of load demand profiles to be considered are; summer and winter profiles. As a general trend of residential consumers, some devices are more frequently used in summer as compared to winter and vice versa. For example, air conditioner is more frequently used in summer for cooling purposes. The same air conditioner is less frequently used for heating in winter and consumes less power as compared to cooling. This is due to the trend of more sunlight utilization and getting done with most of the tasks during the day. This is the reason why peak hours in the load demand are generally observed during 09:00 to 12:00 hours in winters, whereas they occur during 20:00 to 23:00 in summers [37]. Similarly, electric heaters, clothes dryers are more frequently used in winters as compared to summers. This is why clothes dryer is shown as not applicable (NA) for LCS in summers taken from Table 1. In contrast, as a general trend of people, the automatic washers and water pumps are used more in summers due to frequent clothes changing and bathing in hot season. But these do not require hot water as normal tap water is good enough in sunny days. Similarly, the dishwashers can use normal tap water in summers, whereas, they require heated water in winter to get rid of utensils greasiness. For the rice cooker, it is considered that LCS, MCS, UMCS generally takes meal thrice a day, unlike HCS. In winters, due to smaller days, only LC takes thrice, as they wake up too early in the morning. Considering all these facts and the usage parameters given for all the four classes given in [10], Table 1 provides the typical usage parameters for SDs for winters and summers. Classes of communities are analyzed both in winters and summers. Load profile of a community, consisting of 40 houses is considered. An equal number of houses are considered from each class of consumers for winters and summers with a peak load of 35.94 kW for summers and 33.89 kW for winters.

III. DYNAMIC HEMS BASED CONTROL SCHEME [STAGE-I]
This section presents a DLS approach for all SDs in a residential community. It capitalizes on real time electricity prices (RTP) and the modified inclined block rate (IBR) pricing methods. The proposed scheme can be applied to a real system with certain modifications.

A. RESIDENTIAL SDs USAGE PATTERN
For the user preferences involvement, the time parameters include activation time slot (ATS) t a k , device operation time start (DTS) α a k , device operation time end (DTE) β a k , device operation time length (DTL) l a k , device time interval (DTI) VOLUME 11, 2023 FIGURE 2. Smart device's parameter constraint [10]. α a k , β a k during which the SD is valid to be scheduled and SD rating x a k as depicted in Fig. 2 B. MODIFIED IBR WITH EMPLOYED PRICING SCHEME Many firms, including California Edison and Pacific Gas & Electric [38], have employed the IBR as a pricing method for a long period. The use of IBR reduced PAR. IBR controls the power demand of one device by suggesting its penalty factor. However, if a large number of appliances emerge at the same time, the power usage pattern (PUP) of the entire power system will skyrocket. This condition necessitates a power scheduling system that can scan the surrounding area while optimizing ATS for all appliances. The proposed algorithm avoid such scenarios by combining the modified IBR with RTP to overcome its drawback of concentrating many appliances at low electricity price areas [40]. By employing IBR pricing structure, the RTP rates are multiplied by a factor λ >1. This happens whenever a house's power consumption pattern exceeds a predetermined threshold range during any time frame. The penalty term is used by IBR to control this condition and prevent the scheduling algorithm from developing peaks in the power pattern. The IBR is adjusted in the suggested scheme to incorporate the penalty term that is only applicable when the power consumption pattern exceeds a γ c scaled threshold. The threshold represents the number of dwellings in the present community. There are two electricity price levels to consider, with the electricity price changing hourly. The RTP contains a modified IBR control, which is written as: is the real-time electricity price sent by the electric utility company over TS τ , community's power utilization p c decides electricity price given as rtp (τ ), th is the load threshold which is set as 2 kW, and γ c is the number of households in the present community.
The group of houses in the current household community is denoted by C h , while C c denotes the current cluster of SDs. The incorporated IBR in RTP can reduce peaks in PUP to some extent. However, when a large data set consisting of many residential consumers is involved, IBR alone cannot handle the situation [10]. Therefore, there is a need to propose a dynamic device clustering based HEMS approach that can perform effective load scheduling. Section III-D presents such an approach.

C. FINAL OBJECTIVE OF STAGE I APPROACH
For effective analysis of the proposed scheme, 1 hour is divided into 10 minutes length slots. As a result, a day comprises of 144 TSs, indicated by τ ϵ T , which is from the range {1, 2, 3 . . . .144} [41].
A denotes the set of SDs. There are 16 devices in one house; a ϵ {1, 2, . . . .., 16}. For each device a k ϵ A, it is assumed that P a k is the power consumption scheduling vector of dimension 1×144, where p a k (τ ) represents kW power consumption value for a th device of k th house, during τ th time slot. These per time slot values are obtained by dividing the corresponding power consumption values per hour by 6. The power consumption values for devices are assumed as shown in Table. 2.
The ATS for the a th device of k th house is defined by the variable t a k . Once t a k is determined, then the power consumption scheduling vector of a device a is computed because t a k is known. Having α a k , β a k , and l a k are all known, t a k should be greater than or equal to α a k , and less than or equal to β a k − l a k . In other words, the adjustable parameter t a k is denoted as EC is lowered in our case by applying PSO to determine the best t a k allocation for each dwelling in the neighborhood. Customers provide the user preference which is the initial value for optimization termed as α a k . The electricity costminimizing cost function is then saved, and particle best (pbest) location is adjusted.
We must calculate the optimum value of ATS for every SD subject to the constraint specified in the equation for the a th device and k th house in (3). The ATS for all the SDs is stored in a variable vector t a 1k , t a 2k , . . . t a ik . Therefore, a power consumption scheduling matrix for all SDs would have the expression as where P denotes a matrix in which each row stands for the power schedule of a certain device. τ specifies the column indices. τ / ∈ t a k , t a k + l a k denotes that τ belongs to T but not to the range t a k , t a k + l a k . Each column vector of the power utilization scheduling matrix is added up to calculate the total power utilization scheduling vector pscd.  P (τ ) denotes the τ th column in the power utilization scheduling matrix in (5). When the power utilization scheduling problem is defined for a single residence, following is the expression for objective function where The electricity price at the τ th TS is denoted by rtp in equation (7). An optimization strategy can be used to reduce the EC shown in equation (7).

D. FORMULATION OF DHEMS
For various houses, the appliances in question are supposed to have their α a k in a time slot with the lowest electricity price compared to its successor slots. In this case, any scheduling method used in conjunction with the IBR will tend to push t a k of all houses towards the slot with the lowest electricity price. Despite this, IBR is able to keep the PUP of each dwelling below the required level. However, a PUP peak in the general community will be caused by the constellation of appliances t a k arranged around the lowest electricity price. It eventually affects the entire power grid. This condition necessitates a power scheduling system that can scan the surrounding area while optimizing ATS for all appliances. As a result, the following is how the proposed algorithm solves the situation by utilizing device clustering in a dynamic clustered home energy management system (DHEMS). The grid or energy supply business is anticipated to communicate demand response related assignments to the substations. They then spread the word to their respective communities. A community of 40 houses is classified into four type of community classes for non-homogeneous load analysis: LCS, MCS, UMCS, and HCS consumers. As shown in Table 1, each of these four classes has its own set of user preferences based on their daily habits. Table 2 depicts the power ratings utilized for SDs in all four classes. A randomly generated one-day load profile that is exposed to PSO to discover the optimal clustering set among all possible clustering combinations of C1, C2, and C3 as shown in Fig. 3. The C3 is varied from 2 to 7 clusters per community with both uniform and unequal cluster sizes [19]. C1 decides the size of each community under each class of consumers. We have considered 10 houses in each class. Therefore, each class has two communities with 5 houses in each. The SDs in the communities are placed into C3 clusters after being classified according to C2. According to the C1 optimal value, each community consists of 5 dwellings. Under C2, DTE is chosen as the sorting parameter. As the optimum value, the number of SD clusters in each community designated by C3 is set to 5.
The developed algorithm is employed on the formulated data for load scheduling of the SDs. The algorithm as depicted in stage 1 of Fig. 4 begins with sorting all dwellings before creating groups of communities based on C1. Because it is dependent on each cluster's average PAR, the houses are dynamically selected into communities. Hence, 16 devices in each house are split into 5 clusters per dwelling according to their DTS and DTE. PAR is determined for five clusters, and all dwellings are ranked by maximum PAR in increasing order. Because each community is 5 houses, the lower class is made up of 2 communities totaling 10 dwellings. Each of the remaining three classes is made up of 10 dwellings. As a result, each class has two communities. There are 80 devices in a community of 5 dwellings, that is 5 × 16.
DHEMS follows the following steps: Step 1: The whole population is separated into four classes with equal number of houses in each class.
Step 2: Population is sorted using staggered houses sets so that PARs must descend in corresponding clusters for the total population.
Step 3: Criterion for the best clustering is chosen.
Step 4:Categorization of devices by neighborhood is done by C2.
Step 5: C3 determines each community's device cluster number.
Step 6: Within the range α a k , β a k − l a k , the parameters t a k belonging to the current cluster are set, and Step 4 is repeated till all clusters are completed. Groups of t a k are used as particles.
Step 7: By evaluating P cc and EC, fitness is computed for each cluster.
Step 8: After updating velocities and positions of particles, if the new particle's fitness is better than the previous pbest, the pbest is updated. If later is preferable, replace global best (gbest) with pbest.
Step 9: If the termination requirement is not met, proceed to step 6.
Step 10: Once the full population has been scheduled, stop.
Step 11: Repeat Steps 8-11 until all of the communities have been scheduled.
The steps taken by the algorithm are depicted in stage 1 of the flow diagram in Fig. 4. The following is the formulation of the overall power scheduling goal: Here, EC(P cc ) is the total EC based on PUP, PUP for the cluster of the community being scheduled is denoted by P cc , rtp pc (τ ) represents electricity rate for the τ th time slot according to equation (1), p a k (τ ) is the power rating of SD for k th house and a th device. All the houses in the current community are represented by C h , current cluster is denoted by C c , therefore, the objective function of our proposed algorithm is to minimize overall consumer EC of power consumption. The modified IBR is applied on the entire community to keep the PAR under control, as the population is divided into several smaller communities.
The outcomes of the proposed load scheduling scheme in stage I are reflected in Fig. 6 for winters and summer day conditions. Results show reduction in PAR as compared to the actual load demand. But some still existing or new emerging peaks suggest that an optimization scheme can be used to reduce them further.

IV. OPTIMAL PEAK SHAVING (OPS) BASED CONTROL SCHEME [STAGE-II]
DLS of SDs in stage-I reduces PAR with some load peaks remaining as shown in Fig. 6. Therefore, to cater the remaining load peaks, the proposed stage-II defines a rule-based OPS control using PSO algorithm which is applied on load scheduled output of stage-I. As shown in Fig. 1 [37], a gridconnected residential community system is presented in which the OPS is proposed for a utility grid-connected MG, with a community-based HEMS architecture. It comprises of PV source, BES and consumer loads within the MG system. As grid is a power source that can both deliver and absorb energy, by ignoring system losses at the point of common coupling (PCC), the power balance equation is defined as follows P gd (t) + P pv (t) + P bat (t) = P LD (t) (10) where, P gd denotes utility grid power. P pv , P bat and P LD represents PV, battery and load demand powers all in kWs. t denotes the time interval [(t − 1) × T C , t × T C ] and T C represents the time slot duration, i.e., T C = 10 minutes. Note that P gd is assumed to be the load scheduled output of stage I. Henceforth, P gd refers to the load scheduled output of stage I that requires peak shaving with the help of distributed energy resources such as PV and BES.  [39]. Each community will have its own locally generated PV in various houses. It is assumed that LCS has 2 %, MCS has 4 %, UMCS has 6 % and HCS has 8 % of PV installation. Additionally, it is assumed that PV system with a capacity of 15 kW is installed where, LCS has 300W, MCS has 1.3 kW, UMCS has 1.5kW and HCS has 3.2 kW. For peak shaving, a battery bank with a rating of 240 V and 600 Ah has been selected.

B. OPERATING MODES OF BES
It is possible to restrict P gd (t) to demand limit P dem−lm using the battery and PV source under consideration. In Fig. 5, the operating time slots of BES modes for average PV power and load demand profiles are shown. In the presence of a PV source, there are four modes of operation to limit P gd (t) to P dem−lm via a BES.

1) DISCHARGING MODE: [DCH-MD]
Occurs through the time t dsch , when the demand limit has been crossed by the load demand and the PV source fails to provide the needed power, i.e., P LD (t) > P dem−lm && P pv (t) ≤ P LD (t) − P dem−lm . Note the symbol && indicates logical AND operator.

2) CHARGING MODE-I: [CH-M1]
Occurs through the time t chg1 , when the load demand is within the range of the demand limit, i.e., P LD (t) < P dem−lm . VOLUME 11, 2023

3) CHARGING MODE-II: [CH-M2]
Occurs through the time t chg2 , when demand limit has been crossed by the load demand and the PV source successfully supplies the needed power, i.e., P LD (t) > P dem−lm && P pv (t) > P LD (t) − P dem−lm .

4) CHARGING MODE-III: [CH-M3]
Occurs through the time t chg3 , when the load demand is within the range of the demand limit and PV source is not available i.e., P LD (t) < P dem−lm && P pv (t) = 0.

C. PROPOSED TECHNIQUE TO DETERMINE INPUTS
Predicted load demand and PV powers are used to determine the required inputs for the suggested rule-based peak shaving management. P dem−lm , E bat−chg , E pv−chg , E gd−chg , C gd , P m dem−lm , and P fd−lm are the inputs. Initially, P dem−lm , E bat−chg and E pv−chg are determined. Then, E gd−chg is determined if E pv−chg ≤ E bat−chg . And P m dem−lm is determined if E pv−chg +E gd−chg ≤ E bat−chg ; otherwise C gd is determined. Where P fd−lm is determined if E pv−chg ≤ E bat−chg . The charge/discharge schedules of BES for peak shaving management are determined via these inputs. Following is the technique to determine these inputs.

1) DEMAND LIMIT
Let us specify a control variable called the BES's dischargeable energy over 24 hours E * bat−dsch , which is selected from 0 kWh to the BES's rated energy capacity E bat−rated (which contains both 0 kWh as well as E bat−rated , i.e., 0 ≤ E * bat−dsch ≤ E bat−rated (11) Since E bat−rated is 132 kWh, E * bat−dsch ϵ [0, 12] kWh. To determine the demand limit, it is defined such that E bat−dsch is equal to E * bat−dsch . The result achieved are When P LD (t) > P dem−lm , the required quantity of power P LD (t) − P dem−lm is delivered to the load by a PV source or a battery. The BES, on the other hand, delivers the power that the PV source fails to supply. Consequently, the resultant is; Substituting (14) into (13) gives (15) Equation (15) is in form of f (P dem−lm ) = 0 where P dem−lm is an independent variable in equation (16). The root-finding method of the regula falsi approach is utilized to solve for P dem−lm [43]. The secant method and the bisection search theorem are combined in the regula falsi method. The regula falsi approach is quicker than the bisection approach and guarantees root convergence. (P dem−lm1 , P dem−lm2 ) are selected using the regula falsi approach so that f (P dem−lm1 ) is a positive value while the f (P dem−lm2 ) is negative. P dem−lm0 is then calculated using the following equation: where, m is defined as . Using equation (17), we determine f (P dem−lm0 ). When |f (P dem−lm0 )| < e, P dem−lm0 becomes P dem−lm . When |f (P dem−lm0 )| > e, either replace P dem−lm1 by P dem−lm0 , if (f (P dem−lm0 ) > 0) or replace P dem−lm2 by P dem−lm0 if (f (P dem−lm0 ) < 0). Then, continue the above process till P dem−lm0 equals P dem−lm . Note, here e is the tolerance and m is the slope of regula falsi method.

2) ENERGY NEEDED TO CHARGE BATTERY OVER 24 HOURS
To be flexible for daily management, the energy necessary to charge the BES over 24 hours must be equivalent to the energy required to discharge the BES over 24 hours, i.e.,

3) PV ENERGY AVAILABLE TO CHARGE BATTERY OVER 24 HOURS
In can be deduced from equation (18), the BES will be charged by the total energy E bat−chg , from either a PV source or from the utility grid. Firstly, the PV energy that is available for charging the battery over the duration of 24 hours is determined (without having to inject into the grid). If the available PV energy is insufficient, then the utility grid energy that may be available for charging the BES is calculated.
The P pv−chg is P pv (t) and P pv (t) − P LD (t) − P dem−lm (t) during t chg1 and t chg2 , respectively, i.e., The PV energy available to charge the BES over 24 hours is then the sum of P pv−chg over 24 hours. It is given as; (20) Here, T is the 24-hour predictive horizon which is considered as 144 TSs in our case.

4) UTILITY GRID ENERGY AVAILABLE FOR CHARGING BATTERY OVER 24 HOURS
If E pv−chg ≤ E bat−chg from equations (18) and (20) is true, the available PV energy is insufficient for charging the battery with the needed amount of energy. Consequently, if the demand is not greater than the demand limit, a deficit energy amount is extracted from the utility grid. It signifies that during t chg2 , the utility grid was not utilized for charging the battery. Then, for restricting P gd to P dem−lm during t chg2 , the available power in the utility grid to charge the battery P gd−chg (t) equals P dem−lm − P LD (t), i.e.
The available utility grid energy for charging the BES over 24 hours is hence the sum of P gd−chg (t) over a day, as shown in

5) COEFFICIENT OF UTILITY GRID ENERGY TO CHARGE THE BES
If E pv−chg ≤ E bat−chg && E gd−chg + E pv−chg > E bat−chg , the deficit energy amount to fully charge the BES, i.e. E bat−chg − E pv−chg , must be provided by the utility grid, as shown in equations (18), (20) and (22). But, when using the total amount of the available PV energy for charging the battery, only a portion of the utility grid energy is needed. In the mentioned situation, if C gd E gd−chg is used as the needed utility grid energy for charging the ES, it equals E bat−chg − E pv−chg , as

6) MODIFIED DEMAND LIMIT
If E bat−chg + E pv−chg ≤ E bat−chg from equations (18), (20) and (22) is true, the battery fails to charge with the appropriate amount of energy to limit P gd to P dem−lm . In this situation, SoC f cannot match with SoC i , resulting in a breach of flexibility for daily control. To prevent this violation, P dem−lm is changed so that the sum of the energy available from the utility grid and the PV source for charging the BES over time T matches the energy expected to be discharged by the BES over the duration of T. Therefore, the resultant expression for calculating the modified demand limit is as follows,

7) FEED-IN LIMIT
Based on the equations (18) and (20), if E pv−chg > E bat−chg , then the BES can be charged with the appropriate quantity of energy without using all of the available PV energy. As a result, a PV power limit P fd−lm is established in a way that the PV source is not utilized for charging the BES when P pv−chg (t) ≤ P fd−lm . When P pv−chg (t) > P fd−lm during the period t chg , the battery is fully charged with P pv−chg (t) − P fd−lm , i.e.,

D. PROPOSED RULE-BASED PEAK SHAVING MANAGEMENT
To determine the BES charge/discharge schedules for the upcoming day, rules for peak shaving management are formulated using the previously calculated inputs. These regulations are written in a way that they ensure flexibility in the daily management while restricting the peak utility grid demand and feed-in powers to the relevant demand and feedin limitations. This section explains the defined principles for discharging and charging modes.
Rule-3: If E pv−chg ≤ E bat−chg &&E pv−chg + E gd−chg ≤ E bat−chg , the battery's charging amount using the PV source and the utility grid is represented as P pv (t) + P m dem−lm − P LD (t) . Rule-4: If E pv−chg > E bat−chg &&P pv (t) > P fd−lm , the BES's charging amount using the PV source is represented as P pv (t) − P fd−lm as per equations (19). Rule-5: If E pv−chg > E bat−chg &&P pv (t) ≤ P fd−lm , the BES does not use the PV source for its charging.
Rule-8: If E pv−chg > E bat−chg &&P pv (t) − (P LD (t) − P dem−lm ) ≤ P fd−lm , the BES does not use the PV source for its charging.
D. CH-M3 (During t chg3 ) Rule-9: If TS < 10 and a considerable peak in load arises before the PV power appears, i.e., P LD (t) > P dem−lm . Then the BES takes charge from the utility grid with the amount C gd (P dem−lm − P LD (t)) so that peak arising before the PV power appears in regular sunlight timings may be catered by sufficient BES.
Rule-10: If TS > 130 && SoC(t) ≤ SoC f , BES takes charge from the utility grid with the amount C g (P dem−lm − P LD (t)) so that SoC f = SoC i for flexible day to day management.
In this study, the SoC of the BES in discharging/charging modes is determined using the coulomb-counting approach given in [44]. Based on the preceding Rules 1-8, the resulting utility grid power is presented in Table 3 (a).

E. ESTIMATION OF OPTIMAL INPUTS
The peak shaving of utility grid electricity while ensuring optimal utilization of the BES is critical. To formulate such an optimization problem, the following fitness function and constraints have been considered minimize f = E gd−pk (26) subjected to, P gd (t) + P pv (t) + P bat (t) = P LD (t) (27) SoC l ≤ SoC(t) ≤ SoC u , SoC f = SoC i (28) P bat−chg (t) ≤ P bat−chg−mx , P bat−dsch (t) Here the goal to minimize E gd−pk while enforcing the power balance constraint in equation (27), the BES's SoC constraints (to ensure BES's flexibility in daily operations) in equation (28), restrictions on the battery's charge/discharge power in equations (29) and dis-chargeable energy during a day in equation (30). Note in equation (26), E gd−pk is the peak energy drawn from the utility grid over the course of a day, i.e., where, E gd is determined as, Because the inputs needed for peak shaving control are dependent on E * bat−dsch , as previously mentioned, E * bat−dsch is regarded as a control variable. The problem represents an offline optimization problem that is defined with a nonlinear fitness function. The problem is handled in MAT-LAB with PSO which is a prominent heuristic optimization method [19]. PSO is frequently used for resource scheduling and peak shaving algorithms as it has a low level of computational complexity and can quickly identify nearly optimal solutions in a reasonable timeframe [45]. Therefore, PSO is appropriate for our community-based architecture that consists of large number of houses. Also, the battery's optimal dis-chargeable energy E * obat−dsch is determined using the PSO. After determining E * obat−dsch the inputs related to E * obat−dsch are regarded as the optimal inputs needed for the postulated rule-based control, i.e., P odem−lm , E obat−chg , E opv−chg , E ogd−chg , C ogd , P odem−lm , and P dem−lm . It signifies that the optimal rule-based inputs are obtained as a result of optimization, i.e., solving the optimization problem. The proposed rule-based peak shaving management method then uses these optimal rule-based inputs to generate optimal BES schedules. The suggested two-stage DHEMS based peak shaving management is depicted in Fig. 4 as a flowchart.

V. SIMULATION RESULTS
To demonstrate the application of the proposed technique for any grid-connected PV system using BES, the technique has been tested on the studied system for various load and PV power profiles. Fig. 6 shows the outcomes of the first  stage. The optimal inputs necessary for performing the control algorithm for these cases are determined in the second stage and are listed in Table 4b. The best fitness value is acquired for multiple runs of PSO algorithm for the case of winter load profile with more PV availability. The minimum value among these best fitness values (considering all runs), i.e., 19.33 kWh, is the optimal peak energy drawn from the utility grid. For these cases, the acquired results using the proposed technique are discussed as follows.

A. CASE 1&3: LOAD PROFILE FOR WINTER AND SUMMER WITH HIGH AVAILABILITY OF PV ENERGY
In this scenario, the load demand profile for winter and summer that has a higher availability of PV energy availability during a day is taken into account, as shown in Fig. 7a (i) and 8 a( Table 3 (b). But the grid power is only used in the beginning for charging BES with off peak power to handle any arising peak before PV appears. Also, at the end of the day, the grid power is used to restore BES SoC to 50% for daily management. Hence, the values of E ogd−chg and C ogd for DHEMS are also shown in Table 3 (b). As per Fig. 7a( Fig. 7a (v) and 8 a(v) depicts the SoC for the resulting BES schedules. Fig. 7a(iii) and 8a(iii) illustrates that SoC i = SoC f = 50%, which is desirable to ensure flexibility in daily management. Fig. 7a (v) and 8a (v) depicts the corresponding utility grid demand. The illustration suggests that the utility grid demand is capped for P odem−lm = 19.9321 kW (winters) and P odem−lm = 20.3317 kW (summers) and the feed in power is limited to 2.2432 kW (winters) and 2.4009 kW (summers). The DHEMS based OPS (i), (iii), (v) and (vii) can be compared with the NDHEMS based reference scheme (ii), (iv), (vi) and (viii) as shown in Fig. 7a and 8a. It can be observed that the percentage peak shaving (PPS) reduction for the proposed scheme is improved by 10.26% (winters) and 7.79% (summers) as compared to the non-dynamic scheme.
The detailed comparison of all the parameter values is shown in As a result, in this scenario, P * odem−lm and P ofd−lm are not applicable (NA) as shown in Table 3 (b). According to Fig. 7b (iii) and 8b (iii), for the estimated P obat−chg , the discharging mode DCH-MD occurs during the period t =  Fig. 7b (iii) and 8b (iii). Figure 7b (v) and 8b (v) depict the SoC for the estimated BES schedules. Fig. 7b (iii) and 8b (iii) illustrate that SoC i = SoC f = 50%, which is appropriate for flexibility in daily management. Fig. 7b (vii) and 8b (vii) reflect the utility grid demand. This means that the utility grid demand is limited to P dem−lm = 24.6328 kW/TS (winters) and P dem−lm = 23.7314 kW (summers) with no feed-in power available. The DHEMS based OPS (i), (iii), (v) and (vii) can be compared with the NDHEMS based reference scheme (ii), (iv), (vi) and (viii) as shown in Fig. 7b and 8b. It can be observed that the PPS reduction for the proposed scheme is 2.94% (winters) and 7.79% (summers) improved as compared to the non-dynamic scheme. The detailed comparison of all the parameter values is shown in Table 3 (b).

1) DISCUSSION AND COMPARISON
The study is inspired by the system chosen in [37] by Rampelli et al which is quantitatively compared in Table 4 (a). The method proposed by Rampelli et al is applied on the MG community structure to analyze the comparison with the proposed scheme. In the proposed method, the peak utility grid power (PUGP) is less for the proposed method as compared to [37] in all cases. It means an improved PPS is achieved with the proposed method. Since for the proposed method, the stage 1 DHEMS improves the PAR of load profile initially. Moreover, modification of charging mode (CH-M3) addition in the proposed scheme also utilizes available distributed energy resources effectively. A qualitative comparison Table 4(b) shows the performance of techniques proposed in this paper with previous work. On the average improvement of 8.5% in PPS for the cases of winters and summers is achieved.
The available literature does not consider the demand and feed-in restrictions in OPS following DLS scheme, as presented in stage I of our proposed scheme. However, the proposed solution takes into account both demand and feed-in constraints, as well as a dynamic load scheduling HEMS scheme, while maintaining the system's flexibility on a daily basis. Furthermore, demand and feed-in restrictions are assumed to be dynamic. It means that the demand and feed-in restrictions change depending on the PV power and load demand forecasts for the day.

VI. CONCLUSION
The paper proposed a two-tier, dynamic, clustered, and community HEMS aimed at scheduling and balancing load in the power grid. The first step illustrates the dynamic clustered community load scheduling scheme. Due to the optimization in the first stage based on user constraints, the remaining peaks are taken care of by optimal peak shaving in stage II. To limit the use of energy from the public grid, optimal dynamic demand and supply limits are determined for a community MG with integration of a PV source via a battery.
The proposed control algorithm is tested for a variety of load demand scenarios and PV power profile. The collected data show that utility grid demand and feed-in powers are constrained to the day's demand and feed-in limits in all scenarios. Moreover, for a flexible daily management, the SoC is maintained at the same value at the end of the day as at the beginning of the day. To minimize the peak energy drawn from the power grid, a PSO technique is used to calculate the optimal inputs needed to implement the appropriate rule-based management strategy. The proposed control algorithm is compared to previous work both qualitatively and quantitatively. The results showed that the proposed control algorithm outperforms previous work in terms of percentage peak removal, showing an average improvement of 8.5%. More accurate and precise calculations of BES and PV ratings for the proposed shared apartment architecture can further improve the results. Areas for future research recommended could be the implementation of metaheuristic optimization techniques such as hybrid grey wolf and crow search algorithm with incorporation of electric vehicles with grid-tovehicle and vehicle-to-grid operations.