Search code examples
node.jslistnode-red

How to output a list of multiple msg.payload in node-red?


I have multiple node (e.g. date + server status message (via http get)) and I want to display a list of the last x messages on the node-red dashboard. I could not find a node / npm package for this, I assume I can do it with the standard packages?

List example:

  • 1-3-17 - Status: Online
  • 1-2-17 - Status: Offline
  • 1-2-17 - Status: OK
  • 1-1-17 - Status: Bad

The message handling ain't the problem here, but how to output and delete the xth entry in the list if a new one comes in?


Solution

  • Sounds like you need to look at how use the context to store data.

    The simplest way to do this will be in a function node, something like this:

    var list = context.get("statusList") || [];
    var date = new Date().toLocaleDateString();
    var entry = date + " - " + msg.payload;
    list.push(entry);
    if (list.length > 5) {
      list = list.slice(-5);
    }
    context.put("statusList", list);
    msg.payload = list;
    return msg;
    

    This should keep the last 5 input messages and outputs and array with those 5 messages.