# Add a value to payload

**URL:** <https://discourse.nodered.org/t/add-a-value-to-payload/73552>\
**Category:** General\
**Created:** [11 January 2023 17:06 UTC](https://discourse.nodered.org/t/add-a-value-to-payload/73552 "2023-01-11T17:06:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rhysgh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rhysgh/32/74195_2.png) [@Rhysgh](https://discourse.nodered.org/u/Rhysgh)\
**Post date:** [11 January 2023 17:06 UTC](https://discourse.nodered.org/t/add-a-value-to-payload/73552/1 "2023-01-11T17:06:37Z")

</div>

Hi, I'm looking to use arithmetic in payloads. For example, take a payload of 5 and put out 10.

I have been messing with a change node but cannot seem to find the correct expression.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [11 January 2023 17:15 UTC](https://discourse.nodered.org/t/add-a-value-to-payload/73552/2 "2023-01-11T17:15:49Z")

</div>

Why not use a function node to access the payload and modify it?

---

<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 January 2023 17:23 UTC](https://discourse.nodered.org/t/add-a-value-to-payload/73552/3 "2023-01-11T17:23:19Z")

</div>

The standard nodes Change(Jsonata) and function(Javascript) can do this. There are also contributor nodes you can install.  
Here is a change node example using JSONAta

```auto
[{"id":"c828b9168a3f213f","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":250,"y":3100,"wires":[["1fa7f070dcc73066"]]},{"id":"1fa7f070dcc73066","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number($$.payload) + 5","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":3080,"wires":[["a81d87f562b74deb"]]},{"id":"a81d87f562b74deb","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 225","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":3080,"wires":[]}]

```

The Jsonata expression.

```auto
$number($$.payload) + 5

```

---

<div class="post-metadata">

**Author:** ![Rhysgh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rhysgh/32/74195_2.png) [@Rhysgh](https://discourse.nodered.org/u/Rhysgh)\
**Post date:** [11 January 2023 17:37 UTC](https://discourse.nodered.org/t/add-a-value-to-payload/73552/4 "2023-01-11T17:37:24Z")

</div>

> [@E1cid](#):
>
> `$number($.payload) + 5`

This is perfect, thank you..  
How can I learn more about this? is there a reference?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/8/b8fb41bd10fd9e3f76f458b29fded40120be9beb.jpeg)

---

<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 January 2023 19:32 UTC](https://discourse.nodered.org/t/add-a-value-to-payload/73552/5 "2023-01-11T19:32:56Z")

</div>

Sure google `jsonata docs`

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 January 2023 07:29 UTC](https://discourse.nodered.org/t/add-a-value-to-payload/73552/6 "2023-01-12T07:29:07Z")

</div>

Personally I suggest learning a bit of JavaScript and doing such things in a function node before trying to master jsonata. Unless you find that your brain copes naturally with jsonata that is.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 January 2023 09:42 UTC](https://discourse.nodered.org/t/add-a-value-to-payload/73552/7 "2023-01-12T09:42:18Z")

</div>

In a function node it would be

```auto
msg.payload = msg.payload + 5
return msg

```

or if msg.payload is not already a number (but is a string):

```auto
msg.payload = Number(msg.payload) + 5
return msg

```

---

<div class="post-metadata">

**Author:** ![Rhysgh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rhysgh/32/74195_2.png) [@Rhysgh](https://discourse.nodered.org/u/Rhysgh)\
**Post date:** [12 January 2023 13:15 UTC](https://discourse.nodered.org/t/add-a-value-to-payload/73552/8 "2023-01-12T13:15:33Z")

</div>

Thanks, I think i will go with

msg.payload = Math.round(Number(msg.payload)) + 5  
return msg

Appreciate all the help

---

<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:** [26 January 2023 13:16 UTC](https://discourse.nodered.org/t/add-a-value-to-payload/73552/9 "2023-01-26T13:16:15Z")

</div>

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