Sensor networks are one important building block towards the realisation of context-aware applications. Suitable communication protocols and middleware solutions are necessary to facilitate the development of sensor network applications. Additionally, the vast number of nodes in sensor networks necessitates a new programming model for application developers. Commonly used programming abstractions force the programmer to express the global behaviour of a network in terms of local actions taken at individual nodes. We argue that global programming abstractions are needed to express global behaviour of a network. We present RuleCaster, a novel state-based programming framework for sensor network applications. RuleCaster provides a high-level language for application definition, a compiler that splits the high-level specification into individual tasks and assigns them to the nodes, and a service-based middleware that provides the interface for collaborative execution of these tasks.