# New to MQTT... couple Qs

**URL:** https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408
**Category:** General
**Created:** [3 September 2020 15:01 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408 "2020-09-03T15:01:29Z")
**Posts on this page:** 14
**Page:** 2

<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: [3 September 2020 20:55 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408/21 "2020-09-03T20:55:18Z")

</div>

Once you have watched the videos then you need to formulate the problem in a way that you can solve it with node-red. So you might, for example, have a set of requirements like: When I receive a message from the MQTT In node with this content then I want to send a message to the MQTT Out node with some other content. To implement this you might need a Switch node (not the dashboard ui-switch node) to route messages from mqtt down particular paths, then possibly Change nodes to set message content to the required values.  
When you get to that stage it is probably also worth reading the docs page Working with Messages.  
When using a node make sure you look carefully at the help text for the node which will (believe it or not) often have useful information.

---

<div class="post-metadata">

### Author: ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)
#### Post date: [3 September 2020 22:14 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408/22 "2020-09-03T22:14:44Z")

</div>

Agree with Colin that the rest is so much the core Node-RED functionality it would be good to get an overview of what is possible and what are the common ways of solving issues.

However if you're like me and learn best by fast progress, I would believe a single change node after the switch input would do to change all 3 relays at once. It would need to:

- move `msg.switchcmd` to `msg.payload`
- change `msg.payload` from `On` to `ON`
- change `msg.payload` from `Off` to `OFF`

I've not used Tasmota myself so I'm not sure if sending a payload of `ON` to `fan1/relay/POWER` is the right topic/payload combination but maybe you'll get the idea.

---

<div class="post-metadata">

### Author: ![Negativ3](https://avatars.discourse-cdn.com/v4/letter/n/8baadc/32.png) [@Negativ3](https://discourse.nodered.org/u/Negativ3)
#### Post date: [4 September 2020 05:02 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408/23 "2020-09-04T05:02:07Z")

</div>

Thanks a lot, that got me heading in the right direction, although I thing my solution may not be ideal.

Solved a problem and got this welder able to control extraction fans remotely, which is a real bonus.

Thanks to all.

---

<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: [4 September 2020 07:50 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408/24 "2020-09-04T07:50:06Z")

</div>

If you want someone to look at the flow and suggest imporovements then export it and paste it here and I expect someone will that a look. Select the relevant nodes and then Ctrl+E and Copy to Clipboard. Then here use the `</>` button and paste it in.

---

<div class="post-metadata">

### Author: ![Negativ3](https://avatars.discourse-cdn.com/v4/letter/n/8baadc/32.png) [@Negativ3](https://discourse.nodered.org/u/Negativ3)
#### Post date: [4 September 2020 09:51 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408/25 "2020-09-04T09:51:15Z")

</div>

I can do that... this is my change node... basically reducing the string to just on & off.

```auto
[{"id":"eb96c7bb.6b9818","type":"change","z":"ac5c3d8c.1c7c4","name":"Output","rules":[{"t":"change","p":"payload","pt":"msg","from":"On","fromt":"str","to":"ON","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"Off","fromt":"str","to":"OFF","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"switchcmd","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"switchlight","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"command","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"RSSI","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"idx","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":":","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"\"","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"{","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"}","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":",","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"num","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"2","fromt":"num","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"3","fromt":"num","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"4","fromt":"num","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"5","fromt":"num","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"6","fromt":"num","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"7","fromt":"num","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"8","fromt":"num","to":"","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"9","fromt":"num","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":340,"wires":[["543922a7.a9fd6c","945f5fed.7fc","4782895a.5d7b38","aa151564.7c8f88"]]}]

```

---

<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: [4 September 2020 09:58 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408/26 "2020-09-04T09:58:02Z")

</div>

Aargh no, that isn't the way to do it. Add a debug node showing us what you are getting from the MQTT In node and tell us what you want to convert that to.

In node red if it is difficult or messy that very often means you are not doing it the best way.

Edit. Also try setting the Output mode in the MQTT In node to Parsed Json and show us what it gives in a debug node.

---

<div class="post-metadata">

### Author: ![Negativ3](https://avatars.discourse-cdn.com/v4/letter/n/8baadc/32.png) [@Negativ3](https://discourse.nodered.org/u/Negativ3)
#### Post date: [4 September 2020 16:59 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408/27 "2020-09-04T16:59:25Z")

</div>

hi Colin, Hit my 5-post limit earlier... see post#12 for input, and below for MQTT-out node.

```auto
[{"id":"945f5fed.7fc","type":"mqtt out","z":"ac5c3d8c.1c7c4","name":"fan1","topic":"fan1/relay/cmnd/POWER","qos":"2","retain":"","broker":"217c6d87.d2cc82","x":530,"y":280,"wires":[]},{"id":"217c6d87.d2cc82","type":"mqtt-broker","z":"","name":"PI_MQTT","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

---

<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: [4 September 2020 17:13 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408/28 "2020-09-04T17:13:42Z")

</div>

Did you see the edit I made to my post, suggesting changing the output to parsed JSON?

---

<div class="post-metadata">

### Author: ![Negativ3](https://avatars.discourse-cdn.com/v4/letter/n/8baadc/32.png) [@Negativ3](https://discourse.nodered.org/u/Negativ3)
#### Post date: [4 September 2020 17:22 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408/29 "2020-09-04T17:22:49Z")

</div>

Just saw the edit...

```auto
9/5/2020, 12:21:21 AMnode: 543922a7.a9fd6c
control/switch : msg.payload : Object
object
idx: 1
RSSI: 7
command: "switchlight"
switchcmd: "On"

```

---

<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: [4 September 2020 17:44 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408/30 "2020-09-04T17:44:37Z")

</div>

Excellent, now you have a JavaScript object rather than a string. Have you read the docs page working with messages?

---

<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: [4 September 2020 17:50 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408/31 "2020-09-04T17:50:50Z")

</div>

Hold on though, shouldn't your input be the state of something rather than a command, or have I lost track of exactly what you are trying to do?

---

<div class="post-metadata">

### Author: ![Negativ3](https://avatars.discourse-cdn.com/v4/letter/n/8baadc/32.png) [@Negativ3](https://discourse.nodered.org/u/Negativ3)
#### Post date: [4 September 2020 18:01 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408/32 "2020-09-04T18:01:05Z")

</div>

Here's the log output from the ESP32

```auto
3031779: SW : GPIO=18 State=1 Output value=1
3032003: WD : Uptime 51 ConnectFailures 0 FreeMem 207512 WiFiStatus 3
3035379: SW : GPIO=18 State=0 Output value=0
3037579: SW : GPIO=18 State=1 Output value=1
3040478: SW : GPIO=18 State=0 Output value=0
3041978: SW : GPIO=18 State=1 Output value=1
3044478: SW : GPIO=18 State=0 Output value=0

```

and the configuration setup for the GPIO.

 ![ESP_EASY_TOGGLE](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/9/a93a9676062296f9af3304f5109178a305f767b2.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: [4 September 2020 18:39 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408/33 "2020-09-04T18:39:38Z")

</div>

Ok, I see what you are doing.  
So with the the js object you posted and the working with messages page you should be able to make some progress.

---

<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: [18 September 2020 18:39 UTC](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408/34 "2020-09-18T18:39:44Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/new-to-mqtt-couple-qs/32408.md?page=1)
