Different value when reading custom attribute

I’m new and playing around. In particular I’ve been using an intermittent problem with an iris smart button as my stepping stone to development. While reading a custom attribute two different ways I intermittently get different values. is there a preferred method that is more deterministic?

the log below shows .value returning the correct value while .date.getTime() does not

note that the usage of currentState and lastState is that I’m trying to understand how they differ as they never seem to for me.

the log shows:

10:55:14 AM EST: debug Button 1 pushed
10:55:14 AM EST: debug device.latestState('lastPress').value=1457711714107 //right
10:55:14 AM EST: debug device.latestState('lastPress').date.getTime()=1457711713819 //wrong
10:55:14 AM EST: debug device.currentState('lastPress').value=1457711714107 //right
10:55:14 AM EST: debug device.currentState('lastPress').date.getTime()=1457711713819  //wrong
10:55:14 AM EST: debug startOfPress=1457711713819
17910:55:14 AM EST: debug +++++ PRESS OUT
10:55:14 AM EST: debug Parsing 'catchall: 0104 0006 01 01 0140 00 ED22 01 00 0000 00 00 '
10:55:14 AM EST: debug setting state and attribute time to =1457711714107
10:55:14 AM EST: debug +++++ PRESS IN
10:55:14 AM EST: debug Parsing 'catchall: 0104 0006 01 01 0140 00 ED22 01 00 0000 01 00 '

on a button push-in a createEvent sets the attribute:

    def curTime = now() //get the current time to use as debug
    log.debug "setting state and attribute time to =${curTime}"
    return createEvent([name: 'lastPress', value: curTime, data:[buttonNumber: button], displayed: false])

on button release the following:

    def curLastPressDate = device.currentState('lastPress').date.getTime()
    log.debug "device.currentState('lastPress').date.getTime()=${curLastPressDate}"
   
    def curLastPressValue = device.currentState('lastPress').value
    log.debug "device.currentState('lastPress').value=${curLastPressValue}"
    
    def latestLastPressDate = device.latestState('lastPress').date.getTime()
    log.debug "device.latestState('lastPress').date.getTime()=${latestLastPressDate}"
   
    def latestLastPressValue = device.latestState('lastPress').value
    log.debug "device.latestState('lastPress').value=${latestLastPressValue}"

the difference between them is 288ms, is that really an issue?

should have mentioned I’m not concerned about the impact of the difference - more so that they are different, trying to understand why this would be different.

there are other inconsistencies like this run with null values for value and not for the .date.getTime method:

179938ad-5332-45d1-b1ed-cd61902bf478  12:59:02 PM EST: debug device.latestState('lastPress').value=null
179938ad-5332-45d1-b1ed-cd61902bf478  12:59:02 PM EST: debug device.latestState('lastPress').date.getTime()=1457719126008
179938ad-5332-45d1-b1ed-cd61902bf478  12:59:02 PM EST: debug device.currentState('lastPress').value=null
179938ad-5332-45d1-b1ed-cd61902bf478  12:59:02 PM EST: debug device.currentState('lastPress').date.getTime()=1457719126008

When you are setting the value to the timeStamp, that value is being captured using now() which is the current time of the node the app is executing on, using date.getTime is likely the time stamp created when the event is written to the back end data store, which is going to be on a different node.
I’m not sure I follow the need to create an event who’s value is a timestamp, when the event time stamps are already created with reasonable accuracy for every event anyway.

1 Like

gotcha, that makes sense. The code I’m playing with is using a custom attribute to store persistent data - the author is moving it over to use state for persistent data. I’m just playing around and learning as I go :slightly_smiling:

thank you.