Testing PLC Logic using a Digital Twin
Summary: A digital twin can act as a virtual commissioning environment for energy system controllers. By connecting battery control logic, PLC software or supervisory control algorithms to a simulation model, engineers can test system behavior before deployment. This makes it possible to reproduce operating conditions, evaluate edge cases, detect control errors and improve the strategy before the real system is affected.

Modern energy systems are increasingly controlled by software. A battery controller, for example, may decide when to charge from local PV production, when to discharge to supply consumers, when to support a heat pump and when to protect the battery from operating outside its limits.
Testing this behavior directly on the real system is possible, but often inefficient and risky. Some scenarios occur only under specific weather or load conditions, while others should not be provoked on a real installation at all. This dynamic simulation model of the energy system can provide a virtual test environment where the controller can be developed, tested and improved before deployment.
Using a simulation model created via the Green City Simulation Library, a controller can be tested against the simulated systems before it is connected to the real installation. The simulation provides virtual sensor signals, such as PV power, consumer demand, the battery state of charge or external values like the current spot market electricity price.
The controller has to balance several objectives. During periods of high PV production and low local demand, the battery should charge instead of exporting all surplus electricity to the grid.
During periods of high demand and low PV production, the battery can discharge to reduce grid import. At the same time, the controller must respect technical constraints such as minimum and maximum state of charge, charging power limits, discharging power limits and battery protection rules.

This creates a closed-loop test environment: the simulated system influences the controller, and the controller influences the simulated system. The digital twin not only calculates system performance, it allows the control software to interact with the system model dynamically. Depending on the project, the connection to the controller can be implemented via an interface such as OPC UA, Modbus, MQTT, FMU or a dedicated co-simulation/HIL interface that can be implemented in Python or other languages.
A Digital Twin coupled to a battery controller allows the testing of several edge cases such as:
- Battery switching rapidly between charging and discharging during cloudy periods.
- Controller discharging the battery shortly before a predicted PV surplus.
- Heat pump operation causing unexpected grid peaks.
- Battery reaching the lower SOC limit too early in the evening.
- Controller ignoring a set limit because of a signal scaling error.
- PLC logic using the wrong sign convention for battery power.
- A delay in measurement signals causing unstable control behavior.
In a real system, these behaviors increase component stress and can reduce efficiency. In the digital twin, this can be detected early and corrected by adding a hysteresis, minimum switching time or by filtering a reference signal.
Another powerful application for this approach is testing how the system responds to worst-case critical failures. This type of information cannot be safely generated from a live system, making simulation an invaluable tool for understanding failure scenarios before they occur. It also provides the opportunity to build and validate robust fallback strategies, safety functions and recovery procedures in a controlled environment.
Depending on the project phase, the same digital twin can support different testing levels. In early design, the control strategy can be tested directly inside the simulation environment. It can visualize the resulting costs of different strategies. This is shown in the reduction of the annual electricity costs, shown below. It compares an advanced logic controller with a baseline strategy.
It was possible to reduce the energy costs by about 8% compared to the simple rule-based baseline operating mode, while keeping battery SOC and power limits within the defined constraints. This can be expanded to later use
Later in a projects life, the implemented control logic can be connected to the model in a software-in-the-loop setup and catch logic errors and evaluate new code behavior.

It even possible to build a hardware-in-the-loop setup, where the real PLC exchanges signals with the simulated plant before it is send to the physical installation. This makes it possible to test signal mapping, scaling, timing behavior and edge cases in real time conditions.
Both options are possible using the presented model. For hardware-in-the-loop testing, the model usually has to exchange signals with the PLC at a defined cycle time, so timing, signal mapping and scaling become an integral part of the test setup.
The value of the digital twin is thus not limited to calculating annual energy balances, it creates a test environment where software behavior can be evaluated before it affects real equipment. In these systems, small control decisions can have a large impact on grid exchange, battery cycling, peak loads and overall system efficiency.
For complex energy systems, the digital twin becomes more than a planning tool: it becomes a practical test bench for control development and commissioning. The capabilities range from the battery controller shown to heating and cooling production controllers, as well as comprehensive energy management system control.