iMac randomly shuts down

Chances are this is a bad power supply board.

enter image description here

Shutdown error codes can be negative or positive values; negative being hardware and positive being software.

A "0" code is right there in the middle where it usually means a sudden loss of power. See this post for more info: Are OS X shutdown cause and sleep cause numbers listed/explained anywhere?

If any part of your logic board was beginning to fail, it would generate kernel panics which would be in the log and likewise, if you had software causing issues, there would be a log of a crash somewhere.

I've see this previously on other platforms whether it was a Mac, or a PC, or even enterprise grade servers. What is probably happening is that a component in your power supply is getting too hot and consequently shutting down. If you take it to the Apple store, they probably have the ambient temperature much lower than you home so it never has a chance to over heat. Just when you think everything is good, the symptoms return.

Is your downstairs location warmer or cooler than the upstairs location where it normally resides?

If you need to replace your power supply, rest assured these are fairly easy to come by and fairly inexpensive. At the time of this post, they were trending around $65. It's also very easy to replace (see the guide at ifixit.com)

While you are in there, I would swap out your (spinning) HDD with a SSD. I just installed a Samsung 850 EVO SSD in my 27" iMac and it is substantially quieter and cooler than before.

Disclaimer: There still is a chance it's the logic board, but it's highly unlikely. Before you do any expensive logic board replacements, try replacing the power supply first.

One more thing....I would definately follow agentroadkill's advice from his comment and put a UPS (battery backup) between the outlet and your Mac.