# How to store historical values (i.e. previous 10 temperature readings)

I want to add a trend indicator / value to my custom SHT30 Temperature Humidity sensor driver.

I need some guidance in how to store the old readings. Currently I'm thinking of storing 10 sets of each reading and perhaps the time of those readings.

All I can see is either:

• Multiple States
• Hub Variables

To my knowledge neither of the above can be any type of list or array.

Is there another way I may be missing?

Thanks
John

I donâ€™t see why you canâ€™t just keep an array of previous values in your state. Certainly attributes can be a list or array because thermostats have a list or their supported modes. But I could easily be mistaken.

Another potentially-simpler way may be to use an exponential moving average. Itâ€™s a way of approximating the average of the last N data points without actually storing all those points.

Letâ€™s assume you want an average of the last 60 points....if â€śemaâ€ť is your current average value â€śnewâ€ť is your latest reading, youâ€™d do the following:

``````ema = (ema * (59/60)) + (new * (1/60))
``````

That should show you the basics of the math, itâ€™s probably written in code as

``````ema = (ema * 59 + new) / 60
``````

You can easily adjust to keep the EMA over any number of points and you never store more than a single value. If you Google exponential moving average youâ€™ll find more compicated formulas that do a better job or allow you to add more emphasis to more recent points and whatnot...but the gist is the same of approximating the average of the last N points.

Now if you were actually wanting to store all those points to plot a trend line or something, then everything I said is meaningless. =p

All "state" are stored as strings - but nothing to stop the driver from making that string "array-like" with a comma delimiter, and making that transformation when reading/writing to state.

2 Likes

Thanks I try this approach.

Now I have to find the string length limit for states (or perhaps hub variables).

I wonder if I could create a json like string??

Absolutely, could use the groovy libraries JsonSlurper and JsonOutput to facilitate this...

1 Like

Thank you. My vision/goal is to have something like:

• Temperature changed +/- xxÂ° in the last hour (or maybe 2 hours).
• Max of the day
• Min of the day
• Max since reset
• Min since reset