Button variables in csv not resetting across trials

OS: Win11
PsychoPy version: 2022.2.5
Standard Standalone? Yes

What are you trying to achieve?
I want to use three variables that are automatically saved to the csv for buttons. Assuming the button object name is button, the three variables are:

button.numClicks
button.timesOn
button.timesOff

In the csv file, these variables are not getting set to zero on successive runs of the routine containing the button when the button is not used. Specifically, the values that show up in the csv file are from the previous trial where the button was used.

What did you try to make it work?
To reset the values, I put the following code in the End Routine. [Note: For some reason, this code did not work in the Begin Routine and I still don’t know why.]

if trialtype==0:
zero=0
thisExp.addData(‘button.numClicks’,zero)
thisExp.addData(‘button.timesOn’,zero)
thisExp.addData(‘button.timesOff’,zero)

It should be noted that when trialtype==0 the button is hidden using setAutoDraw(False).

What specifically went wrong when you tried that?:
For now, my solution is to use the above lines to reset the values to zero in End Routine. However, I am wondering if there is a better way. For example, a reset command that resets all the variables appearing in the csv when a routine begins.

Thanks in advance for your help.

Ken