My motion sensor CoRE programs only seem to work when the motion sensors activate the lights

I wrote a motion sensor timeout core program that so far seems to be doing what I want when the sensor activate the lights, however I’m sure it’s not perfect. But I noticed with a little testing, that it only seems to timeout when the motion sensor turn on the lights. It I do it by the switch or the app they do not seem to timeout. I would of thought it would work either way. Is their something that need to be added to the piston. Or is it the motion sensor have to go from active to inactive to activate the piston?