The story
The idea is very old. A 12th-century Spanish story collection, the Disciplina Clericalis, tells of a sleepy storyteller who spins a tale about ferrying 2,000 sheep across a river two at a time, and nods off after the first pair. In English, an 1854 Seba Smith character tries “to think of sheep jumping over a wall,” and “counting sheep” turns up in print by 1890.