Getting values from a payload in Node-Red

How do I pass the value of a payload to a variable in Node-Red?

I am trying to pass the value of a payload into a variable using a Node-Red function. I isolated one bite from the payload which will be either a 0 or a 1 by using the code “var newMsg = { payload: msg.payload.sensor_data.input_1 };” . I then passed the newMsg to a debut node as seen by the screen shot below:

Now I am trying to read value of the payload.

FYI, I am using an IoT Long Range Wireless Push Notification Dry Contact Transmitter. I have already wrote a Python script to evaluate the payload byte above which is byte 25, I am using this byte to communicate if the equipment is or is not in production. The other byte, byte 26, is used to calculate cycle times and indicate machine stoppage while in production.

I’m not entirely sure what you’re trying to do. The payload is a property of the msg object so it can be used as a variable anywhere in the flow.

let my_var = msg.payload;

Are you looking to make it a global variable or pass it into your python script?

If you’re wanting to break up up the input 1 and 2 into multiple message you can return multiple messages from a function node.

@jacob I am not trying to pass a global variable to the python script. I brought up the python script to say I have the unit working correctly and avoid the endless troubleshooting to determine if the unit is working correctly.

I am trying to evaluated byte 25 of the message to determine whether it is a high or low. This tells me if the machine is in production or not in production. I know the machine is in production because output 1 is a high as seen by the circled part of the screen shot below.

Unfortunately I don’t know the code to evaluate this byte.

if(msg.payload == 1){


Or you can use a switch node in the flow configured like this one:
Screen Shot 2023-12-15 at 10.17.57 AM


@jacob Thank you I will try that when I get a chance.

Hi, I’m trying to do the same thing here, where I read in the parameters from a 4-20mA device and then append a timestamp and some other data to it.

I want to convert the object to a JSON


my function block looks like this

var d = new Date();

var t = d.getTime();

payload = {“mA1”: msg.mA1 , “mA2”: msg.mA2, “time” : t};

msg.payload = payload;

return msg;

Hi Dan,

There should be a timestamp on every message already although it may not be in the payload. I think its msg.time, but I don’t have one in front of me.

Do you mean you want to turn the payload into a JSON string? If so you can use:
msg.payload = JSON.stringify(msg.payload);

Not sure if this answers your question.

thanks, I see that now for msg.time.

what is the best way to extract a single value from the object? I am trying to use msg.payload.mA1 and msg.mA1 and don’t seem to be have any luck extracting the data.

Hi @danempiric, I share you this flow that could help you.

Instead of using:

payload = { “mA1”…

you can assign directly with:

msg.payload = { “mA1”…

I hope you find the information useful.

[{"id":"22329d1c96b7d419","type":"tab","label":"Flow 15","disabled":false,"info":"","env":[]},{"id":"22ce173095ae309c","type":"inject","z":"22329d1c96b7d419","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":370,"y":220,"wires":[["39bb4355ea1ec16d"]]},{"id":"39bb4355ea1ec16d","type":"function","z":"22329d1c96b7d419","name":"Simulation","func":"msg = {\n    \"adc1\":1133,\n    \"adc2\":653,\n    \"mA1\":0.7775779,\n    \"mA2\":0.44815390000000005,\n    \"byteOne\":4,\n    \"byteTwo\":109,\n    \"byteThree\":2,\n    \"byteFour\":141\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":220,"wires":[["2aa581834652641d"]]},{"id":"2aa581834652641d","type":"function","z":"22329d1c96b7d419","name":"your code","func":"var d = new Date();\nvar t = d.getTime();\nmsg.payload = {\"mA1\": msg.mA1 , \"mA2\": msg.mA2, \"time\" : t};\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":220,"wires":[["787d42e703c6793e"]]},{"id":"787d42e703c6793e","type":"debug","z":"22329d1c96b7d419","name":"debug 38","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":800,"y":220,"wires":[]}]

you should get:
Screenshot from 2024-02-20 09-55-05