5V Relay and ESPHome

Hi,
I have the exact same relay and using the D1 Mini. If relay is connected to 5V the D1 output pin will always trigger relay. If I connect relay to 3.3V VCC, then it will work as normally with the exact same code as you use here. But when connecting VCC to 5V it will just trigger the relay all the time.
If I change the code to be pin D3 and put the wire there, then it will stay low and not trigger. But as soon as the code uploads it will be forced high, and inverting the pin als does nothing. Why is this?