Creating "message center" (trimming string/expression)


1) Give a description of the problem
I’m working on creating a “messages” tile on my SharpTools dashboard, which is currently updated by a HTTP Post from WebCore. It will show recent messages sent over from various pistons (e.g. “Clothes have finished drying,” “Trash pickup tomorrow,” etc.)

I have it successfully receiving strings sent over, but I’m trying to get WebCore to only send over the 3 most recent messages and am not sure how to tackle this.

2) What is the expected behaviour?

  1. Any piston can append the “@messages” global variable.
  2. When the @messages variable is updated, it trims some of the “older” messages so the variable isn’t a huge list of outdated messages. Each message is on a new line in the string.

3) What is happening/not happening?
Right now, I’m planning on using an expression to take the current value of “@messages” and add a new line to it. But, here is my question: I don’t want a massive list of old messages to go through. Only the last 3-5. If it’s not easy to “truncate” a variable based on lines, perhaps I can do so by characters? How do you recommend I do this?


If I’m understanding this correctly, you can set up your @messages variable to hold last 3 messages separated by commas. Example would be

@messages = message3, message2, message1

You then access each one using arrayItem(index,@messages)

So if you have a new message4, this will push off message1 from the global var.

Then your new task would be to:

Set variable @messages = message4","arrayItem(0,@messages)","arrayItem(1,@messages)

@messages should now have message4,message3,message2 value.


That did it - thank you!