Switch that change its state with a signal of some seconds
How is your ESP8266 powered? Via the sensor? How far is the sensor from the ESP8266? How is the sensor powered?

It is not a good practice to power the micro controller through the sensor.

A good approach to this is to put your ESP8266 into "power saving mode" and let the signal from the sensor "wake-up" the ESP8266 via interrupts. this video explains on how this can be done

Edit:
 You could use a feature of the 555 timer IC along with a transistor/relay. called the 555 bistable mode connect the Trigger pin to your sensor and the reset pin to the ESP.