The fourth and fifth industrial revolutions (Industry 4.0 and Industry 5.0) have driven significant advances in digitalization and integration of advanced technologies, emphasizing the need for sustainable solutions. Smart Energy Systems (SESs) have emerged as crucial tools for addressing climate change, integrating smart grids and smart homes/buildings to improve energy infrastructure. To achieve a robust and sustainable SES, stakeholders must collaborate efficiently through an energy management framework based on the Internet of Things (IoT). Demand Response (DR) is key to balancing energy demands and costs. This research proposes an edge-based automation cloud solution, utilizing Eclipse Arrowhead local clouds, which are based on Service-Oriented Architecture that promotes the integration of stakeholders. This novel solution guarantees secure, low-latency communication among various smart home and industrial IoT technologies. The study also introduces a theoretical framework that employs AI at the edge to create environment profiles for smart buildings, optimizing DR and ensuring human comfort. By focusing on room-level optimization, the research aims to improve the overall efficiency of SESs and foster sustainable energy practices.