The ability to sense and predict occupancy - i.e. to establish when the residents are and will be in a building - represents a basic requirement for the energy-efficient operation of many building automation systems. In residential households, in particular, the absence of all residents allows a heating controller to automatically lower the temperature of the home, thereby saving energy that would have been otherwise wasted on heating an empty building. However, if the home has been thus allowed to cool, a boiler and heat distribution system need a non-negligible time to reheat the home to a comfortable temperature. Therefore, to avoid a loss of comfort, a heating control system also requires a sufficiently accurate prediction of when the occupants are going to return in order to trigger the heating at the right time. Since space heating accounts for a large fraction of residential energy use (e.g. 68% in the European Union member states), heating control systems based on occupancy sensing and prediction - often referred to as smart thermostats – play an important role in reducing energy consumption and carbon dioxide emissions, while at the same time ensuring occupant comfort. The objective of this thesis is thus to investigate how the two main computational components of a smart thermostat - occupancy sensing, based on sensors that typically exist in a residential environment, as well as occupancy prediction from historical occupancy patterns - can be used to automatically reduce the energy consumption of a heating system while trying to maximise thermal comfort. Current smart thermostats require the installation of dedicated hardware to sense whether the occupants are at home or away. This increases installation and maintenance costs and thus prevents widespread adoption of such potentially energy-saving solutions. To overcome this hurdle, we investigate the suitability of opportunistically using devices already existing in households to sense occupancy. This opportunistic sensing approach seeks to utilise available devices to replace or augment dedicated infrastructures. An example are smart electricity meters, which are mandated to be installed in many households worldwide. We hypothesise that the information contained in the electrical load of the household, as measured by the smart electricity meter, can be used to infer its occupancy. To verify this hypothesis, we have performed an extensive data collection campaign over seven months in six Swiss households to collect occupancy ground truth data as well as the aggregated and device-level electrical consumption of the households. Using this data, we employ supervised machine learning algorithms to infer occupancy solely from the households’ aggregated electricity consumption. We show that such an approach yields a classification accuracy of up to 94%. As soon as the occupancy sensing infrastructure detects that residents left the house, the temperature can be allowed to drop resulting in energy savings during this setback period. However, a reactive strategy cannot be employed upon the arrival of the occupants as it may take a considerable amount of time to bring the house back to a comfortable temperature. To avoid loss of comfort, occupancy prediction algorithms are used to predict the time of arrival of the occupants to determine the right time to start pre-heating the house. To analyse the performance of such prediction approaches we have derived occupancy schedules from a large, publicly available mobile phone location dataset. Using the schedules from 45 participants we show that current state-of-the art occupancy prediction algorithms achieve an accuracy around 85%, which is close to the theoretical optimum given by the predictability of the schedules (which in practice always feature some level of irregular behaviour). The accuracy of the occupancy prediction alone does not necessarily reflect the energy savings and comfort loss that can be achieved or caused by a smart thermostat. The actual savings depend upon the occupancy schedule of the household, the prediction accuracy, the weather conditions and the physical properties of the building. The final part of this thesis thus deals with the simulation of various heating scenarios to investigate the effect of a smart thermostat on the overall energy savings under different environmental conditions. To this end, we assess the overall energy expenditure in several building scenarios. Furthermore, we develop a new methodology to accurately assess the impact of the weather conditions on the energy savings. We show that building parameters result in a range of savings from 6% to 17%, while the savings in the 25% of households with the lowest occupancy are 4-5 times higher than in the quarter with the highest occupancy. The unifying theme of this thesis is to show how current technology, which already exists in many homes, can help to save energy without sacrificing comfort. For this purpose, we draw upon recent work in the distributed systems domain to access smart electricity meters and machine learning algorithms to derive occupancy data. We show how predictable occupancy schedules are and, by providing a simulation framework to evaluate different occupancy prediction algorithms, we seek to answer the question how much energy a smart thermostat can save.