Optocouplers giving erratic values

Pin 3 is an Emitter and it should go to GND.
Pin 4 is a Collector and it should go to ESP input.
Pin 1 (the anode) should be more positive than Pin 4 (the cathode).

If you rotate the opto symbol vertically 180 degrees (without changing anything else), it will be correct (except for the missing pullups, which you can fix with Input Pullup config in setup.)

That's it.
I'm out.
Good Luck.