Energy management for smart homes/grids