Search code examples
nodesmqttnode-red

Sending value from array to mqtt in nodered


I have the attach debug output from nodered modbus node and i need to send just the data value in the array to mqtt. How do i achieve this with function node?

node-red debug

I am attaching what i have tried and the debug error, instead of it sending the value to mqtt that is not happening. I am a newbie to this so will appreciate your suggestions. Instead it sending value 36(which is the value of pv1 at the time i took the second screenshot) its returning 0

node-red debug1


Solution

  • OK, so there are a couple things we need for fix.

    Firstly the incoming message is passed into a function node as the msg object, so you need to use this to access the input data. The path to the first element of the data field in the payload would be:

    msg.payload.data[0]
    

    Secondly it is not good practice to create new msg objects, it is better to reuse the same input object for the output.

    So putting it all together the best solution using a function node would be:

    msg.payload = msg.payload.data[0];
    msg.topic = "emon/deye/pv1`;
    
    return msg;
    

    Another solution to this problem would be to use the change node to move the required values around and to set the msg.topc