Moisture sensors show random spikes

I humbly ask for assistance as I am unable to find the solution myself.

In short, I made a system to track moisture level of my salad’s soil.

I have two capacitive moisture sensors v1.2 that are connected NO and NC terminals of a relay. COM is connected to A0 of my NodeMCU, which streams readings to Blynk App.

I use Nano as power supply for the sensors and the relay. The relay coil is energized by D7 of NodeMCU sending volts to the base of NPN transistor.

Attached is the function that takes the analog reading from the sensor that is on the NC, then triggers the relay, takes reading from NO.

The problem: once in a while a have these reading spikes on both sensors. They can be seen on the graph.

I am unable to figure out the source of those spikes...

Rough Code:
void function(){
int sensor=analogRead(A0);
Blynk.virtualWrite(V0,1023-sensor);
delay(900);
digitalWrite(D7,HIGH);
delay(900);
int sensor2=analogRead(A0);
Blynk.virtualWrite(V1,1023-sensor2);
digitalWrite(D7,LOW);

This will be EMI, fridge turning on/off, local heavy industry, lightning in the distance, stuff like that.

All you need to do is take several readings in a row and take the median. Median - Wikipedia

Thanks a lot! I can totally do that!

Can anything fix it? Like adding a diode or something?

A capacitor maybe. For lack of schematics that's as specific I can be.

Software filtering is easier.