Hello, is there a simple way to store always the last 100 states of a device in a stringified JSON object as variable in sharptools? Like is there a function that lets me remove the first element of the array and add a new one at the last position?
I know I could write it in a static way
myVar = parseJson($myVar)
myVar[0]=myVar[1]
myVar[1]=myVar[2]
…
myVar[99]=$context.event.value
But is there a shorter alternative?
I’ve done something similar. This probably won’t work for your use case though. But, in case it helps, I created a few variables, essentially called current-state, old-state, older-state. Then, before the current-state gets updated, that value is written to the old-state, and so on.
This is a little hacky, but more compact than assigning all 100 elements of the array. You would set variable $myVar to this expression (assuming that $context.event.value is a string) :
myVar = parseJson($myVar)
q = count(myVar)
myVar[0] = q < 99 ? myVar[0] : “DELETEME”
myVar[q] = $context.event.value
exclusions = [“DELETEME”]
filter(myVar,not(contains(exclusions,x)))
Note: I quickly tested it with a smaller array and it seemed to work, but there could be some bugs remaining
2 Likes