How to make inverse switch with bc547?
simulate this circuit &ndash; Schematic created using CircuitLab

Figure 1. (a) The diode version. (b) The BC548 version, if that's all you've got.

Either of these solutions should work if if \$ DC_{MAINS+} &gt; V_{BAT} \$.