This episode would have felt a little less contrived had it been established a few episodes before that Data was trying out a sleep subroutine. Maybe could have used its reference as a minor device in other ways, and then by this episode there's no need to develop the idea, you just use it as a method of detection for the invasion. And then the convenient timing is removed. Data's small talk or relationship routines are other examples. The emotion chip was done this way, being very important in the movies.
An example where they did to this. Riker and Minuet, as well as his trombone playing. Or Picard and the flute, or his detective hologram programs.