Please give folks here some time. We occasionally have to do some work for our regular jobs.
Mike's suggestion was based on the presumption that you were trying to stop the file write while it was writing. That's not what you're trying to do, so let's forget that.
There's one aspect of your requirements which seems a little odd. You say you want to initiate the file write once a valve is opened, and stop it once it closes. But, you want the file writing to stop even if the valve remains opened and you've been logging for X seconds. That seems odd to me, as you would be missing data while the valve remains open. Is this right?