# How to use switch node to publish an inject node to MQTT Broker

**URL:** <https://discourse.nodered.org/t/how-to-use-switch-node-to-publish-an-inject-node-to-mqtt-broker/46510>\
**Category:** General\
**Created:** [31 May 2021 13:24 UTC](https://discourse.nodered.org/t/how-to-use-switch-node-to-publish-an-inject-node-to-mqtt-broker/46510 "2021-05-31T13:24:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weatherkicksass](https://avatars.discourse-cdn.com/v4/letter/w/2bfe46/32.png) [@weatherkicksass](https://discourse.nodered.org/u/weatherkicksass)\
**Post date:** [31 May 2021 13:24 UTC](https://discourse.nodered.org/t/how-to-use-switch-node-to-publish-an-inject-node-to-mqtt-broker/46510/1 "2021-05-31T13:24:12Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/9/89008e35b3b9538efe2d852cfc056525987a73e6.png)

So, I am getting a temperature data from the MQTT broker and I want to publish a message which will overwrite a data that is on an OPC server, which looks like this;

"[{"id":"Visu+ UA Server.Visu+ SCADA.Tags.Start\_Button","v":false}]"

When I use the inject node that is connected to the MQTT out node, it works and changes the data. But I only want it to work automatically when the other (temperature) data coming from the MQTT broker is less than "50". I used the switch node for this and I am able to get output when the comparison happens. My problem is, how do I use this "switch" node to inject a message into MQTT out node. I'm also copying the flow code here. Thanks in advance.

```auto
[{"id":"97527151.57977","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"c8c0178d.388638","type":"mqtt in","z":"97527151.57977","name":"","topic":"iotgateway","qos":"1","datatype":"json","broker":"42bbbb0d.1b4ff4","nl":false,"rap":true,"rh":0,"x":460,"y":480,"wires":[["ca4e64d.53f1198"]]},{"id":"bcdd863a.a8cb18","type":"mqtt out","z":"97527151.57977","name":"","topic":"iotgateway/write","qos":"1","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"42bbbb0d.1b4ff4","x":940,"y":200,"wires":[]},{"id":"b7ac1935.42ee18","type":"inject","z":"97527151.57977","name":"Start_Value=False","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Message","payload":"[{\"id\":\"Visu+ UA Server.Visu+ SCADA.Tags.Start_Button\",\"v\":false}]","payloadType":"str","x":490,"y":200,"wires":[["bcdd863a.a8cb18"]]},{"id":"ca4e64d.53f1198","type":"switch","z":"97527151.57977","name":"","property":"payload.values[0].v","propertyType":"msg","rules":[{"t":"lt","v":"50","vt":"str"},{"t":"gte","v":"50","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":650,"y":480,"wires":[["d7bba128.d4f7a"],["7db978c6.f2df18"]]},{"id":"7db978c6.f2df18","type":"debug","z":"97527151.57977","name":">=50","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.values[0].v","targetType":"msg","statusVal":"","statusType":"auto","x":870,"y":560,"wires":[]},{"id":"d7bba128.d4f7a","type":"debug","z":"97527151.57977","name":">50","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.values[0].v","targetType":"msg","statusVal":"","statusType":"auto","x":870,"y":400,"wires":[]},{"id":"42bbbb0d.1b4ff4","type":"mqtt-broker","name":"","broker":"tcp://localhost","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

```

---

<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:** [31 May 2021 14:19 UTC](https://discourse.nodered.org/t/how-to-use-switch-node-to-publish-an-inject-node-to-mqtt-broker/46510/2 "2021-05-31T14:19:36Z")

</div>

If I understand you, you should add a `change` node on each side of the `switch` node to setup the msg and topic to be sent, then connect the output of the `change` nodes to the mqtt-out node.

---

<div class="post-metadata">

**Author:** ![weatherkicksass](https://avatars.discourse-cdn.com/v4/letter/w/2bfe46/32.png) [@weatherkicksass](https://discourse.nodered.org/u/weatherkicksass)\
**Post date:** [31 May 2021 16:55 UTC](https://discourse.nodered.org/t/how-to-use-switch-node-to-publish-an-inject-node-to-mqtt-broker/46510/3 "2021-05-31T16:55:31Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/82257b106d12248a198e7751f5bcfe105e89fccd.png)

I tried to do it like this but nothing's happening. I couldn't understand what you meant by adding a change node before the switch. What should it do before the switch?

```auto
[{"id":"97527151.57977","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"c8c0178d.388638","type":"mqtt in","z":"97527151.57977","name":"","topic":"iotgateway","qos":"1","datatype":"json","broker":"42bbbb0d.1b4ff4","nl":false,"rap":true,"rh":0,"x":320,"y":280,"wires":[["ca4e64d.53f1198","6c5f1de5.8bec34","5e97d1a0.63993"]]},{"id":"bcdd863a.a8cb18","type":"mqtt out","z":"97527151.57977","name":"","topic":"iotgateway/write","qos":"1","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"42bbbb0d.1b4ff4","x":1100,"y":160,"wires":[]},{"id":"b7ac1935.42ee18","type":"inject","z":"97527151.57977","name":"Start_Value=False","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Message","payload":"[{\"id\":\"Visu+ UA Server.Visu+ SCADA.Tags.Start_Button\",\"v\":false}]","payloadType":"json","x":490,"y":160,"wires":[["bcdd863a.a8cb18"]]},{"id":"ca4e64d.53f1198","type":"switch","z":"97527151.57977","name":"","property":"payload.values[0].v","propertyType":"msg","rules":[{"t":"lt","v":"50","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":550,"y":280,"wires":[["87f6ca7a.00d4f8"]]},{"id":"87f6ca7a.00d4f8","type":"change","z":"97527151.57977","name":"","rules":[{"t":"set","p":"payload.values[0].v","pt":"msg","to":"[{\"id\":\"Visu+ UA Server.Visu+ SCADA.Tags.Start_Button\",\"v\":false}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":280,"wires":[["bcdd863a.a8cb18"]]},{"id":"6c5f1de5.8bec34","type":"debug","z":"97527151.57977","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.values[0].v","targetType":"msg","statusVal":"","statusType":"auto","x":610,"y":400,"wires":[]},{"id":"5e97d1a0.63993","type":"debug","z":"97527151.57977","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":480,"wires":[]},{"id":"42bbbb0d.1b4ff4","type":"mqtt-broker","name":"","broker":"tcp://localhost","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

```

---

<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:** [31 May 2021 17:33 UTC](https://discourse.nodered.org/t/how-to-use-switch-node-to-publish-an-inject-node-to-mqtt-broker/46510/4 "2021-05-31T17:33:56Z")

</div>

Sorry,I meant after. I have a head cold and the neurons slow down going thru all the congestion.

This is what I mean.

```auto
[{"id":"97527151.57977","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"c8c0178d.388638","type":"mqtt in","z":"97527151.57977","name":"","topic":"iotgateway","qos":"1","datatype":"json","broker":"42bbbb0d.1b4ff4","x":460,"y":480,"wires":[["ca4e64d.53f1198"]]},{"id":"bcdd863a.a8cb18","type":"mqtt out","z":"97527151.57977","name":"","topic":"iotgateway/write","qos":"1","retain":"","broker":"42bbbb0d.1b4ff4","x":940,"y":200,"wires":[]},{"id":"b7ac1935.42ee18","type":"inject","z":"97527151.57977","name":"Start_Value=False","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Message","payload":"[{\"id\":\"Visu+ UA Server.Visu+ SCADA.Tags.Start_Button\",\"v\":false}]","payloadType":"str","x":490,"y":200,"wires":[["bcdd863a.a8cb18"]]},{"id":"ca4e64d.53f1198","type":"switch","z":"97527151.57977","name":"","property":"payload.values[0].v","propertyType":"msg","rules":[{"t":"lt","v":"50","vt":"str"},{"t":"gte","v":"50","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":650,"y":480,"wires":[["d7bba128.d4f7a","85b3c62.bcb5938"],["7db978c6.f2df18"]]},{"id":"7db978c6.f2df18","type":"debug","z":"97527151.57977","name":">=50","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.values[0].v","targetType":"msg","statusVal":"","statusType":"auto","x":870,"y":560,"wires":[]},{"id":"d7bba128.d4f7a","type":"debug","z":"97527151.57977","name":">50","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.values[0].v","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":480,"wires":[]},{"id":"85b3c62.bcb5938","type":"change","z":"97527151.57977","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"id\":\"Visu+ UA Server.Visu+ SCADA.Tags.Start_Button\",\"v\":false}]","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":380,"wires":[["bcdd863a.a8cb18"]]},{"id":"42bbbb0d.1b4ff4","type":"mqtt-broker","name":"","broker":"tcp://localhost","port":"1883","clientid":"","usetls":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

---

<div class="post-metadata">

**Author:** ![weatherkicksass](https://avatars.discourse-cdn.com/v4/letter/w/2bfe46/32.png) [@weatherkicksass](https://discourse.nodered.org/u/weatherkicksass)\
**Post date:** [2 June 2021 06:41 UTC](https://discourse.nodered.org/t/how-to-use-switch-node-to-publish-an-inject-node-to-mqtt-broker/46510/5 "2021-06-02T06:41:52Z")

</div>

Oh it worked! Thank you very much 🙂 Hope you'll get better
