Search code examples
javascriptdashboardnode-red

Fetching data through Node-red Dashboard


I am using node-red to fetch data through MQTT. I am having response something like in the picture..

Node Red Debug Screen

No i want to get data w.r.t Phase1,Phase2,Phase3,Phase4 and Timestamp separately with the help of functions.I dont know how to get like that way. P.S I am new to node.red. Thankyou


Solution

  • If all you want to do is get the numeric value out of each msg, you can use a function node to split the string, like so:

    var parts = msg.payload.split(":");
    var value = parts.length > 1 ? parts[1].trim() : parts[0].trim();
    
    // append the string before the ":" to the topic...
    msg.topic += "/" + parts.length > 1 ? parts[0] : "Total";
    // coerce the value after the ":" to be numeric...
    msg.payload = +value;
    return msg;
    

    Now you will have a topic and payload that you can show directly in your dashboard elements.

    There are also at least 2 nodes that can do the same thing, without writing any javascript code. You might want to install and try the node-red-contrib-string node -- it is good for extracting bits of strings. The change node can also be used, but would require a Jsonata expression, which is powerful, but also a bit more complicated than even javascript code...