If you look at my screenshot, they have a home base of Dagru 4 and are set to Defend Dagru 4 and system. Thus, they should only stay in the Dagru 4 system and defend (located at the top of the screenshot). However, they continue to automatically move to a far off location for no apparent reason, leaving the system undefended.
By any chance, do you have an attack target set up for your defense fleets?
By that I mean, if you decide to switch your defense fleet to an attack fleet, do you already have an attack target set up for it or is the attack target default/cleared when you assign them the defense role?