*4.1. Forecasting*

Solar PV and load demand forecasts are required in order to schedule the charging of EV and BES accurately. The forecasting of PV energy can be divided into three categories: statistical data-based methods such as Auto Regressive Integrating Moving Average (ARIMA) [37,38] and machine learning based methods such as neural networks. The disadvantage of the methods mentioned above is that their accuracy solely relies on historical data, as there are no environmental inputs. The third category is a hybrid method based on historical data as well as weather data and satellite images. This study uses the solar radiation forecasts of the royal dutch weather institute. The advantage of this is that the computation of the forecasts is being performed outside the moving horizon controller and is based on previous data, satellite images, weather prediction modelling, and radiative transfer modelling [39]. The forecast is a combination of a short-term forecast (0–6 h) (SEVIRI) and a long-term forecast up to 48 h (HARMONIE) and includes Global Horizontal Irradiance (GHI) and Direct Normal Irradiance (DNI) [30]. In combination with the moving horizon control, good accuracy on the short term can be obtained, while at the same time, scheduling up to 48 h ahead can be performed. Then, using the approach presented in [32], the produced PV power can be calculated based on the orientation of the solar panels. Forecasting the load demand is done based on an aggregated residential profile obtained from [40]. Here, both appliance and heating demand are taken into account. An example of the forecasted and actual powers is shown in Figure 2. Even though the forecasted trend of PV power is accurate, sudden clouding can still cause significant errors of several kilowatts. Similarly, for the load demand profile, its highly stochastic nature is not captured by the aggregated data. However, daily and seasonal variations are incorporated. Due to the existence of these errors combined with a relatively coarse resolution of 15 min in most optimal scheduling problems, significant deviations from the actual optimal solution would occur. This motivates the use of a real-time control scheme.

**Figure 2.** Example of the resulting PV and load forecasts and the actual powers for a summer day.

#### *4.2. Optimal Charging Algorithm*

In this section, the optimization model is discussed. All variables are denoted and described in the nomenclature at the end of this article. The optimal charging algorithm's goal is to find the optimal charging schedule based on the forecasts of load and PV while taking into account battery degradation and ancillary services. The main contribution of the proposed optimization problem is the use of a Li-ion battery degradation model. Because of the good balance of power density, energy density, and lifetime, Nickel-Manganese-Cobalt (NMC) based batteries are being used for both vehicle and stationary applications [41]. Therefore, the same ageing model can be used for both the stationary and vehicle battery. Similarly, the costs of V2G, regulatory services, and degradation can be determined based on the actual operating conditions. Furthermore, since the model is included in the objective, the solver will minimize the degradation and the resulting costs. The non-linear behaviour of these cells makes the optimization problem a non-linear programming (NLP) model. The problem was solved using the CONOPT solver (part of BARON/Antigone [42]) using the generic algebraic modelling system (GAMS) platform on a PC with 3.6 GHz, Intel Xeon 4 core and 16 GB RAM.

## 4.2.1. Objective Function

The objective of this optimization is to minimize the total cost of energy. Here, the total cost of energy *Ctotal* is made up out of battery energy storage costs *CBES*, electric vehicle costs *CEV*, PV energy costs *CPV*, grid energy costs *Cgrid*, and regulatory revenue *Creg*. Mathematically, this can be expressed as follows: 

$$\min\left(\mathbb{C}\_{\text{total}}\right) = \min\left(\mathbb{C}\_{\text{BES}} + \mathbb{C}\_{EV} + \mathbb{C}\_{PV} + \mathbb{C}\_{\text{grid}} - \mathbb{C}\_{\text{reg}}\right) \tag{1}$$

The battery costs are operational costs, which are determined by assessing the remaining value of the degraded battery. This is done by calculating the degraded capacity Δ*Etot BES* and by subtracting this from the initial capacity *Emax BES*, both in kWh. Next, the remaining value per kWh, *VBES* in euro/kWh, is calculated according to the model presented in [43]. The decay of *VBES* versus the remaining capacity is shown in Figure 3. This study assumes that the battery is still in its first life, which ends at 80% remaining capacity, and that the value at the start of second-life *V*2*nd BES* equals 50% of a new BES: *V*2*nd BES* = 0.5*Vnew BES* [43]. *VBES* can than be calculated according to the formula presented in Equation (2). The costs are then equal to the difference between a new BES and the degraded BES as shown in Equation (3).

$$V\_{BES} = \frac{(V\_{BES}^{2 \text{nd}} - V\_{BES}^{\text{new}})}{0.2} \Delta E\_{BES}^{tot} + V\_{BES}^{\text{new}} \tag{2}$$

$$C\_{BES} = V\_{BES}^{\text{new}} E\_{BES}^{\text{max}} - V\_{BES} \left( E\_{BES}^{\text{max}} - \Delta E\_{BES}^{\text{tot}} \right) \tag{3}$$

**Figure 3.** Model of remaining value per kWh as presented in [43]: Here, *V*2*nd BES*, *Ven<sup>d</sup> BES*, *C*2*nd BES*, and *Cen<sup>d</sup> BES* represent the value at the start of second life, value at the end of its lifetime, capacity at the start of second life, and capacity at the end of lifetime, respectively.

The costs related to the EV are related to the degradation costs of charging and V2G. A similar notation of variables as for the BES is used. Here, the degradation due to driving is not taken into account, as it is not under the control of the smart charging system. Furthermore, it is assumed that the 2nd life of an electric vehicle battery starts at 80% remaining capacity at 50% of its original value per kWh [43]. This is described in Equations (4) and (5).

$$V\_{EV} = \frac{(V\_{EV}^{2nd} - V\_{EV}^{new})}{0.2} \Delta E\_{EV}^{tot} + V\_{EV}^{new} \tag{4}$$

$$C\_{EV} = V\_{EV}^{\text{new}} E\_{EV}^{\text{max}} - V\_{EV} \left( E\_{EV}^{\text{max}} - \Delta E\_{EV}^{\text{tot}} \right) \tag{5}$$

In this study, the cost of PV energy is not assumed to be zero, to take into account the installation and investment costs of the PV system, or to simulate a contractual power purchase agreement. These costs are levelled per kWh to make them independent of the simulation time, without neglecting the related costs. Here, *λPV* = 0.03 e/kWh [12] is assumed. The costs for PV energy are then determined according to the following:

$$\mathcal{C}\_{PV} = \sum\_{t=1}^{T} P\_{PV} \Delta t \lambda\_{PV} \tag{6}$$

Here, *PPV*(*t*) is the generated PV power, Δ*t* is the simulation timestep, and *λPV* is the levelized cost of PV energy. The next part of the objective function is the revenue obtained by acting as a frequency containment reserve (FCR) and demand-side management. Here, it is assumed that an SGO acts as an aggregator and mediator between the frequency regulation market and the prosumer, aggregating several flexible systems such that the combined power meets the minimum power requirements for the FCR market. Here, the revenue is obtained by reserving a part of the available power capacity for primary frequency regulation. The up/downregulation prices are *<sup>λ</sup>upandλdn*, respectively. Based on these prices, the operational costs, and the current demand, the optimization will determine how much of the available capacity will be reserved for frequency regulation. The revenue obtained from this is calculated according to the following:

$$\mathbb{C}\_{\text{reg}} = (1 - \epsilon\_{f\varepsilon}) \eta\_{\text{inv}} \eta\_{\text{ch}} \sum\_{t=1}^{T} \left( P\_{\text{reg}}^{\text{up}}(t) \lambda\_{\text{up}}(t) + P\_{\text{reg}}^{\text{down}}(t) \lambda\_{\text{down}}(t) \right) + \mathbb{C}\_{\text{comp}} \tag{7}$$

Here, *f c* is the maximum forecasting error; this is taken into account to limit the error between actual and reserved capacity caused by forecasting errors. Due to the moving horizon control, a good short-term accuracy can be achieved. However, as errors will still occur, compensation factor *Ccomp* is introduced such that any difference in revenue caused by differences in actual and reserved capacity or in scheduled and used capacity is compensated using *Ccomp*. This makes *Ccomp* dependent on the bidding process in the regulation market. Therefore, it is assumed that the smart grid operator calculates this compensation factor based on the actual operation. Additionally, *Ccomp* could be the compensation obtained for curtailing PV power as part of demand-side management. Here, *Ccomp* could be equal to the curtailed energy multiplied by the energy price. Next, *ηinv*/*ch* are the efficiencies of the inverter and BES/EV charger, respectively. Finally, *Pup*/*dwn reg* (*t*) are the reserved capacities for regulation. The calculation of these capacities will be explained later in Section 4.2.2.6. The final part of the objective function is the grid energy cost. In this study, a dynamic pricing tariff is used, where the selling price *<sup>λ</sup>sell*(*t*) is 10% lower than the buying price *<sup>λ</sup>buy*(*t*). This is done to simulate a future environment where electricity prices are a function of demand and supply, optimizing costs and separating buying/selling grid energy results in energy being taken out of the grid when demand and price are low while the energy fed back to the grid is fed in during a time of high demand and high price. The calculation of grid energy costs is shown in Equation (8).

$$\mathbf{C}\_{grid} = \sum\_{t=1}^{T} P\_{grid}^{buy}(t) \Delta t \lambda\_{buy}(t) - \sum\_{t=1}^{T} P\_{grid}^{sell} \Delta t \lambda\_{selll}(t) \tag{8}$$

Here, *Pbuy*/*sell grid* (*t*) are the powers drawn and fed from the grid, respectively.
