State variable scope

So I have some code that sets a variable of state[evt.deviceId] and sets that to now() to compare the last time the function was called for that device. Line 89 and Line 112. I believe this used to work, it no longer works; state[evt.deviceId] is always null, even after the function has run to set it to now(). So my question is, do I need to define state[evt.deviceId] to be in global scope? I don't really know how to do that since I have to do this to get all deviceId's, and at that point, the scope is of the contact.each function.

contact.each {

I tried this and it didn't work. However the code below works, assuming I set state.lastTime=NULL at the beginning of my code, but that would only let this work with a single contact device (which I guess is fine, but I want to know why it's not working).

lastTime = state.lastTime
state.lastTime = now()

So my question is, did something change recently, maybe in 2.0.7, with the scope of these variables?