# Retained message with msg.payload returns Function tried to send a message of type string

**URL:** <https://discourse.nodered.org/t/retained-message-with-msg-payload-returns-function-tried-to-send-a-message-of-type-string/5376>\
**Category:** General\
**Created:** [1 December 2018 13:13 UTC](https://discourse.nodered.org/t/retained-message-with-msg-payload-returns-function-tried-to-send-a-message-of-type-string/5376 "2018-12-01T13:13:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MANNAS3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mannas3/32/4317_2.png) [@MANNAS3](https://discourse.nodered.org/u/MANNAS3)\
**Post date:** [1 December 2018 13:13 UTC](https://discourse.nodered.org/t/retained-message-with-msg-payload-returns-function-tried-to-send-a-message-of-type-string/5376/1 "2018-12-01T13:13:57Z")

</div>

I have a dashboard button node in which it is connected to a function node and defined the context.global.stat to retain the state of the button across function node, also have another mqtt in node to gather sensor data from it, both nodes, the (button --\> function) and the (mqtt in) nodes are connected to a third function node, that I have defined the below code:

```
if( context.global.stat =="on")
{
    return[msg.payload,null];
}

if (context.global.stat == "off")
{
    return[null, null]; 
}

```

What I am after is to return only the values from mqtt in node if the button in the dashboard is "on", so when I run the above it returns the error below when the button is "on":

"Function tried to send a message of type string"

If I replace the return[msg.payload,null]; to return[msg,null]; it first return the msg of the button node then works normally and can read the sensor data from the mqtt in, so what I would like to have only the msg.payload returned for the mqtt in when the button is on.

The function node connected to the button node contains:

context.global.stat = msg.payload;  
return msg;

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/93f2b692458fb364731762aa7f3dff46f5e6ff27.png)

---

<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:** [1 December 2018 13:31 UTC](https://discourse.nodered.org/t/retained-message-with-msg-payload-returns-function-tried-to-send-a-message-of-type-string/5376/2 "2018-12-01T13:31:15Z")

</div>

> [@MANNAS3](#):
>
> return[msg.payload,null];

As the error says, you are trying to return a string. You must always return an object. I think you probably mean  
`return [msg, null];`

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [1 December 2018 20:20 UTC](https://discourse.nodered.org/t/retained-message-with-msg-payload-returns-function-tried-to-send-a-message-of-type-string/5376/3 "2018-12-01T20:20:35Z")

</div>

@MANNAS3 I expect that @Colin's suggestion will eliminate the error message. You might be able to use the `gate` node ([node-red-contrib-simple-gate](https://flows.nodered.org/node/node-red-contrib-simple-gate)) instead of building from scratch.

---

<div class="post-metadata">

**Author:** ![MANNAS3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mannas3/32/4317_2.png) [@MANNAS3](https://discourse.nodered.org/u/MANNAS3)\
**Post date:** [2 December 2018 07:37 UTC](https://discourse.nodered.org/t/retained-message-with-msg-payload-returns-function-tried-to-send-a-message-of-type-string/5376/5 "2018-12-02T07:37:09Z")

</div>

you are correct, and thank you for the guidance, I have used the msg before but was under the impression that I can return msg.payload in case I need to have the value, instead I have defined a new newmsg object to return msg.payload.

Regarding the on status that was returned with the msg, it seems was actually reflected due to the fact that I had another function node in which I have returned msg instead of null, so I had to replace with the below:

context.global.stat = msg.payload;  
return null;

Thank you so much for your help with this!

---

<div class="post-metadata">

**Author:** ![MANNAS3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mannas3/32/4317_2.png) [@MANNAS3](https://discourse.nodered.org/u/MANNAS3)\
**Post date:** [2 December 2018 07:37 UTC](https://discourse.nodered.org/t/retained-message-with-msg-payload-returns-function-tried-to-send-a-message-of-type-string/5376/6 "2018-12-02T07:37:57Z")

</div>

Thank you for the suggestion and will have a look into it 🙂

---

<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:** [2 December 2018 09:13 UTC](https://discourse.nodered.org/t/retained-message-with-msg-payload-returns-function-tried-to-send-a-message-of-type-string/5376/7 "2018-12-02T09:13:58Z")

</div>

> [@MANNAS3](#):
>
> I have defined a new newmsg object to return msg.payload.

There should be no need to make a new message, just set `msg.payload` to what you want and then return msg. That is the normal convention as it means any other attributes are passed through unaffected, which can be useful.
