# Value to MQTT Topic

**URL:** <https://discourse.nodered.org/t/value-to-mqtt-topic/26549>\
**Category:** General\
**Created:** [12 May 2020 18:55 UTC](https://discourse.nodered.org/t/value-to-mqtt-topic/26549 "2020-05-12T18:55:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Demitos](https://avatars.discourse-cdn.com/v4/letter/d/7ab992/32.png) [@Demitos](https://discourse.nodered.org/u/Demitos)\
**Post date:** [12 May 2020 18:55 UTC](https://discourse.nodered.org/t/value-to-mqtt-topic/26549/1 "2020-05-12T18:55:47Z")

</div>

I'm sure this is some type of newb thing.

![picture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/243d298398ea2bccc6abc2dcee62391e705e5d74.png)

I am sending a message to the Telegram reciver node via telegram. If this message has a fixed content, the function node should pass a string with "1" to the mqtt out node. However, I always get the following error message.

![picture2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/9/69026022737b86833c1ae93f109d3b9146b7468e.png)

But if i try to inject a string that contains "1" manualy using the inject node, it works fine. I dont know what to do about it.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 May 2020 19:06 UTC](https://discourse.nodered.org/t/value-to-mqtt-topic/26549/2 "2020-05-12T19:06:31Z")

</div>

Put a debug before the function. What is going into it?

Copy and paste the function code

`````  
`like this`  
`````

Can't help otherwise!

Edit ...

A guess, you are not returning an object at the end of the function...?

---

<div class="post-metadata">

**Author:** ![Demitos](https://avatars.discourse-cdn.com/v4/letter/d/7ab992/32.png) [@Demitos](https://discourse.nodered.org/u/Demitos)\
**Post date:** [12 May 2020 19:37 UTC](https://discourse.nodered.org/t/value-to-mqtt-topic/26549/3 "2020-05-12T19:37:21Z")

</div>

Here you can see the output from the debug:

![picture3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b7c6f281775ed7186233ba89953e18e8ec2c2ec6.png)

The function contains this code here:

if (msg.payload.content=="1")  
msg.payload = "1";  
return msg.payload;

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [12 May 2020 20:18 UTC](https://discourse.nodered.org/t/value-to-mqtt-topic/26549/4 "2020-05-12T20:18:43Z")

</div>

In the function node you want to return `msg` not `msg.payload`

---

<div class="post-metadata">

**Author:** ![Demitos](https://avatars.discourse-cdn.com/v4/letter/d/7ab992/32.png) [@Demitos](https://discourse.nodered.org/u/Demitos)\
**Post date:** [12 May 2020 20:45 UTC](https://discourse.nodered.org/t/value-to-mqtt-topic/26549/5 "2020-05-12T20:45:16Z")

</div>

If I change it the way you suggest, I get this error again:

![picture4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d44462b482bdf6eae6bac46965a7fb757357f317.png)

The code inside the function now looks like this:

if (msg.payload.content=="1")  
msg = "1";  
return msg;

---

<div class="post-metadata">

**Author:** ![Onixarts](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/onixarts/32/6052_2.png) [@Onixarts](https://discourse.nodered.org/u/Onixarts)\
**Post date:** [12 May 2020 20:56 UTC](https://discourse.nodered.org/t/value-to-mqtt-topic/26549/6 "2020-05-12T20:56:40Z")

</div>

Any message returned by function node should have at least this structure:

```auto
return { payload: "1" }

```

Which will create new msg object with payload property set to string "1".  
If You want modify and return the original msg then:

```auto
msg.payload = "1"
return msg

```

So in Your case:

```auto
if(msg.payload.content === "1")
   msg.payload = "1"
else
   return // this will break the flow, and send nothing to the output

return msg

```

---

<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 May 2020 21:04 UTC](https://discourse.nodered.org/t/value-to-mqtt-topic/26549/7 "2020-05-12T21:04:31Z")

</div>

Have a good read of the node red doc Working with Messages, a little time spent doing that will save you many hours very quickly. Also look through the other documents, Writing Functions in particular.  
[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

---

<div class="post-metadata">

**Author:** ![Demitos](https://avatars.discourse-cdn.com/v4/letter/d/7ab992/32.png) [@Demitos](https://discourse.nodered.org/u/Demitos)\
**Post date:** [12 May 2020 21:13 UTC](https://discourse.nodered.org/t/value-to-mqtt-topic/26549/8 "2020-05-12T21:13:52Z")

</div>

Okay, I have tried out all of your suggestion. With this help it works as intended. Thanks for every comment.

---

<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 May 2020 21:13 UTC](https://discourse.nodered.org/t/value-to-mqtt-topic/26549/9 "2020-05-26T21:13:52Z")

</div>

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