Problem with MQTT and switch update

Hey everybody,

I have a problem with changing the state of my switch. Some words about the setup: I am using a smart plug controlled via MQTT to switch a plug. This works so far:


The problem: I am also switching the plug using my Alexa. If this happens, the switch is in the wrong position, obviously ... So I connect the MQTT-input "couchlicht" to the switch "couchlich".
What happens: Alexa can't switch, because it is forced... What I mean: Plug:OFF, say Alexa to turn it on, turns on, but turns OFF again immediately - or even worse: turns OFF and ON >10 times per second ...
So I tried to change the option of the switch to
Pass though msg if payload matches new state FALSE
Switch icon shows state of the input
In this case, the state of the switch is updated sometime, sometimes it sticks to OFF, don't know why ...

Also I can't switch in the dashboard anymore. With "... shows state of the output" it's vice versa.
So how can I update the switch properly?
Hope you can help me!

which version of the dashboard do you have ? - there were some recent fixes to this node.

2.16.0 - is this the problem?

What may help is rather than using the switch as both input and indication, use two switch nodes.
There is also is/was a thing about the switch node having two modes.
In one it indicates what is sent to it and the other it is a source for the output.

(@dceejay will be able to give you a better run down than I can.)

Then, with two switch nodes, one can be used simply as an indicator and one as an input.
That is a way around the problem you describe. Though not very nice, I know.

That worked until version 2.14 but with version 2.16 one solution is to do it this way:

switch216

switch%20setting216

[{"id":"4861d5bb.68746c","type":"ui_switch","z":"1d3866f1.0e8e09","name":"","label":"control","tooltip":"","group":"8bcf4352.7afe4","order":1,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":350,"y":220,"wires":[["bdf40b8e.65fbd8","97b60c95.049bf"]]},{"id":"6592697e.812898","type":"inject","z":"1d3866f1.0e8e09","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":240,"wires":[["4861d5bb.68746c"]]},{"id":"f5144e2e.25a0a","type":"inject","z":"1d3866f1.0e8e09","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":200,"wires":[["4861d5bb.68746c"]]},{"id":"bdf40b8e.65fbd8","type":"debug","z":"1d3866f1.0e8e09","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":220,"wires":[]},{"id":"97b60c95.049bf","type":"link out","z":"1d3866f1.0e8e09","name":"","links":["5ea10aea.6790d4"],"x":475,"y":160,"wires":[]},{"id":"5ea10aea.6790d4","type":"link in","z":"1d3866f1.0e8e09","name":"","links":["97b60c95.049bf"],"x":235,"y":160,"wires":[["4861d5bb.68746c"]]},{"id":"8bcf4352.7afe4","type":"ui_group","z":"","name":"Control","tab":"1ef6b756.7d01a9","order":3,"disp":true,"width":"6","collapse":false},{"id":"1ef6b756.7d01a9","type":"ui_tab","z":"","name":"Greenhouse","icon":"dashboard","order":5}]
1 Like

If it works, good.

Thumbs up.

Hey,

thanks for the reply. Unfortunately this does not work, since when switching by Alexa, the state of the switch does not change ...


Any ideas?

Just to catch up:

The switch node......

Do you have the button set to pass input to output?

If you don't, that may be a problem.

(If you want post those node's flow)
6 nodes isn't too much.

If I pass the input through, I have the problem that the state of the switch is changed very of then every second, which results in an on/off/on/off/.. - loop and the light is switched on an off all the time...

Did you try the example I uploaded? does it work?

Unforunately not. As you see in my comment above, MQTT is "OFF", but the switch stays "ON" on both sides...

Not sure I understand you, but in my example I send true or false not on or off.

Well okay ... I changed the ON/OFF to true/false ... But that is obviosly not the problem, since it is still not working.
If the state of the lamp (MQTT) ist changed externaly (e.g. via Alexa), the button does not change, although the MQTT sends an input

Here are the nodes:
[{"id":"9f23ef6d.eac848","type":"mqtt in","z":"86e742aa.4f6e","name":"couchlicht","topic":"stat/couchlicht/POWER","qos":"0","datatype":"auto","broker":"787a048.474047c","x":260,"y":40,"wires":[["bbed3b5c.101e9","d3254fdf.63399"]]},{"id":"50bf574b.ec736","type":"mqtt out","z":"86e742aa.4f6e","name":"Couchlicht","topic":"cmnd/couchlicht/POWER","qos":"","retain":"","broker":"787a048.474047c","x":510,"y":140,"wires":[]},{"id":"bbed3b5c.101e9","type":"debug","z":"86e742aa.4f6e","name":"mqttCouchlichState","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":530,"y":40,"wires":[]},{"id":"1cbfe08f.4e2137","type":"debug","z":"86e742aa.4f6e","name":"CouchlichtSwitch","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":530,"y":200,"wires":[]},{"id":"1a2ee7e9.2fa3e","type":"ui_switch","z":"86e742aa.4f6e","name":"","label":"Couchlicht","tooltip":"","group":"9e7276e6.5d1f4","order":1,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":270,"y":140,"wires":[["50bf574b.ec736","1cbfe08f.4e2137","e64cc813.63c35"]]},{"id":"4df591d7.ec4c88","type":"inject","z":"86e742aa.4f6e","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":160,"wires":[["1a2ee7e9.2fa3e"]]},{"id":"8bc902bf.8c9678","type":"inject","z":"86e742aa.4f6e","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":120,"wires":[["1a2ee7e9.2fa3e"]]},{"id":"d3254fdf.63399","type":"change","z":"86e742aa.4f6e","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"ON","fromt":"str","to":"true","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"OFF","fromt":"str","to":"false","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":100,"wires":[["233a75a5.4367d2","1a2ee7e9.2fa3e"]]},{"id":"233a75a5.4367d2","type":"debug","z":"86e742aa.4f6e","name":"mqttCouchlichState","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":530,"y":100,"wires":[]},{"id":"e64cc813.63c35","type":"link out","z":"86e742aa.4f6e","name":"","links":["e3ddbc39.5b98f8"],"x":340,"y":200,"wires":[]},{"id":"e3ddbc39.5b98f8","type":"link in","z":"86e742aa.4f6e","name":"","links":["e64cc813.63c35"],"x":220,"y":200,"wires":[["1a2ee7e9.2fa3e"]]},{"id":"787a048.474047c","type":"mqtt-broker","z":"","name":"mqttserver","broker":"10.0.0.17","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"9e7276e6.5d1f4","type":"ui_group","z":"","name":"Wohnzimmer","tab":"5e73d01b.c77cd","disp":true,"width":6,"collapse":false},{"id":"5e73d01b.c77cd","type":"ui_tab","z":"","name":"Licht","icon":"dashboard","disabled":false,"hidden":false}]

change the setting in your change node (Boolean not String):

1 Like

That has done the trick!! Thanks very much, now this actually quite little thing which cost me hours is finally working!!
Thaaaank you!

That's the working example:

[{"id":"9f23ef6d.eac848","type":"mqtt in","z":"86e742aa.4f6e","name":"couchlicht","topic":"stat/couchlicht/POWER","qos":"0","datatype":"auto","broker":"787a048.474047c","x":260,"y":40,"wires":[["bbed3b5c.101e9","d3254fdf.63399"]]},{"id":"50bf574b.ec736","type":"mqtt out","z":"86e742aa.4f6e","name":"Couchlicht","topic":"cmnd/couchlicht/POWER","qos":"","retain":"","broker":"787a048.474047c","x":510,"y":140,"wires":[]},{"id":"bbed3b5c.101e9","type":"debug","z":"86e742aa.4f6e","name":"mqttCouchlichState","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":530,"y":40,"wires":[]},{"id":"1cbfe08f.4e2137","type":"debug","z":"86e742aa.4f6e","name":"CouchlichtSwitch","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":530,"y":200,"wires":[]},{"id":"1a2ee7e9.2fa3e","type":"ui_switch","z":"86e742aa.4f6e","name":"","label":"Couchlicht","tooltip":"","group":"9e7276e6.5d1f4","order":1,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":270,"y":140,"wires":[["50bf574b.ec736","1cbfe08f.4e2137","e64cc813.63c35"]]},{"id":"233a75a5.4367d2","type":"debug","z":"86e742aa.4f6e","name":"mqttCouchlichState","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":530,"y":100,"wires":[]},{"id":"e64cc813.63c35","type":"link out","z":"86e742aa.4f6e","name":"","links":["e3ddbc39.5b98f8"],"x":340,"y":200,"wires":[]},{"id":"e3ddbc39.5b98f8","type":"link in","z":"86e742aa.4f6e","name":"","links":["e64cc813.63c35"],"x":220,"y":200,"wires":[["1a2ee7e9.2fa3e"]]},{"id":"d3254fdf.63399","type":"change","z":"86e742aa.4f6e","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"ON","fromt":"str","to":"true","tot":"bool"},{"t":"change","p":"payload","pt":"msg","from":"OFF","fromt":"str","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":100,"wires":[["233a75a5.4367d2","1a2ee7e9.2fa3e"]]},{"id":"787a048.474047c","type":"mqtt-broker","z":"","name":"mqttserver","broker":"10.0.0.17","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"9e7276e6.5d1f4","type":"ui_group","z":"","name":"Wohnzimmer","tab":"5e73d01b.c77cd","disp":true,"width":6,"collapse":false},{"id":"5e73d01b.c77cd","type":"ui_tab","z":"","name":"Licht","icon":"dashboard","disabled":false,"hidden":false}]

You can simplify your flow if you change the setting in the ui-switch:

[{"id":"375931c3.7f0abe","type":"mqtt in","z":"7c6a9ec3.f1512","name":"couchlicht","topic":"stat/couchlicht/POWER","qos":"0","datatype":"auto","broker":"462b3048.b7db9","x":340,"y":1640,"wires":[["9f153f65.aa6d7","a7067cfc.45d86"]]},{"id":"1ae8d926.67e2d7","type":"mqtt out","z":"7c6a9ec3.f1512","name":"Couchlicht","topic":"cmnd/couchlicht/POWER","qos":"","retain":"","broker":"462b3048.b7db9","x":590,"y":1740,"wires":[]},{"id":"9f153f65.aa6d7","type":"debug","z":"7c6a9ec3.f1512","name":"mqttCouchlichState","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":610,"y":1640,"wires":[]},{"id":"45e54180.ebe4f","type":"debug","z":"7c6a9ec3.f1512","name":"CouchlichtSwitch","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":610,"y":1800,"wires":[]},{"id":"a7067cfc.45d86","type":"ui_switch","z":"7c6a9ec3.f1512","name":"","label":"Couchlicht","tooltip":"","group":"6972a237.54e1bc","order":1,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"","style":"","onvalue":"ON","onvalueType":"str","onicon":"","oncolor":"","offvalue":"OFF","offvalueType":"str","officon":"","offcolor":"","x":350,"y":1740,"wires":[["1ae8d926.67e2d7","45e54180.ebe4f","e7219679.fa6af8"]]},{"id":"7cd7413c.eb42f","type":"debug","z":"7c6a9ec3.f1512","name":"mqttCouchlichState","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":610,"y":1700,"wires":[]},{"id":"e7219679.fa6af8","type":"link out","z":"7c6a9ec3.f1512","name":"","links":["f56dc2dd.4142f"],"x":420,"y":1800,"wires":[]},{"id":"f56dc2dd.4142f","type":"link in","z":"7c6a9ec3.f1512","name":"","links":["e7219679.fa6af8"],"x":300,"y":1800,"wires":[["a7067cfc.45d86"]]},{"id":"2fb810fb.7b25e","type":"inject","z":"7c6a9ec3.f1512","name":"","topic":"","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":1720,"wires":[["a7067cfc.45d86"]]},{"id":"5f112009.2db7c","type":"inject","z":"7c6a9ec3.f1512","name":"","topic":"","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":1760,"wires":[["a7067cfc.45d86"]]},{"id":"462b3048.b7db9","type":"mqtt-broker","z":"","name":"mqttserver","broker":"10.0.0.17","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"6972a237.54e1bc","type":"ui_group","z":"","name":"Wohnzimmer","tab":"bf70c920.9328f8","disp":true,"width":6,"collapse":false},{"id":"bf70c920.9328f8","type":"ui_tab","z":"","name":"Licht","icon":"dashboard","disabled":false,"hidden":false}]

Hi @cflurin and @HeyJonny,

I'm trying to use this same function but not having any luck with getting my MQTT output node to trigger when using my MQTT in or Inject nodes. Only the Dashboard Switch is working. Do the Link In and Link Out nodes still function as shown in the examples above or has something changed?

Any help would be appreciated

[{"id":"5c02cb03.784ce4","type":"debug","z":"d3418782.7a96e8","name":"payload out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":669,"y":375,"wires":[]},{"id":"f014ae2b.f7bf6","type":"mqtt out","z":"d3418782.7a96e8","name":"Wall Lights","topic":"backyard/wall_lights/stat","qos":"","retain":"","broker":"fe86ba0d.da2868","x":669,"y":275,"wires":[]},{"id":"a933323f.87d1b","type":"mqtt in","z":"d3418782.7a96e8","name":"Wall Lights","topic":"backyard/wall_lights/cmnd","qos":"0","datatype":"auto","broker":"fe86ba0d.da2868","x":199,"y":275,"wires":[["9b7bfe81.38803","fb17c9e6.b5b148"]]},{"id":"13dc3b53.e7d615","type":"rpi-gpio out","z":"d3418782.7a96e8","name":"Wall Lights (31)","pin":"31","set":"","level":"0","freq":"","out":"out","x":939,"y":175,"wires":[]},{"id":"9b7bfe81.38803","type":"debug","z":"d3418782.7a96e8","name":"payload in","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":429,"y":375,"wires":[]},{"id":"fb17c9e6.b5b148","type":"ui_switch","z":"d3418782.7a96e8","name":"","label":"Wall Lights","tooltip":"","group":"cd54934d.ff379","order":2,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":430,"y":275,"wires":[["f014ae2b.f7bf6","5c02cb03.784ce4","b95d337d.02e3d","d533ddfc.eba7f"]]},{"id":"b95d337d.02e3d","type":"change","z":"d3418782.7a96e8","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"false","fromt":"bool","to":"0","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"true","fromt":"bool","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":679,"y":175,"wires":[["13dc3b53.e7d615","176bbbff.0e1a74"]]},{"id":"bac3ec4b.86fe5","type":"inject","z":"d3418782.7a96e8","name":"ON","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":209,"y":75,"wires":[["fb17c9e6.b5b148"]]},{"id":"2e861fd4.9e5b2","type":"inject","z":"d3418782.7a96e8","name":"OFF","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":209,"y":175,"wires":[["fb17c9e6.b5b148"]]},{"id":"176bbbff.0e1a74","type":"debug","z":"d3418782.7a96e8","name":"payload change","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":939,"y":275,"wires":[]},{"id":"7e91aa4d.70c154","type":"link in","z":"d3418782.7a96e8","name":"","links":["d533ddfc.eba7f"],"x":375,"y":175,"wires":[["fb17c9e6.b5b148"]]},{"id":"d533ddfc.eba7f","type":"link out","z":"d3418782.7a96e8","name":"","links":["7e91aa4d.70c154"],"x":484,"y":175,"wires":[]},{"id":"fe86ba0d.da2868","type":"mqtt-broker","z":"","name":"AutomationMQTT","broker":"192.168.1.5","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"cd54934d.ff379","type":"ui_group","z":"","name":"Lights","tab":"c3f2c080.1789d","disp":true,"width":"6","collapse":false},{"id":"c3f2c080.1789d","type":"ui_tab","z":"","name":"Backyard","icon":"dashboard","disabled":false,"hidden":false}]

So I think I'm misunderstanding the switch node and that the flows in the examples above are really only concerned with the switch status and not actually triggering any other out nodes.

Assuming I'll need to separate flow or change to my current flow to actually switch my rpi GPIO out for when the inject or MQTT in are triggered.

Welcome to the forum @LeelooAuto, I started looking at this but I am not clear what the problem you are having is, and haven't got time to start wading through an old thread to see what it is all about, so can I suggest that you start a new thread, explain exactly what you are trying to do and what you have tried and we can go from there.
Thanks.

Hello,
My turn to have same problemon similar config (RaspPi via MQTT Mosquitto)

I have simple flow with a switch and button.
But when i switch off the light with physical switch, it does not update the state of nodered switch in nodered dashboard. (am allset in boolean)

Any idea welcome..