ESP32 and moisture sensor transistor switch
Your proposed setup will not work, but the second (corrected) one will. Here are some issues in the first setup:

Your NPN transistor is upside down. The emitter needs to be connected to GND and the collector to the GND pin of the sensor (the current path is from the sensor to GND).
You can't put the LED and the resistor in the GND supply path of the sensor. They will prevent the sensor from getting the needed supply voltage between the Vcc and GND pins.

The solution would be to put the BC547 in the right way, remove the LED and 220 Ohm resistor in the GND path and put a resistor of around 2.2K between D18 and the base of the transistor. The 2.2K resistor is for limiting the base current from the GPIO pin while assuring enough current can flow through the transistor.
This way, your transistor can switch the GND current to the sensor on and off freely. If you need an indicator, you can use a different GPIO pin, and LED and a resistor to provide it.



simulate this circuit &ndash; Schematic created using CircuitLab