Help with msg.payload in a function/subflow needed

Hi guys,

First off... I'm a newbie in pretty much all respects, I know a little about programming, but am struggling my way through as best as is possible... Be gentle with me...

I am trying to write a function/subflow to modify msg.payload.xxxxxx

xxxxxx I can set via the environment variables in the subflow template.

What I would like to do is use this environment variable in my subflow to set the "name extension" (I dont know what the correct term is) of the msg.payload... Best described, possibly by example, again, I am not the most learned where it comes to this, so please be patient with me!


Environment Variable Name: Fred and Fred's Contents: "Voltage"

I wish to: msg.payload."Fred" = 123.4 (in other words have msg.payload.Voltage = 123.4)

Is this possible?


Hi @eddee54455

In a function node you can use env.get to retrieve the value of a subflow env property.

You should then be able to do something like this:

msg.payload[env.get("Fred")] = 123.4
return msg;

Thank you!!

I'm still trying to wrap a befuddled brain around .... [{()}] .... these things!!

Hi @knolleary
I'm trying to develop this a bit further, but ran into the limits of my befuddled brain again....

Your above example works 100%, but when I try and "broaden" the scope of it, things seem to go awry...

Using a simple "inject node" I push in a payload.Volts with a value of 250 -
to this line of code:
msg.payload[env.get("Payload1")] = env.get("Payload1TOV");return msg
where:(Payload1 = "Volts" and Payload1TOV = 123456

I get this output:
msg : Object
_msgid: "fae1f4e5.417fa8"
payload: object
Volts: 123456 ..... This is 100%!

..... But....

where I use this code:
msg[env.get("Payload1")] = env.get("Payload1TOV");return msg

where:(Payload1 = "payload.Volts" and Payload1TOV = 123456)

I get this Output:
msg : Object
_msgid: "732b59d0.d78b18"
payload: object
Volts: 250
payload.Volts: 123456

Im a little "confused" as to why... Sorry Java 101 is still way above the paygrade for a 60yr old brain...LOL


Java is not JavaScript. Totally different. So if you are reading up on Java, you are wasting your time. :joy:

So ignoring the environment variables for a second, you are essentially saying...

msg["payload.Volts"] =  "123456";
return msg;

Which will correctly set a property in msg named payload.Volts - as you are getting.

If you want nested objects e.g. a Volts property inside an object named payload inside the msg object you will have to approach this differently.

Right... Both Counts!!

Shows how ignorant I actually am...

I am so out of my depth here, I don't actually know how to ask the questions!!

This looks like what I am after -> "If you want nested objects e.g. a Volts property inside an object named payload inside the msg object you will have to approach this differently."

Any hints?

Try this...

var propPath = env.get("Payload1");
var propValue = env.get("Payload1TOV");

//temp for debugging - remove the next 2 lines if all ok...
node.warn( "propPath = " + propPath);
node.warn( "propValue = " + propValue);

* setMessageProperty(msg: object, prop: string, value?: any, createMissing?: boolean): boolean
RED.util.setMessageProperty(msg, propPath, propValue, true); 

return msg;


A bit more complex than I thought....

Thanks HEAPS!!

I'm gonna go try it out and see if I can learn something in the process... I have written a rather "clumsy" home auto system in Node Red using the standard pallettes and am dying to "refine" it a bit... This is a great help!!

Thank you!!

Its not complex really, I just wrote it long hand and added some node.warns - to help you grasp it. The only (extra) bit that really matters is RED.util.setMessageProperty

TBH: what you are doing is a bit odd. I mean why are you grabbing stuff from environment? Arent you just overcomplicating things?

Possibly, yes... But...

In the home auto system, I have a bunch of Tasmota/Sonoffs monitoring things and turning all sorts on and off...(compressors/fridges/deepfreezes/solar level checking/power and temp monitoring/lighting/irrigation/grey water, to name but a name it..around 30 to 40 wifi switches or so at the mo...)

One of the problems I have, is that with rolling blackouts over here, some of the units go down and obviously dont send their data/payloads... The home auto system "carries forward" any telemetry data ie the Volts field, and does not update until the unit sends again when the power comes back on...

I have written a small subflow (really tiny) that has a timeout timer as part of the parameters for the node... ie if the data isnt rec'd within 15secs, set chosen payloads to 0... That also allows me to track if someone has cut through and is busy stealing copper cables (again), at a glance.....

Currently, I am doing this through a few nodes and a little custom timeout timer node I kludged together... This subflow will make things way "neater" on the flows for me...

I'm quite chuffed with the whole home auto thing so far, it seems to be working well....


1 Like

Implemented and working like a charm!! Thanks again!!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.