PreviousValue vs CurrentValue

Within a Device Handler I can/could capture the previous value by including
previousValue = device.currentValue(“energy”) ?: cmd.scaledPreviousMeterValue ?:` 0

How do I do this within a smartapp? At this moment, my smartapp gets the currentValue of “energy” every hour and, while I could codethe following:

def curEnergy=sensor1.currentValue("current").toDouble()
def hourlyUsage=curEnergy - state.previousEnergy

I have little faith that state.previousEnergy (or atomicState) will retain its value hour after hour. Besides, the device already apears to have this value so why not use it? But how??

Most devices don’t have a Previous Value that they send back. “scaledPreviousMeterValue” is a value that is being sent from the zwave device and I have not used it personally. In the Aeon DH it warns that it sometimes does not always contain a value, so not sure how reliable it is.

State is probably the best way to handle this. It is persisted to the database, so it should not disappear. We use AtomicState in the Nest-Manager app quite a bit. We did have issues back in March (with everything ST) with the states not updating, but since the updates has been rock solid.