In this paper we propose two novel coalitional game theory based optimization methods for minimizing the cost of electricity consumed by households from a smart community. Some households in the community may own renewable energy systems (RESs) conjoined with energy storing systems (ESSs). Some other residences own ESSs only, while the remaining households are simple energy consumers. We first propose a coalitional cost optimization method in which RESs and ESSs owners exchange energy and share their renewable energy and storage spaces. We show that by participating in the proposed game these households may considerably reduce their costs in comparison to performing individual cost optimization. We further propose another coalitional optimization model in which RESs and ESSs owning households not only share their resources, but also sell energy to simple energy consuming households. We show that through this energy trade the RESs and ESSs owners can further reduce their costs, while the simple energy consumers also gain cost savings. The monetary revenues gained by the coalition are distributed among its members according to the Shapley value. Simulation examples show that the proposed coalitional optimization methods may reduce the electricity costs for the RESs and ESSs owning households by 20%, while the sole energy consumers may reduce their costs by 5%.