Here are 2 ways:
1. Set Output On Extrude, M571. This is suitable if you want to open the valve during extruding but not during retraction or on-printing moved.
2. P parameter on G1 commands. This can control the valve however you want, but you need to generate GCode that includes the correct P parameters.