In the context of escalating concerns about environmental sustainability in smart cities, solar power and other renewable energy sources have emerged as pivotal players in the global effort to curtail greenhouse gas emissions and combat climate change. The precise prediction of solar power generation holds a critical role in the seamless integration and effective management of renewable energy systems within microgrids. This research delves into a comparative analysis of two machine learning models, specifically the Light Gradient Boosting Machine (LGBM) and K Nearest Neighbors (KNN), with the objective of forecasting solar power generation in microgrid applications. The study meticulously evaluates these models’ accuracy, reliability, training times, and memory usage, providing detailed experimental insights into optimizing solar energy utilization and driving environmental sustainability forward. The comparison between the LGBM and KNN models reveals significant performance differences. The LGBM model demonstrates superior accuracy with an R-squared of 0.84 compared to KNN’s 0.77, along with lower Root Mean Squared Error (RMSE: 5.77 vs. 6.93) and Mean Absolute Error (MAE: 3.93 vs. 4.34). However, the LGBM model requires longer training times (120 s vs. 90 s) and higher memory usage (500 MB vs. 300 MB). Despite these computational differences, the LGBM model exhibits stability across diverse time frames and seasons, showing robustness in handling outliers. These findings underscore its suitability for microgrid applications, offering enhanced energy management strategies crucial for advancing environmental sustainability. This research provides essential insights into sustainable practices and lays the foundation for a cleaner energy future, emphasizing the importance of accurate solar power forecasting in microgrid planning and operation.