# Help with msg.payload in a function/subflow needed

**URL:** https://discourse.nodered.org/t/help-with-msg-payload-in-a-function-subflow-needed/42549
**Category:** General
**Created:** [12 March 2021 16:58 UTC](https://discourse.nodered.org/t/help-with-msg-payload-in-a-function-subflow-needed/42549 "2021-03-12T16:58:23Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)
#### Post date: [12 March 2021 16:58 UTC](https://discourse.nodered.org/t/help-with-msg-payload-in-a-function-subflow-needed/42549/1 "2021-03-12T16:58:23Z")

</div>

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!

Example:

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?

Regds  
Ed

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [12 March 2021 17:06 UTC](https://discourse.nodered.org/t/help-with-msg-payload-in-a-function-subflow-needed/42549/2 "2021-03-12T17:06:17Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)
#### Post date: [12 March 2021 19:11 UTC](https://discourse.nodered.org/t/help-with-msg-payload-in-a-function-subflow-needed/42549/3 "2021-03-12T19:11:08Z")

</div>

Thank you!!

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

---

<div class="post-metadata">

### Author: ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)
#### Post date: [13 March 2021 12:50 UTC](https://discourse.nodered.org/t/help-with-msg-payload-in-a-function-subflow-needed/42549/4 "2021-03-13T12:50:22Z")

</div>

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  
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  
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

Regds  
Ed

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [13 March 2021 12:58 UTC](https://discourse.nodered.org/t/help-with-msg-payload-in-a-function-subflow-needed/42549/5 "2021-03-13T12:58:51Z")

</div>

> [@eddee54455](#):
>
> Sorry Java 101 is still way above the paygrade

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

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

```auto
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.

---

<div class="post-metadata">

### Author: ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)
#### Post date: [13 March 2021 13:03 UTC](https://discourse.nodered.org/t/help-with-msg-payload-in-a-function-subflow-needed/42549/6 "2021-03-13T13:03:06Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [13 March 2021 13:07 UTC](https://discourse.nodered.org/t/help-with-msg-payload-in-a-function-subflow-needed/42549/7 "2021-03-13T13:07:56Z")

</div>

Try this...

```auto

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;

```

---

<div class="post-metadata">

### Author: ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)
#### Post date: [13 March 2021 13:11 UTC](https://discourse.nodered.org/t/help-with-msg-payload-in-a-function-subflow-needed/42549/8 "2021-03-13T13:11:40Z")

</div>

Wow!

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!!  
Ed

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [13 March 2021 13:16 UTC](https://discourse.nodered.org/t/help-with-msg-payload-in-a-function-subflow-needed/42549/9 "2021-03-13T13:16:35Z")

</div>

> [@eddee54455](#):
>
> A bit more complex than I thought....

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?

---

<div class="post-metadata">

### Author: ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)
#### Post date: [13 March 2021 13:40 UTC](https://discourse.nodered.org/t/help-with-msg-payload-in-a-function-subflow-needed/42549/10 "2021-03-13T13:40:59Z")

</div>

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 few...you 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....

Regds  
Ed

---

<div class="post-metadata">

### Author: ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)
#### Post date: [13 March 2021 14:55 UTC](https://discourse.nodered.org/t/help-with-msg-payload-in-a-function-subflow-needed/42549/11 "2021-03-13T14:55:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [12 May 2021 14:56 UTC](https://discourse.nodered.org/t/help-with-msg-payload-in-a-function-subflow-needed/42549/12 "2021-05-12T14:56:03Z")

</div>

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