you were on the right track...
I added a debug text box in the top left corner of the screen - click any device and its details will be listed. This comes in handy when checking how much power a generator has left...
I started with the "Every One Second" condition first, because it isn't necessary to calculate the number of devices every tick - only when the devices actually need power.
if the device is on and needs power, then we go looking for generators it is attached to. If they are on and have power, then we try to take it.
if there are multiple generators, then we divide what we need between them.
if any don't have enough to supply their portion, then we try to get it from another generator.
if the device can't get enough power, then we turn it off.
(if a device is off, and you click it, turn it back on. if it can't get power it goes off again).