Any way to detect optimal Wi-Fi channel settings in a script?

Hello, I'm developing an setup utility to automatically configure several of my OpenWrt routers for my need.

I'm currently meddling with the wireless subsystem settings and wonder whether there's a way to determine a (subjectively) optimal Wi-Fi channel settings at the current environment at the moment(like, for 2.4GHz radio use X channel and for 5GHz radio use Y channel) without manual configuration, is it possible?

Setting wireless.${radio_device}.channel to auto doesn't seem to be a solution that works across products...

Amen! I'd go a step further and say I've never (ever) seen any device set to auto select an empty channel. Invariably any device I have set to auto chooses not just busy channels, but the busiest channel in the area. And when I manually set a channel, I scan again a week later and find that all the other neighbourhood devices that are still on auto have followed me to the previously empty space they avoided before.

Fantastic. This would be a great benefit.

iw scan [device] will produce a lot of output on each SSID that it sees. It is this same output that LuCI parses when you click scan there. With some work, you should be able to parse its output. It won't be easy - you'll have to interepret channel widths and capabilities to determine the channel blocks each SSID are using.