Toggle switch based on payload

Hi everyone,

New here, new to node red, new to mqtt, new to IoT, .... so bare with me :slight_smile:

I’m puzzled with the following:
I just started today with the dashboard and there’s something I can’t figure out. I have a dashboard with one “switch”. The switch publishes trui/false to my Mosquitto broker. This part functions.
Also I can read without issue from that topic and put true/false into a text box.
Now what I would like to do is have the switch change position if a third party writes to the same topic (in this case an ESP module) with a mechanical switch connected to it.

My idea at first was to have the mqtt input at the left-hand side of the switch and the mqtt output on the right-hand side. But when I deploy the mqtt pub seems to go into an endless loop (I have a Putty terminal with a subscription to that topic to see what happens). The value changes from true to false when I toggle, but instead of a single pub it just goes on and on.

Can someone shed a light here?

Thanks in advance

I suspect you have created an MQTT loop (subscribing to a topic (or wildcard) you then write to causing an endless loop)

Check the dashboard switch is not set to pass incoming msg through.

If that's not the solution, post a screenshot of your flow and export your flow and paste it in a reply (click the code format button then paste code where it tells you)

That's a good question and there is a trick to it if certain things are done.

So, as I read it:
You send two messages. One to turn it on and the other to turn it off.

There is a quirk with MQTT and sending messages.
All messages become a string.
So really you can't send true and false, because when they are received the become "true" and "false".

It is a subtle difference, but enough to trip a new person.

There is also a trick with the switch node.

You have to set it so the node indicates the input and to untick the "if message is received, send it to output."

Here is a very basic flow which does what I think you want to do.

What you have to understand is it is not using true and false to signal.

[{"id":"19c72ec5.517ec1","type":"debug","z":"b9924a74.4d98f8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":747,"y":3999,"wires":[]},{"id":"37622de9.6886b2","type":"mqtt in","z":"b9924a74.4d98f8","name":"","topic":"test","qos":"2","datatype":"auto","broker":"378c0403.8cda04","x":587,"y":3999,"wires":[["19c72ec5.517ec1"]]},{"id":"6cedf4eb.ad6e94","type":"ui_switch","z":"b9924a74.4d98f8","name":"","label":"switch","tooltip":"","group":"53792891.774238","order":12,"width":"3","height":"1","passthru":false,"decouple":"true","topic":"","style":"","onvalue":"1","onvalueType":"str","onicon":"","oncolor":"","offvalue":"0","offvalueType":"str","officon":"","offcolor":"","x":270,"y":4000,"wires":[["dd8652.089a79b","19075bb6.a11ea4"]]},{"id":"dd8652.089a79b","type":"mqtt out","z":"b9924a74.4d98f8","name":"","topic":"test","qos":"","retain":"","broker":"378c0403.8cda04","x":440,"y":4000,"wires":[]},{"id":"4c072efb.0d50f","type":"mqtt in","z":"b9924a74.4d98f8","name":"","topic":"test","qos":"2","datatype":"auto","broker":"378c0403.8cda04","x":130,"y":4000,"wires":[["6cedf4eb.ad6e94","3be4856c.34e62a"]]},{"id":"3be4856c.34e62a","type":"debug","z":"b9924a74.4d98f8","name":"Receive","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":280,"y":3960,"wires":[]},{"id":"19075bb6.a11ea4","type":"debug","z":"b9924a74.4d98f8","name":"Send","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":440,"y":3960,"wires":[]},{"id":"79fce463.fe6a24","type":"inject","z":"b9924a74.4d98f8","name":"","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":4130,"wires":[["13abeb5b.ca4aed"]]},{"id":"40f8573a.2fee88","type":"inject","z":"b9924a74.4d98f8","name":"","topic":"","payload":"0","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":4170,"wires":[["13abeb5b.ca4aed"]]},{"id":"13abeb5b.ca4aed","type":"mqtt out","z":"b9924a74.4d98f8","name":"","topic":"test","qos":"","retain":"","broker":"378c0403.8cda04","x":280,"y":4150,"wires":[]},{"id":"edf5d0c5.0c1d18","type":"comment","z":"b9924a74.4d98f8","name":"ESP device","info":"","x":680,"y":3960,"wires":[]},{"id":"a2520722.8a9978","type":"comment","z":"b9924a74.4d98f8","name":"Other device sending on/off signals.","info":"","x":220,"y":4090,"wires":[]},{"id":"378c0403.8cda04","type":"mqtt-broker","z":"","name":"MQTT HOST","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"53792891.774238","type":"ui_group","name":"Group 7","tab":"de5134a7.f0a0d","order":4,"disp":true,"width":6},{"id":"de5134a7.f0a0d","type":"ui_tab","name":"Tab 6","icon":"dashboard","order":5}]

After you have seen it working, change the inject nodes and the switch to using/sending true and false, and activate the two debug nodes: Send and Receive and see the difference.

You will have to edit the MQTT nodes to use your server.
And maybe edit where the switch is. But that is more dressing than important.

1 Like

There is no need for all that complexity. The switch node to MQTT is already working, as I believe is the mechanical switch to MQTT. All it needs is to feed the MQTT In into the ui-switch, disable Pass Through in the node, and set it to Show State Of Input.

Which complexity?

I added a few nodes to show the structure of what to do.

The real work/magic was done in the switch node - as you agreed.

The actual message is also important, because as I explained sending true over MQTT you get "true" at the other end which is not the same.

The OP already has that working.

I'm walking away.

Probably off topic but if you want to pass boolean true or false across MQTT all you have to do is to set the MQTT In node to parse the incoming value as JSON and will give you boolean true/false out.

Apologies are required from me however, I misinterpreted your comments as suggesting that additional nodes were required to answer the question. I did not realise that your flow also included all the extra mqtt flows not actually associated with the question. I should have looked at it not just assumed that the fact that it contained numerous nodes meant that you had added complexity.

No problems. All good.

I just was wanting to show how it worked and not just say things that may have been misunderstood.

1 Like

Thx for you time to reply. So my basic idea was correct, I only missed some detail in the switch config. Cool.

Only thing now: My dashboard ui page is just blank. Not sure if it really has something to do with the code I just copy/pasted in...

I'm running node-red of a docker if that may be of any concern. Which I stopped last night. Do I need to restart the dashboard separately? Or should it start at the same time when I launch the Docker image? (Yesterday it lauched automatically aafter I had installed it from the "manage palette" menu)
Agreed this is kinda off topic...

Forget about that last part... seems to be browser caching issue... just restarted FireFox and all is well

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