How to trigger automation at MacOS shutdown/logout?

I have studio monitors connected to USB bus-powered audio interface on my Mac Mini. Whenever the computer reboots or shuts down, it powers off the interface and sends a horribly loud “POP” through the speakers.

I’d like to create a script to run on the Mac that can turn off an outlet before the USB port powers down the interface. What’s the best way to approach this? I’m not a skilled coder, but I can usually get going once I know where to start digging. Is this an AppleScript thing? I just need to know how to trigger it and send a command to home assistant.

Thanks for any tips.