A sleeping macaw does not balance by willpower. The branch itself triggers a built in clamp. When the bird settles, its body weight bends the leg joints. That bend pulls on flexor tendons that run from muscle to claw. The more the leg folds, the tighter the toes curl around the wood.
This arrangement, known as the perching reflex, is a passive mechanical system rather than an active muscular effort. Once engaged, the tendon lock keeps the foot in flexion with almost no additional energy expenditure or neural input. Even when the brain enters deep sleep and cortical activity drops, the closed loop of bone, tendon and claw holds steady.
Macaws also sleep with their center of gravity positioned low and close to the branch, which reduces tipping torque if the wind moves the canopy. Many individuals rest while tucking one foot or even the beak into feathers, but the supporting foot stays in that tendon based clamp. The system only releases when the bird straightens the leg, relaxing tension in the flexor tendons and opening the toes.