# Variables + Append Values in function node payload

**URL:** <https://discourse.nodered.org/t/variables-append-values-in-function-node-payload/25471>\
**Category:** General\
**Created:** [25 April 2020 10:16 UTC](https://discourse.nodered.org/t/variables-append-values-in-function-node-payload/25471 "2020-04-25T10:16:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![notnice](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@notnice](https://discourse.nodered.org/u/notnice)\
**Post date:** [25 April 2020 10:16 UTC](https://discourse.nodered.org/t/variables-append-values-in-function-node-payload/25471/1 "2020-04-25T10:16:52Z")

</div>

I am setting up controls for Osram bulb and setting reusable flow variables in a system values node i.e. for my Osram dimmer bulb:  
flow.set("OsramBulbDimmer", {"cluster\_id":"0x0008", "endpoint\_id": "3", "command\_id": "0x00", "parent\_command": "zcl level-control", });

In the function node I am trying use an entry like below where the dimmer params are appended to the flow variable in the payload (dimmerlevel is a numeric value already set), but I get get formatting errors every which way I try to spin it.  
msg.payload = {flow.get("OsramBulbDimmer") , "params":{level: dimmerlevel, "transition time": 10}};

This way kind of works, but it creates a new object in the payload for the variable name "bulbval" and the bulb rejects the command.  
var bulbval = flow.get("OsramBulbDimmer");  
msg.payload = {bulbval, "params": {level: dimmerlevel,"transition time": 10}};

For comparison, if I set the entire payload in the function node like this then it works:  
msg.payload = {  
"cluster\_id":"0x0008",  
"endpoint\_id": "3",  
"command\_id": "0x00",  
"parent\_command": "zcl level-control",  
"params":{level: dimmerlevel,"transition time": 10}},

---

<div class="post-metadata">

**Author:** ![DamianosS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/damianoss/32/16990_2.png) [@DamianosS](https://discourse.nodered.org/u/DamianosS)\
**Post date:** [25 April 2020 14:42 UTC](https://discourse.nodered.org/t/variables-append-values-in-function-node-payload/25471/2 "2020-04-25T14:42:34Z")

</div>

@notnice, maybe you could go this way:

```auto
var obd = flow.get("OsramBulbDimmer");

msg.payload = {
    "cluster_id":obd["cluster_id"],
    "endpoint_id": obd["endpoint_id"],
    "command_id": obd["command_id"],
    "parent_command": "zcl level-control",
    "params":{level: dimmerlevel,"transition time": 10}
}

return msg;

```

---

<div class="post-metadata">

**Author:** ![notnice](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@notnice](https://discourse.nodered.org/u/notnice)\
**Post date:** [25 April 2020 15:02 UTC](https://discourse.nodered.org/t/variables-append-values-in-function-node-payload/25471/3 "2020-04-25T15:02:06Z")

</div>

Hi Damians - thx for the reply. That will probably work as its very close to my working scenario where all the commands are individually specified in the function node. What I'm trying to do is get away from doing that and make those common commands flow level variable so I can re-use in other nodes. In my mind thats a neater solution.

---

<div class="post-metadata">

**Author:** ![DamianosS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/damianoss/32/16990_2.png) [@DamianosS](https://discourse.nodered.org/u/DamianosS)\
**Post date:** [25 April 2020 15:07 UTC](https://discourse.nodered.org/t/variables-append-values-in-function-node-payload/25471/4 "2020-04-25T15:07:03Z")

</div>

Yes, the solution I suggested needs the attribute names to be hardcoded.  
Not very pretty.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [26 April 2020 05:36 UTC](https://discourse.nodered.org/t/variables-append-values-in-function-node-payload/25471/5 "2020-04-26T05:36:21Z")

</div>

Try it like this:

```auto
var bulbval = flow.get("OsramBulbDimmer");
msg.payload = bulbval
msg.payload.params = {level: dimmerlevel,"transition time": 10}
return msg

```

---

<div class="post-metadata">

**Author:** ![notnice](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@notnice](https://discourse.nodered.org/u/notnice)\
**Post date:** [26 April 2020 09:09 UTC](https://discourse.nodered.org/t/variables-append-values-in-function-node-payload/25471/6 "2020-04-26T09:09:28Z")

</div>

> [@bakman2](#):
>
> var bulbval = flow.get("OsramBulbDimmer"); msg.payload = bulbval msg.payload.params = {level: dimmerlevel,"transition time": 10}

Cool - thats works and means I can use that as a template in other nodes to cut down on referencing the same commands in every node 🙂

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [26 April 2020 10:13 UTC](https://discourse.nodered.org/t/variables-append-values-in-function-node-payload/25471/7 "2020-04-26T10:13:30Z")

</div>

it could be even shorter

```auto
msg.payload = flow.get("OsramBulbDimmer")
msg.payload.params = {level: dimmerlevel,"transition time": 10}
return msg

```

---

<div class="post-metadata">

**Author:** ![notnice](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@notnice](https://discourse.nodered.org/u/notnice)\
**Post date:** [26 April 2020 10:36 UTC](https://discourse.nodered.org/t/variables-append-values-in-function-node-payload/25471/8 "2020-04-26T10:36:50Z")

</div>

Saving 1 line in every node is even better - thx 👍

---

<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:** [10 May 2020 10:36 UTC](https://discourse.nodered.org/t/variables-append-values-in-function-node-payload/25471/9 "2020-05-10T10:36:53Z")

</div>

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