# Insert variable into a change node

**URL:** https://discourse.nodered.org/t/insert-variable-into-a-change-node/41779
**Category:** General
**Created:** [1 March 2021 03:34 UTC](https://discourse.nodered.org/t/insert-variable-into-a-change-node/41779 "2021-03-01T03:34:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nappyjim](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nappyjim/32/34077_2.png) [@nappyjim](https://discourse.nodered.org/u/nappyjim)
#### Post date: [1 March 2021 03:34 UTC](https://discourse.nodered.org/t/insert-variable-into-a-change-node/41779/1 "2021-03-01T03:34:11Z")

</div>

This is Home Assistant related, but I believe someone here may be able to help. I found similar question on here, but it dealth with a http response.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/8/f8fb0bdff916cb0e8b8f71fe57e5815d2361c775.png)

```auto
[{"id":"9d4d90de.91179","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"b3fe91.07c4e17","type":"server-state-changed","z":"9d4d90de.91179","name":"Brightness changed","server":"817e283a.e704f8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_number.light_strip_brightness","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":320,"y":200,"wires":[["44c70077.bf323"]]},{"id":"7ff53dc4.9645a4","type":"change","z":"9d4d90de.91179","name":"22","rules":[{"t":"set","p":"topic","pt":"msg","to":"zwave/_CLIENTS/ZWAVE_GATEWAY-Mosquitto/api/sendCommand/set","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"{ \"args\": [{ \"commandClass\": 112, \"nodeId\": 12, \"property\": 23 }, \"set\", [ 23, {{brightness}}, 4] ] }","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":200,"wires":[["9a70e8de.15d6c8"]]},{"id":"44c70077.bf323","type":"change","z":"9d4d90de.91179","name":"","rules":[{"t":"set","p":"brightness","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":200,"wires":[["7ff53dc4.9645a4"]]},{"id":"9a70e8de.15d6c8","type":"debug","z":"9d4d90de.91179","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":990,"y":200,"wires":[]},{"id":"817e283a.e704f8","type":"server","name":"Home Assistant","addon":true}]

```

When I change brightness slider, the event state node is triggered and the number is put into msg.payload.

I use the change node to move the number value in msg.payload into msg.brightness

What I then want in the change node labeled 22 is to input that msg.brightness value here:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2a147e5273be37df024fc18234f90ab105467ec9.png)

But when I try that and check the debug, it just sends {{brightness}} instead of the number value. So it will send this…  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/2/e295df7f6ba55d59ee03128f182e5947a811a9fa.png)

When I want it to send this (assuming brightness slider was set to 99)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/1/d16c5b706cf6368584383b21049bc275d7e1572c.png)

---

<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: [1 March 2021 06:20 UTC](https://discourse.nodered.org/t/insert-variable-into-a-change-node/41779/2 "2021-03-01T06:20:50Z")

</div>

I would recommend to go through the documentation and specifically [how to work with messages](https://nodered.org/docs/user-guide/messages).

You are trying to make a new object by using a change node and setting it to text with a new message, this is not how it works (well i guess it could be done in some way, but this is complicating things).

There are multiple ways to do this, create a new object with a change node by means of specifying each property, using jsonata or with a function node. I think a function node will be the easiest.

example in a function node:

```auto
let input_brightness = msg.brightness

msg.payload = {args:[{brightness:input_brightness}]} // build your new object here

return msg

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [1 March 2021 07:41 UTC](https://discourse.nodered.org/t/insert-variable-into-a-change-node/41779/3 "2021-03-01T07:41:00Z")

</div>

This seems like a template node will do the job.  
e.g.

```auto
[{"id":"d3b9ea1c.861f1","type":"inject","z":"9d4d90de.91179","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"78","payloadType":"num","x":340,"y":200,"wires":[["5c2de512.39f6fc"]]},{"id":"5c2de512.39f6fc","type":"template","z":"9d4d90de.91179","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{ \n \"args\": \n [\n {\"commandClass\": 112, \n \"nodeId\": 12, \n \"property\": 23 \n }, \n \"set\", \n [ 23, {{payload}}, 4] \n ] \n }","output":"str","x":490,"y":200,"wires":[["9a70e8de.15d6c8"]]},{"id":"9a70e8de.15d6c8","type":"debug","z":"9d4d90de.91179","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":650,"y":180,"wires":[]}]

```

Then just use msg.payload from the dropdown.

---

<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: [30 April 2021 07:41 UTC](https://discourse.nodered.org/t/insert-variable-into-a-change-node/41779/4 "2021-04-30T07:41:46Z")

</div>

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