To combat cyber threats in the smart grid, an intrusion detection system can be integrated into the advanced metering infrastructure. Anomaly-based intrusion detection can detect even the tiniest changes in the parameter under investigation, whereas signature-based intrusion detection only recognises known attacks. The growing usage of smart grids necessitates the classification, identification, and implementation of countermeasures to threats. At the absolute least, smart grids must be protected against cyberattacks; thus, the highest level of information security must be offered. As a result of digitisation and the usage of more smart applications, the research looked at a variety of attack types, smart grid assaults, and major cyber threats on the voltage regulation. Machine learning techniques that analyse data in real time and formulate patterns to recognise an attack and scan through huge data for anomalies can be implemented into the advanced metering infrastructure (AMI) for intrusion detection for anomaly-based intrusion detection. The comparative test study done for the research found that the proposed method, median absolute deviation for anomaly identification in smart metering datasets, produced the most accurate and precise differentiations with the highest accuracy and precision. The median absolute deviation (MAD) algorithm model is trained using test data, and raw predictions are made, before true data are used to derive final test result parameters, precision, recall, and F1 scores. The methodology of the entire study is discussed in this paper, as well as how the MAD algorithm is best suited for anomaly-based intrusion detection, as well as result comparisons of other machine learning algorithms.