# Declare msg.payload as a value

**URL:** <https://discourse.nodered.org/t/declare-msg-payload-as-a-value/63790>\
**Category:** General\
**Created:** [11 June 2022 14:19 UTC](https://discourse.nodered.org/t/declare-msg-payload-as-a-value/63790 "2022-06-11T14:19:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xenos](https://avatars.discourse-cdn.com/v4/letter/x/ea5d25/32.png) [@Xenos](https://discourse.nodered.org/u/Xenos)\
**Post date:** [11 June 2022 14:19 UTC](https://discourse.nodered.org/t/declare-msg-payload-as-a-value/63790/1 "2022-06-11T14:19:07Z")

</div>

My message payload is a value of X, its always changing, in this example it's 33.13.

![Payload](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/6540c2ccd76a83ca601cfc703f8d10869a2af7dc.jpeg)

How would I go about declaring it so I can use it in the position or value I have as "X" in this example

 ![Jason](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/1/614180df240d61bba447c86e0b286a603154cf7e.jpeg)

Thank you for your help

---

<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:** [11 June 2022 14:53 UTC](https://discourse.nodered.org/t/declare-msg-payload-as-a-value/63790/2 "2022-06-11T14:53:20Z")

</div>

There are numerous ways to do this, here are examples of 4 ways.

```auto
[{"id":"ee071f60.912ab","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"33.45","payloadType":"num","x":200,"y":3260,"wires":[["7c38eac9.44776c","483ff17b.1523d8","52df4dc0.6d0da4","9a658455.b43348"]]},{"id":"7c38eac9.44776c","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"hold","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"{\"p1\":{\"v\":1,\"w\":2}}","tot":"json"},{"t":"move","p":"hold","pt":"msg","to":"payload.p1.x","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":3160,"wires":[["a51b5a1f.26af08"]]},{"id":"483ff17b.1523d8","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"p1\":{\"v\":1,\"w\":2,\"x\":$$.payload}}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":3260,"wires":[["a51b5a1f.26af08"]]},{"id":"52df4dc0.6d0da4","type":"template","z":"bf9e1e33.030598","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\"p1\":{\"v\":1,\"w\":2,\"x\":{{{payload}}}}}","output":"json","x":370,"y":3320,"wires":[["a51b5a1f.26af08"]]},{"id":"9a658455.b43348","type":"function","z":"bf9e1e33.030598","name":"","func":"msg.payload = {\"p1\":{\"v\":1,\"w\":2,\"x\":msg.payload}}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":3400,"wires":[["a51b5a1f.26af08"]]},{"id":"a51b5a1f.26af08","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":3240,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Xenos](https://avatars.discourse-cdn.com/v4/letter/x/ea5d25/32.png) [@Xenos](https://discourse.nodered.org/u/Xenos)\
**Post date:** [11 June 2022 22:25 UTC](https://discourse.nodered.org/t/declare-msg-payload-as-a-value/63790/3 "2022-06-11T22:25:58Z")

</div>

I'm just a beginner, and to be honest I'm lost with your code. Can you break out one so I can review it.

Thank you

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [12 June 2022 10:53 UTC](https://discourse.nodered.org/t/declare-msg-payload-as-a-value/63790/4 "2022-06-12T10:53:43Z")

</div>

To see the 4 examples - copy the code and then in Node-RED - click on the hamburger menu top-right - select Import - paste into the big box and click on import

You can then see them all

I personally would think that the template one is the easiest to understand and play around to suit what you want

---

<div class="post-metadata">

**Author:** ![Xenos](https://avatars.discourse-cdn.com/v4/letter/x/ea5d25/32.png) [@Xenos](https://discourse.nodered.org/u/Xenos)\
**Post date:** [12 June 2022 13:49 UTC](https://discourse.nodered.org/t/declare-msg-payload-as-a-value/63790/5 "2022-06-12T13:49:52Z")

</div>

Got it now, this makes a big difference.

Thank you

---

<div class="post-metadata">

**Author:** ![Xenos](https://avatars.discourse-cdn.com/v4/letter/x/ea5d25/32.png) [@Xenos](https://discourse.nodered.org/u/Xenos)\
**Post date:** [18 June 2022 15:12 UTC](https://discourse.nodered.org/t/declare-msg-payload-as-a-value/63790/6 "2022-06-18T15:12:42Z")

</div>

I don't think I'm asking the question properly.

the string I send to my controller is

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

what ever I set in "value": "X" presently ( "value": "85")

gets written.

how do i make this my payload value?

---

<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:** [2 July 2022 15:13 UTC](https://discourse.nodered.org/t/declare-msg-payload-as-a-value/63790/7 "2022-07-02T15:13:41Z")

</div>

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