Looking at the manual for the Magic Trackpad 2, the issue seems to be that (unlike most devices) it only supports pairing with one Mac at a time:
After you pair your Magic Trackpad 2 with a Mac, you can pair it again with a different Mac. To do this, remove the existing pairing and then pair the trackpad again using the setup instructions in the previous section.
Because of this, I’m not sure there’s much you can do to fix this via software. It would probably be easiest to temporarily plug it into the other Mac via Lightning to make it connect to that Mac.
What’s the other Bluetooth device that you’re having trouble with?
You should be a able to do this with blueutil and a script like:
#!/bin/sh
blueutil --power 0