With the increase in local energy generation from Renewable Energy Sources (RESs), the concept of decentralized peer-to-peer Local Energy Market (LEM) is becoming popular. In this paper, a blockchain-based LEM is investigated, where consumers and prosumers in a small community trade energy without the need for a third party. In the proposed model, a Home Energy Management (HEM) system and demurrage mechanism are introduced, which allow both the prosumers and consumers to optimize their energy consumption and to minimize electricity costs. This method also allows end-users to shift their load to off-peak hours and to use cheap energy from the LEM. The proposed solution shows how energy consumption and electricity cost are optimized using HEM and demurrage mechanism. It also provides economic benefits at both the community and end-user levels and provides sufficient energy to the LEM. The simulation results show that electricity cost is reduced up to 44.73% and 28.55% when the scheduling algorithm is applied using the Critical Peak Price (CPP) and Real-Time Price (RTP) schemes, respectively. Similarly, 65.15% and 35.09% of costs are reduced when CPP and RTP are applied with demurrage mechanism. Moreover, 51.80% and 44.37% electricity costs reduction is observed when CPP and RTP are used with both demurrage and scheduling algorithm. We also carried out security vulnerability analysis to ensure that our energy trading smart contract is secure and bug-free against the common vulnerabilities and attacks.