Yeah, if the hero has the skill to control a ballista, you can enable it to automatic control in the battle settings menu. But in our case, the battle isn't being fought in auto mode :(.
Therefore, there are still 2 options.
Regarding the guarantee that the enemy monster unit won't rush to the additional unit, and then won't attack the ballista itself - you are right. But in the example we are considering, the ballista and the peasant are very far apart; the zombies will only hunt the fairy.
Because in the attached file, I only found this: Ballista.h3m
I have some ideas for improving the program's algorithm, but it probably won't be possible to do it in 10 lines, but it will work and probably take into account the morale of your own unit. Are you interested? Should I describe the idea here?