The increasing price of and demand for energy have prompted several organizations to develop intelligent strategies for energy tracking, control, and conservation. Demand side management is a critical strategy for averting substantial supply disruptions and improving energy efficiency. A vital part of demand side management is a smart energy management system that can aid in cutting expenditures while still satisfying energy needs; produce customers’ energy consumption patterns; and react to energy-saving algorithms and directives. The Internet of Things is an emerging technology that can be employed to effectively manage energy usage in industrial, commercial, and residential sectors in the smart environment. This paper presents a smart energy management system for smart environments that integrates the Energy Controller and IoT middleware module for efficient demand side management. Each device is connected to an energy controller, which is the inculcation of numerous sensors and actuators with an IoT object, collects the data of energy consumption from each smart device through various time-slots that are designed to optimize the energy consumption of air conditioning systems based on ambient temperature conditions and operational dynamics of buildings and then communicate it to a centralized middleware module (cloud server) for management, processing, and further analysis. Since air conditioning systems contribute more than 50% of the electricity consumption in Pakistan, for validation of the proposed system, the air conditioning units have been taken as a proof of concept. The presented approach offers several advantages over traditional controllers by leveraging real-time monitoring, advanced algorithms, and user-friendly interfaces. The evaluation process involves comparing electricity consumption before and after the installation of the SEMS. The proposed system is tested and implemented in four buildings. The results demonstrate significant energy savings ranging from 15% to 49% and highlight the significant benefits of the system. The smart energy management system offers real-time monitoring, better control over the air conditioning systems, cost savings, environmental benefits, and longer equipment life. The ultimate goal is to provide a practical solution for reducing energy consumption in buildings, which can contribute to sustainable and efficient use of energy resources and goes beyond simpler controllers to address the specific needs of energy management in buildings.