This work is focused on the optimal management of electricity and heat generation and demand in microgrids. The objective of the proposed mathematical model is to adjust energy and heat availability profiles resulting from the use of renewable energy sources and flexible energy and heat demands. The optimisation of the resulting short-term problem is addressed through a Mixed-Integer Linear Programming (MILP) mathematical model to minimise the operational cost of the microgrid. Delays in the energy demands are allowed to tackle flexible demand profiles, under penalties in the objective function. An additional characteristic was the consideration of non-constant profiles in the considered tasks. Also, this model takes into account eventual interruptions in the tasks, applying penalties in the economic objective function. The main decisions to be made includes the schedule of tasks, as well as energy and heat generation levels, purchases from and exportation to the power grid, and storage levels.