Is it a stop valve or a gate valve..
You "might" be able to get a microswitch in there thats arm is triggered by the in/out movement of the valve stem..
It would be really tight though and I have no idea how you would get it mechanically mounted in such a small area reliably.
Use a bigger valve for more room. I would think about using a reed switch with a magnet attached in place of or in addition to the washer on the valve. Round magnets are available with a hole in the center for the screw.
If you get the apart you will see a round disk moving to close or open the pipe depending on rotation direction valve knob.
Remove the disk and replace it with a micro switch or mount it out site when more space is needed.