# How to add payload to a posted MQTT topic?

**URL:** <https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143>\
**Category:** General\
**Created:** [21 March 2022 11:53 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143 "2022-03-21T11:53:42Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![muharremsari](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/muharremsari/32/7996_2.png) [@muharremsari](https://discourse.nodered.org/u/muharremsari)\
**Post date:** [21 March 2022 11:53 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143/1 "2022-03-21T11:53:42Z")

</div>

I have a float OPCDA signal from PLC

I want add payload to a posted MQTT topic

Regards

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [21 March 2022 12:32 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143/2 "2022-03-21T12:32:17Z")

</div>

May be you should post an example. What is the payload which comes out of your mqtt node and what should be added?

---

<div class="post-metadata">

**Author:** ![muharremsari](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/muharremsari/32/7996_2.png) [@muharremsari](https://discourse.nodered.org/u/muharremsari)\
**Post date:** [21 March 2022 13:13 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143/4 "2022-03-21T13:13:12Z")

</div>

`Preformatted text`

```auto
[{"id":"b261f6b8f06331ab","type":"inject","z":"d2279fc8909d5ffb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Channel1.Device1.PMMTrkPyroStat1Temp","payload":"85","payloadType":"str","x":110,"y":60,"wires":[["8cbef9d81b8fdded","9b8b99ee95277bcc"]]}]

```

`Preformatted text`

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [21 March 2022 13:20 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143/5 "2022-03-21T13:20:40Z")

</div>

> I want add payload to a posted MQTT topic

This is your topic: `"Channel1.Device1.PMMTrkPyroStat1Temp"`  
And this is your payload: `"85"`

If you add them together you get `"Channel1.Device1.PMMTrkPyroStat1Temp85"`

If the above is _not what you are trying to achieve_ you will have to explain better.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [21 March 2022 13:32 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143/6 "2022-03-21T13:32:24Z")

</div>

This is what you posted:

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

Is it correct that you got a string as output (or is it a number ? ) and what you want to add? A number, a string, an object???

---

<div class="post-metadata">

**Author:** ![muharremsari](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/muharremsari/32/7996_2.png) [@muharremsari](https://discourse.nodered.org/u/muharremsari)\
**Post date:** [21 March 2022 13:39 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143/7 "2022-03-21T13:39:29Z")

</div>

I see value client machine.

But i want get from opcclient like this address

[{"id":"Channel1.Device1.PMMTrkPyroStat1Temp","v":" **Floating Value From OPCDA**"}]

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/19942494cc4923cb37132ccde7352798205a33cd.png)

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [21 March 2022 13:47 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143/8 "2022-03-21T13:47:18Z")

</div>

> [@muharremsari](#):
>
> {"id":"Channel1.Device1.PMMTrkPyroStat1Temp","v":" **Floating Value From OPCDA**"}

OK i am not sure if I understand correctly - but may be you meant this:

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

So use a change Node like this:

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

If you want that as JSON String put a JSON Node in as well:

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [21 March 2022 13:57 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143/9 "2022-03-21T13:57:39Z")

</div>

![Untitled 7](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d5d1cf95e8e0f6df9d826d82c276a083a00141e0.jpeg)

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [21 March 2022 14:04 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143/10 "2022-03-21T14:04:00Z")

</div>

What is the difference - using a function node - instead of change node???? - More complex is always possible.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [21 March 2022 14:28 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143/11 "2022-03-21T14:28:51Z")

</div>

I am not very familiar with Jsonata so I naturally reach for a function node.

There is no way I could convert the string to an object, convert the number to floating point and add it as a message property in Jsonata.

I expect smarter people could, but if they did, how easy would the code be to understand in 6 month's time?

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [21 March 2022 21:46 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143/12 "2022-03-21T21:46:03Z")

</div>

There are ways to convert in JSONATA, but that is not the topic. The payload is assigned without any modification to a property of an object.

But even if you do not like to use JSONATA - it is still much easier to use a change node which does the same as your function node. Function nodes needs coding. In my point of view function nodes should be avoided when core nodes can be used instead. Otherwise I do not need NodeRed. Then I have a trigger and an output and between instead of designing a flow with all the transparence of the logical flow - I do all programming in a function node in Javascript. Then I am back to text based coding.

Here a solution - using the topic as you did in your function node:

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

and this without JSONATA - just using the capabilities of a change node.

Sorry for using a localized version of NodeRed - so translated: MOVE is used in the Change Node.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [21 March 2022 22:02 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143/13 "2022-03-21T22:02:34Z")

</div>

There are always lots of ways to solve any programming problem.  
I think your change mode version is better than mine. 😀

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [22 March 2022 07:24 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143/14 "2022-03-22T07:24:53Z")

</div>

> [@mickym2](#):
>
> Function nodes needs coding. In my point of view function nodes should be avoided when core nodes can be used instead. Otherwise I do not need NodeRed

As soon as you start fiddeling with JSONATA, even inside a standard node, that is in itself coding. From that perspective, no difference, just a matter of taste & preference. In addition, it has been mentioned several times that a function node with code has better performance than an equivalent JSONATA expression. Regardless, the last example you provided seems to be the best & straight forward!

---

<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:** [5 April 2022 07:25 UTC](https://discourse.nodered.org/t/how-to-add-payload-to-a-posted-mqtt-topic/60143/15 "2022-04-05T07:25:17Z")

</div>

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