This paper presents a method for the energy management of a set of smart homes, in which batteries, thermal storage and demand response are considered as flexibilities in order to achieve minimum operation costs. To cope with the uncertainty of load forecast, a two-stage stochastic optimization process is proposed, in which the first stage decision is the committed energy to be purchased, and the second stage returns the devices' setpoints. This methodology has been developed in the context of the Horizon 2020 project SENSIBLE as part of the tasks related to the use case, Flexibility and Demand Side Management in Market Participation. NOMENCLATURE t,q,h,i Indexes for time step, quantile (scenario), household and demand response cut , , ℎ , Number of time steps, quantiles, households and demand response cuts Power drawn from the grid at time t , ,ℎ, Demand response power at time t, scenario q, household h and cut i , ℎ / , ℎ Power charge/discharge in the battery for time t and scenario q , State of Charge of battery at time t and scenario q , ,ℎ State of Charge of Electric Water Heaters (EWH) in house h, at time t and scenario q , ,ℎ Power injected into EWH in house h, at time t, and scenario q Spot prime at time t Price of demand response program for step i , ,ℎ Electricity demand at time t, scenario q and house h Power injected by the solar panel at time t ℎ / ℎ Charging/discharging efficiency of battery R/C Thermal resistance/capacitance of the EWH Probability of scenario q