## 2 Materials and Methods
### Generalised Dynamic System Model
This was chosen to be 15 min, which is a common billing period in energy economics, and the MPC prediction horizon was chosen to be 48 h, resulting in \(N=192\) timesteps, allowing a reasonably foresighted microgrid scheduling. The equality constraints in Equations (6)-(10) express the following system interrelationships: Energy conservation is required at the bus (main node) at all timesteps and includes all summarised incoming and outgoing powers. Moreover, there is energy conservation in all BESSs \(\beta\in B\) and EVs \(\gamma\in\Gamma\), where \(\eta_{\beta,\text{ch}}\) and \(\eta_{\gamma,\text{ch}}\) are the charging efficiencies and \(\eta_{\beta,\text{dc}}\) and \(\eta_{\gamma,\text{dc}}\) are the discharging efficiencies, which are simplified to be constant and independent of cell temperature, power, etc. The generated power \(P_{a}^{k}\) of every PV plant \(\alpha\in A\) is assumed to equal a plant-specific PV power forecast \(P_{a,t}^{k}\). The power demand of the aggregated loads in the production facility \(P_{1}^{k}\) is assumed to equal a load power forecast \(P_{1,t}^{k}\). In this generalised model, devices of the category BESS, EV, or PV can be modularly added to the system of equations. By adding devices to the microgrid, corresponding terms are added to the summary powers in Equation (6) and sets of \(N\) equations of type (7)-(9) are added to the equality constraints. The same principle applies to the following inequalities and bounds. Feed-in from BESSs or EVs into the utility grid is typically regulated by region-specific rules. In Germany, grid regulation allows compensated feed-in from storage units into the utility grid only if these components are exclusively charged from on-site renewable generation [14]. However, this makes the exploitation of dynamic grid electricity pricing with these storage units impossible. To exploit time-variable energy tariffs by predictively charging BESSs and EVs and to satisfy the abovementioned grid regulation, feed-in from BESS or EV into the utility grid must therefore be prohibited by the inequality constraint. \[P_{A}=\sum_{\alpha\in A}P_{\alpha}. \tag{1}\]4  
The EU Network Code Requirements for Generators (RfG) [15], which is the underlying European regulation, is not specific on this topic and leaves room for individual regulatory design in the member states, such that the inequality of Equation (13) is not directly applicable to other European member states. However, similar national regulations have been passed in other states. In order not to charge the EVs from the BESSs, the inequality constraint applies. Further constraints result from the limited rated power of the components and cabling. \[P_{A}=\sum_{\alpha\in A}P_{\alpha}. \tag{1}\]5\[P_{A}=\sum_{\alpha\in A}P_{\alpha}. \tag{1}\]6  
The energy contents of the BESSs with the capacities \(C_{\beta}\) and the EVs with the capacities \(C_{\gamma}\) are limited with their respective minimum and maximum permitted state of charge (SOC). \[P_{A}=\sum_{\alpha\in A}P_{\alpha}. \tag{1}\]7 \[P_{A}=\sum_{\alpha\in A}P_{\alpha}. \tag{1}\]8  
The SOC operating window can be a subset of \([0,1]\), in order to increase battery life [16] or to guarantee minimum battery uptime for uninterruptible power supply during grid faults. The vehicle's feed into the local grid can be (temporarily or constantly) limited by the inequality constraint.