# If and return messages from mqtt

**URL:** https://discourse.nodered.org/t/if-and-return-messages-from-mqtt/64839
**Category:** General
**Tags:** function-node, mqtt
**Created:** [8 July 2022 13:23 UTC](https://discourse.nodered.org/t/if-and-return-messages-from-mqtt/64839 "2022-07-08T13:23:15Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Erissin\_Industria4.0](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Erissin\_Industria4.0](https://discourse.nodered.org/u/Erissin_Industria4.0)
#### Post date: [8 July 2022 13:23 UTC](https://discourse.nodered.org/t/if-and-return-messages-from-mqtt/64839/1 "2022-07-08T13:23:15Z")

</div>

How to return message from a payload with boolean true value, but only if it is equal to what I set?

---

<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: [8 July 2022 14:47 UTC](https://discourse.nodered.org/t/if-and-return-messages-from-mqtt/64839/2 "2022-07-08T14:47:02Z")

</div>

Would you expand you issue a bit please  
How does MQTT fit in?

You should be able to use a switch node to test the value and then a change to set the value you want.

---

<div class="post-metadata">

### Author: ![Erissin\_Industria4.0](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Erissin\_Industria4.0](https://discourse.nodered.org/u/Erissin_Industria4.0)
#### Post date: [8 July 2022 16:52 UTC](https://discourse.nodered.org/t/if-and-return-messages-from-mqtt/64839/3 "2022-07-08T16:52:56Z")

</div>

I managed to do it, but using several nodes, How would I do it using only the function node?

My program:

`[{"id":"f9095345dc0c2946","type":"switch","z":"6b0282876aa2ecc3","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Liga","vt":"str"},{"t":"eq","v":"Desliga","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":350,"y":160,"wires":[["ecd87482388e507e"],["57195910e1cca66b"]]},{"id":"b323ec2241fbfad9","type":"mqtt in","z":"6b0282876aa2ecc3","name":"","topic":"PISTAO","qos":"2","datatype":"auto","broker":"c6a90f85a98db3a6","nl":false,"rap":true,"rh":0,"inputs":0,"x":230,"y":160,"wires":[["f9095345dc0c2946"]]},{"id":"57195910e1cca66b","type":"convert","z":"6b0282876aa2ecc3","name":"","convertTo":"boolean (0/1)","x":530,"y":180,"wires":[["baf7742df2534949"]]},{"id":"ecd87482388e507e","type":"change","z":"6b0282876aa2ecc3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":100,"wires":[["baf7742df2534949"]]},{"id":"baf7742df2534949","type":"s7 out","z":"6b0282876aa2ecc3","endpoint":"936469fb997a5f75","variable":"PISTAO","name":"","x":880,"y":160,"wires":[]},{"id":"c6a90f85a98db3a6","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"936469fb997a5f75","type":"s7 endpoint","transport":"iso-on-tcp","address":"192.168.0.101","port":"102","rack":"0","slot":"2","localtsaphi":"01","localtsaplo":"00","remotetsaphi":"01","remotetsaplo":"00","connmode":"rack-slot","adapter":"","busaddr":"2","cycletime":"1000","timeout":"2000","name":"","vartable":[{"addr":"M0.0","name":"Start"},{"addr":"M0.1","name":"RESET"},{"addr":"M0.2","name":"PISTAO"}]}]`

---

<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: [8 July 2022 17:09 UTC](https://discourse.nodered.org/t/if-and-return-messages-from-mqtt/64839/4 "2022-07-08T17:09:58Z")

</div>

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

I edited your post to fix it.

In a `function` node you could do something like this:

```auto
if (msg.payload == 'Liga') {
    msg.payload = true;
}
if (msg.payload == 'Desliga') {
    msg.payload = 1;
}

return msg;

```

I'm unsure if you want the scond one to be a 1 or 0

---

<div class="post-metadata">

### Author: ![Erissin\_Industria4.0](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Erissin\_Industria4.0](https://discourse.nodered.org/u/Erissin_Industria4.0)
#### Post date: [8 July 2022 17:18 UTC](https://discourse.nodered.org/t/if-and-return-messages-from-mqtt/64839/5 "2022-07-08T17:18:17Z")

</div>

"ReferenceError: msg is not defined"  
It's giving this error, by the way where can I learn to program like this?

---

<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: [8 July 2022 17:46 UTC](https://discourse.nodered.org/t/if-and-return-messages-from-mqtt/64839/6 "2022-07-08T17:46:58Z")

</div>

Put a debug node (set to display the complete msg object) on the output of the node feeding the function node and show the results.

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

Take a JavaScript tutorial like this

> **[JavaScript Tutorial](https://www.w3schools.com/js/)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

---

<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: [22 July 2022 17:47 UTC](https://discourse.nodered.org/t/if-and-return-messages-from-mqtt/64839/7 "2022-07-22T17:47:44Z")

</div>

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