Having valve open until condition is met

It looks to me as though the code you provided would do the job if you put it in the loop function. I expect that you would need to change the value you expect from the analogRead to match what the sensor actually returns at 2 bar.