Can I record the states/attributes of the device?

I have an Awair Air Quality Edge Driver which has a function that fetch the air data via Local API as a JSON string. This said function gets called every minute in the init life cycle and every time the user uses the refresh capability to get the latest air data.

Now, I want to implement some MQTT features onto the device. What I want to achieve is that every time the data refreshes, if the change in one of the co2, PM, and VOC values is greater than 20% of corresponding previous data value, encode the new data into a JSON string and send it to the MQTT broker along with a warning message.

I have successfully established a connection to the MQTT broker, but right now I am stuck on how to record the data in order to do the comparison. Any idea on how to do that? Different approaches are also gladly welcome. Thank you very much in advance.

Hi, @sleepdeprived!

If I understood the description, the last value you’re referring to, is one that you sent in a capability event, right?
I mean when you send device:emit_event() with a new value. If so, the latest state for a specific capability can be retrieved using get_latest_state(); you can find an example of how to use it in this driver sample: SampleDrivers/common.lua at main · SmartThingsDevelopers/SampleDrivers · GitHub

Another option could be using the persistent storage (set_field/get_field), they are also used in the sample here.