Electrical energy use at an agency that results in waste is a severe concern. One of them is in the toilet, which is often a waste of energy due to the lights being on while the toilet is not in use. As a result, energy waste is proportionate to an agency's monthly operating expenses. If waste can be eliminated, additional expenses will also be lowered. This study provides an algorithm that begins with the user pressing the push button to turn on the light and ends with the activated motion sensor. When a user enters the toilet and motion is detected, the toilet light remains on automatically. If, however, the user is not on the toilet after hitting the push button, the toilet light will automatically switch off after 60 seconds. Similarly, if the user exits the toilet, the toilet light will switch off automatically after roughly 60 seconds. The findings of a test conducted in a toilet for around two weeks using a motion sensor whose algorithm was optimized revealed a 7.08 per cent reduction in electric power usage after utilizing the equipment. Then, the device's test results indicate that the motion sensor detection is 100 per cent successful.