This paper proposes a plan to manage energy consumption in residential areas using the demand response method, which allows electricity users to contribute to the reliability of the power system by controlling their usage. Due to the growing population, the residential sector consumes a significant amount of energy, and the objectives of this study are to lower electricity costs and the peak to average ratio, as well as reduce the amount of imported electricity from the grid. The study aims to maximize profit by properly utilizing renewable energy sources and addressing energy trading. The manta ray foraging optimization (MRFO) and long term memory MRFO (LMMRFO) algorithms are used to solve this problem. Firstly, the validation of the proposed LMMRFO technique is confirmed by seven benchmark functions and compared its results with the results of the well-known optimization algorithms including hunter prey optimization, gorilla troops optimizer, beluga whale optimization, and the original MRFO algorithm. Then, the performance of the LMMRFO is checked on the optimization of smart home energy management. In the suggested approach, a smart home decides whether to purchase or sell electricity from the commercial grid based on the cost, demand, and production of electricity from its own microgrid, which consists of a wind turbine and solar panels. Energy storage systems support the stable and dependable functioning of the power system since the solar panel and wind turbine only occasionally produce electricity. Through various case studies, the proposed plan is tested and found to be effective in reducing electricity costs and the peak to average ratio while maximizing profit. Furthermore, a comparative study is conducted to demonstrate the legality and effectiveness of LMMRFO and MRFO.