In this paper we present Levels, a programming abstraction for energy-aware sensor network applications. Unlike most previous work it does not try to maximize network lifetime but rather helps to meet user-defined lifetime goals while maximizing application quality. Levels is targeted to applications where there is no redundancy and no node should fail early. With our programming abstraction the application developer defines so-called energy levels. These energy levels form a stack and can be deactivated from top to bottom if the lifetime goal cannot be met otherwise. Each code block within an energy level contains information about its energy consumption, which can be obtained from simulation tools without much effort. The runtime system then uses the data about the energy consumption of the different levels to compute an optimal level assignment for the time remaining. As we show in the evaluation, applications using Levels can accurately meet given lifetime goals and offer good application quality. In addition, the runtime overhead of our system is almost negligible.