A method for setting a security level of wireless sensors (WS) communicating with a switch, the method comprising the following steps in a security device (SD) linked to the switch (SW): collecting (S1) data frames sent from the wireless sensors to the switch and creating a dataset containing the collected data frames, identifying (S2) patterns associated with the wireless sensors from the collected data frames, each pattern being based on a periodic time interval for sending two consecutive data frames by a wireless sensor or based on a number of data frames sent by a wireless sensor during a time interval of usage, introducing (S3) simulated traffic anomalies in the dataset with respect to the traffic patterns, randomizing (S4) the dataset and dividing the randomized dataset in a training dataset and in a testing dataset, wherein the training dataset contains more data frames than the testing dataset, training (S5), by using the training dataset, a machine learning model configured for detecting traffic anomalies, and validating the machine learning model if the trained machine learning model reaches a threshold of accuracy with the testing dataset, characterized in that said method further comprises the steps of: detecting (S6) a traffic anomaly for a given wireless sensor during operation of the wireless sensors by analyzing current data frames sent from the wireless sensors to the switch and by using the validated machine learning model, triggering (S7) a security alert based on the detected traffic anomaly, adapting (S7) a security level for at least the given wireless sensor based on the triggered security alert. collecting (S1) data frames sent from the wireless sensors to the switch and creating a dataset containing the collected data frames, identifying (S2) patterns associated with the wireless sensors from the collected data frames, each pattern being based on a periodic time interval for sending two consecutive data frames by a wireless sensor or based on a number of data frames sent by a wireless sensor during a time interval of usage, introducing (S3) simulated traffic anomalies in the dataset with respect to the traffic patterns, randomizing (S4) the dataset and dividing the randomized dataset in a training dataset and in a testing dataset, wherein the training dataset contains more data frames than the testing dataset, training (S5), by using the training dataset, a machine learning model configured for detecting traffic anomalies, and validating the machine learning model if the trained machine learning model reaches a threshold of accuracy with the testing dataset, characterized in that said method further comprises the steps of: detecting (S6) a traffic anomaly for a given wireless sensor during operation of the wireless sensors by analyzing current data frames sent from the wireless sensors to the switch and by using the validated machine learning model, triggering (S7) a security alert based on the detected traffic anomaly, adapting (S7) a security level for at least the given wireless sensor based on the triggered security alert. detecting (S6) a traffic anomaly for a given wireless sensor during operation of the wireless sensors by analyzing current data frames sent from the wireless sensors to the switch and by using the validated machine learning model, triggering (S7) a security alert based on the detected traffic anomaly, adapting (S7) a security level for at least the given wireless sensor based on the triggered security alert. <EOS>