Trigger a macro when exiting a specific sub window

I'm playing catchup here, so forgive me if I still haven't grasped it, but is this what you mean?

That's pretty cool! Similar to Keyboard Maestro's Palettes.

Anyway, back to the task at hand...

It sounds like you only ever want one device visible at a time. Is that right? In that case, I humbly suggest you do switch profiles rather than device. Why? I'll explain...

Maybe I'm off my game today but it's actually quite hard to get a macro together that does exactly what you want, as the only available SD hotkey is a toggle; i.e. the same hotkey to show the buttons and hide them.

As it's quite likely for the visibility state of each device to get out of sync (you end up with both or the wrong one if the toggle happens at the wrong time), this presents a bit of a problem. It's not made easier by the fact that AppleScript sees the floating button windows as window 1 or window 2 (if two are on-screen) and also doesn't seem able to close/dismiss them.

The only reliable way I can think to do it is by using found images of your device buttons to determine which device(s) are visible at any time. However, I make it a personal rule never to use found images unless I absolutely have to, so I'm wondering if @Nige_S can shake something loose here?

The aforementioned problem with syncing isn't an issue when switching profiles, as only one need ever be visible. If I understood correctly and you only want one profile to show at a time, this is the way I'd go.