Hysteresis Op Amp weird behaviour (LM358p)

Alas you chose the wrong configuration which heavily loaded the voltage you were trying to sense.

You want to observe the voltage at the junction of the photodiode and 1M resistor? The you need an impedance much higher than 1M (not 200 ohms!).

You need a true rail-to-rail device for this, be it a comparator or an opamp, if you want to work right up against the +ve rail.

Make life easy and work down near ground - far more devices go to the -ve rail than to the +ve rail. In particular the LM358 should work close to its negative rail.

So redo the divider: photodiode to ground, 1M to 5V.

The way to wire this is inverting input sensing the photodiode, non-inverting input to the voltage reference (which would be 0.9V with things the other way up), adding hysteresis in that part of the circuit where it doesn't load the diode divider.