A method for setting a security level of wireless sensors communicating with a switch, the method comprising the following steps in a security device linked to the switch: collecting data frames sent from the wireless sensors to the switch and creating a dataset containing the collected data frames, identifying 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 simulated traffic anomalies in the dataset with respect to the traffic patterns, randomizing 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, 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 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 a security alert based on the detected traffic anomaly, adapting a security level for at least the given wireless sensor based on the triggered security alert. collecting data frames sent from the wireless sensors to the switch and creating a dataset containing the collected data frames, identifying 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 simulated traffic anomalies in the dataset with respect to the traffic patterns, randomizing 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, 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 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 a security alert based on the detected traffic anomaly, adapting a security level for at least the given wireless sensor based on the triggered security alert. detecting 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 a security alert based on the detected traffic anomaly, adapting a security level for at least the given wireless sensor based on the triggered security alert.