Easy way to get previous value of attribute?

Is there an easy way to get the previous value of a attribute whenever an event is triggered?

The Event object passed to the handler has properties for the current value, so I was curious if there’s anything similar for getting the previous value. In the past, I’ve stored the previous value in state and compared that to the current value, but I wasn’t sure if there was a better way to do it.

I’ve seen the events method on the Device object, but I wasn’t sure if this was a good use case for it. The SmartThings documentation suggests that it only keeps 7 days of data, but I wasn’t sure if that applies to Hubitat too.

I don't belive there is; I would do what it sounds like you have already thought of, which is subscribe to the event in question and store the "previous" value (current value, I guess, so store if for next time--after you're done doing whatever comparison you need to do this time around).

You can access event history on a device, and I don't believe the platform lets you set it below 5 (the UI says 1, but I don't think that actually sticks), but how much is there will vary based on the user's settings--it's not 7 days necessarily (Hubitat's default is 100 events for a specific attribute or 365 days max, whichever happens first, but both can be adjusted system wide or the former per device). I don't think I'd count on that regardless.

EDIT: the latter thing is what is being described below. (I'm not sure I'd count on it, though, in case it is or ever becomes possible for every event to be purged immediately. I think any stock app that needs to know a previous value stores it in state, so I figure that's a good example to follow if nothing else.) But it could be be helpful depending on what you're really doing--or if the app and devices are entirely within your control and you don't have end users doing odd things to worry about. :slight_smile:

List eventsBetween(Date startDate, Date endDate)
List statesSince(String attributeName, Date startDate)

I generally use the statesSince because I can specify the attribute name I want…

Ok, thanks guys. I wanted to make sure I wasn’t missing anything. Sounds like using state is the way to go for me.

I appreciate the details on events and states, especially the fact that statesSince includes the attribute name. I don’t have an immediate use case that would be better served by those, but I do understand them better, so that’s a win.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.