MQTT is driving me crazy with inconsistent doings

The real way to track this now, is to use a network sniffer and watch the packet traffic, so prove that the communication is going when and where you want. But this is a bit of overkill maybe as well.

Many may disagree, but I always use the publisher name in the MQTT topic, so I know the source. If I use a '#' or '+' options in a given topic, I explicitly parse/filter the MQTT messages any subscriber receives. Otherwise I always have a 1 to 1 relationship between publishers and subscribers.

I run two NR instances as core receivers or subscribers, but only one is ever listening, a primary and secondary model. I also run NR on every remote device, but remote devices are always publishers only, except for a few very rare scenarios... like asking a remote NR instance to restart, or the device to reboot for example..

For the most part the above has let me avoid a lot of issues.

That is pretty much the opposite of what @edje11 suggested.

He said...

So in other words - only update your dashboard from stat topics NOT from the cmnd topic that you generate.

This is very similar to PLC / HMI programming. You (should) send a "command" to the PLC and only update the screen with the "status" feedback from the PLC. This way, you get positive feedback.

(for illustration: HMI == dashoard and PLC==tasmota)


I am happy to concede I am reading your reply wrong but it certainly sounds like you are generating dashboard changes from the commands and not the response. Which would explain some things.

3 Likes

I have understood that this indication is wanted which in a way makes sense, especially if the commands and feedback has "a long way to travel" sort of

I have coded some web pages using the ui_template node and achieved this with some css styling (inset/outset). But with the standard buttons in the dashboard I don't think they can do "self-detect clicks and toggle outlook, i.e. change colour or background", you need to send them a message, setting the desired property

If I am reading that correctly, that is why I have the function node below it.

It sends the intermediate status icon/colours as well as the command to either turn it on or off.

The final state is from the incoming message from the device.

Yeah, ok. Not the *full code but post 14 is an example taken directly from the actual flow.

Here is the real code. It is messy.

[{"id":"c3a6064e.05e3f8","type":"function","z":"6dd5ca4d.d1958c","name":"Button control BULB2","func":"var msg1 = {};\nvar counter = context.get(\"counter\") || 0;\ncounter = (counter + 1) % 2;\ncontext.set(\"counter\",counter);\n\nvar x = msg.payload;\n\nif (x == \"ON\")\n{\n    counter = 1;\n    context.set(\"counter\",1);\n    return;\n} else\nif (x == \"OFF\")\n{\n    counter = 0;\n    context.set(\"counter\",0);\n    return;\n}\n\nif (x == \"X\")\n{\n    //\n    if (counter == 0)\n    {\n        msg = {\n            payload: \"OFF\",\n        };\n    } else\n    {\n        msg = {\n            payload: \"ON\",\n        };\n    }\n    msg.icon = '<font color = \"orangered\"><i class=\"fa fa-lightbulb-o fa-3x\"></i></font>';\n    msg.background = \"black\";\n}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":3100,"y":1450,"wires":[["1f7ab784.0af25","8ecacbe9.2c6dc","ff9e3205.7d3ef8"]]},{"id":"ff9e3205.7d3ef8","type":"ui_button","z":"6dd5ca4d.d1958c","name":"BULB#2","group":"12c72eb4.18b9d9","order":2,"width":"1","height":"3","passthru":false,"label":"{{msg.icon}}","tooltip":"","color":"","bgcolor":"{{msg.background}}","icon":"","payload":"X","payloadType":"str","topic":"","x":3060,"y":1410,"wires":[["c3a6064e.05e3f8"]]},{"id":"1f7ab784.0af25","type":"delay","z":"6dd5ca4d.d1958c","name":"Delay","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":3310,"y":1450,"wires":[["6298a2df.c8b0b4"]]},{"id":"8ecacbe9.2c6dc","type":"link out","z":"6dd5ca4d.d1958c","name":"","links":["27fa2e34.3b9c0a","fddab0da.b82168"],"x":3185,"y":1490,"wires":[]},{"id":"586c4129.55c178","type":"change","z":"6dd5ca4d.d1958c","name":"X *","rules":[{"t":"set","p":"payload","pt":"msg","to":"X","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2840,"y":1570,"wires":[["c3a6064e.05e3f8"]],"info":"The payload is `Y` (or !`X`) to initialise the\nbutton."},{"id":"d2275a52.fc9ec","type":"inject","z":"6dd5ca4d.d1958c","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"X","payloadType":"str","x":2860,"y":1610,"wires":[["c3a6064e.05e3f8"]]},{"id":"76fcfa8.682ab84","type":"change","z":"6dd5ca4d.d1958c","name":"Setup *","rules":[{"t":"set","p":"topic","pt":"msg","to":"SETUP","tot":"str"},{"t":"set","p":"id","pt":"msg","to":"BULB-2","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"X","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":3060,"y":1370,"wires":[["c3a6064e.05e3f8"]],"info":"This needs editing for different BULBS"},{"id":"8bb0a132.6b5038","type":"switch","z":"6dd5ca4d.d1958c","name":"TOPICS *","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"BULB-2/cmnd/power1","vt":"str"},{"t":"eq","v":"BULB-2/cmnd/dimmer","vt":"str"},{"t":"eq","v":"BULB-2/cmnd/ct","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":4,"x":2650,"y":1320,"wires":[["3c7f955a.c14d32","c3a6064e.05e3f8","74fe3ef2.5812c8"],["ed81e075.75366"],["e9fb36f0.632778"],[]],"outputLabels":["State","",null,"LWT"],"info":"This needs editing for different BULBS"},{"id":"79e6bcfe.36e5ec","type":"change","z":"6dd5ca4d.d1958c","name":"Enable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":2850,"y":1410,"wires":[["ff9e3205.7d3ef8"]]},{"id":"3c7f955a.c14d32","type":"function","z":"6dd5ca4d.d1958c","name":"Set state","func":"if (msg.payload == \"RESET\")\n{\n    let state = context.get(\"STATE\");\n    if (state === 0)\n    {\n        //\n        msg= {icon: '<font color = \"red\"><i class=\"fa fa-lightbulb-o fa-3x\"></i></font>'};\n        msg.background = \"black\";\n        return msg;\n    } else\n    {\n        //\n        msg= {icon: '<font color = \"lime\"><i class=\"fa fa-lightbulb-o fa-3x\"></i></font>'};\n        msg.background = \"black\";\n        return msg;\n    }\n}\nif (msg.payload == \"ON\")\n{\n        msg= {icon: '<font color = \"lime\"><i class=\"fa fa-lightbulb-o fa-3x\"></i></font>'};\n        msg.background = \"black\";\n        context.set(\"STATE\",1);\n} else\n{\n        msg= {icon: '<font color = \"red\"><i class=\"fa fa-lightbulb-o fa-3x\"></i></font>'};\n        msg.background = \"black\";\n        context.set(\"STATE\",0);\n}\nreturn msg;\n","outputs":1,"noerr":0,"x":2850,"y":1370,"wires":[["5e5e2ade.7a0cb4","ff9e3205.7d3ef8"]]},{"id":"cf422c2c.6d35","type":"change","z":"6dd5ca4d.d1958c","name":"Disable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":2850,"y":1450,"wires":[["ff9e3205.7d3ef8"]]},{"id":"6298a2df.c8b0b4","type":"change","z":"6dd5ca4d.d1958c","name":"#2 *","rules":[{"t":"set","p":"topic","pt":"msg","to":"BULB-2/cmnd/power1","tot":"str"},{"t":"set","p":"who","pt":"msg","to":"BedPi","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":3310,"y":1490,"wires":[["261d3ca4.0f2b24","f40638e7.a90598"]],"info":"This needs editing for different BULBS"},{"id":"a8b8b766.983a1","type":"inject","z":"6dd5ca4d.d1958c","name":"Setup *","repeat":"","crontab":"","once":true,"onceDelay":"0.8","topic":"","payload":"X","payloadType":"str","x":2870,"y":1320,"wires":[["76fcfa8.682ab84"]],"info":"Probably not needed."},{"id":"c9eb1440.2a32b","type":"catch","z":"6dd5ca4d.d1958c","name":"Not set","scope":["c3a6064e.05e3f8"],"uncaught":false,"x":3050,"y":1490,"wires":[["76fcfa8.682ab84","5ae48904.55b0a8"]]},{"id":"56cc697.fbf8718","type":"mqtt in","z":"6dd5ca4d.d1958c","name":"Bulb-2 *","topic":"BULB-2/#","qos":"2","datatype":"auto","broker":"bbf26a6c.b7922","x":2470,"y":1320,"wires":[["d12a38bf.096b58","d52483f1.8a59d8","d3a62ce0.cf6778","8bb0a132.6b5038","7cd6e58b.0faa74"]],"info":"This needs editing for different BULBS"},{"id":"74fe3ef2.5812c8","type":"debug","z":"6dd5ca4d.d1958c","name":"Bulb 2 power status","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":3090,"y":1280,"wires":[]},{"id":"ed81e075.75366","type":"link out","z":"6dd5ca4d.d1958c","name":"","links":["ba823fac.35c1e","92a61fac.68ce1"],"x":2755,"y":1290,"wires":[]},{"id":"e9fb36f0.632778","type":"link out","z":"6dd5ca4d.d1958c","name":"","links":["3460262d.63677a"],"x":2755,"y":1320,"wires":[]},{"id":"ab02ce72.ca7f78","type":"inject","z":"6dd5ca4d.d1958c","name":"En","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2470,"y":1470,"wires":[["79e6bcfe.36e5ec","4e734307.a340cc"]]},{"id":"d08a95cf.e267","type":"switch","z":"6dd5ca4d.d1958c","name":"LWT","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Online","vt":"str"},{"t":"eq","v":"Offline","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":2640,"y":1410,"wires":[["79e6bcfe.36e5ec","4e734307.a340cc"],["4051e36d.ee7e5c"]]},{"id":"5e5e2ade.7a0cb4","type":"debug","z":"6dd5ca4d.d1958c","name":"From MQTT","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":3070,"y":1320,"wires":[]},{"id":"4e734307.a340cc","type":"change","z":"6dd5ca4d.d1958c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"RESET","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2670,"y":1370,"wires":[["3c7f955a.c14d32"]]},{"id":"4051e36d.ee7e5c","type":"function","z":"6dd5ca4d.d1958c","name":"BAN","func":"msg = {icon: '<font color = \"red\"><i class=\"fa fa-ban fa-3x\"></i></font>'};\nreturn msg;","outputs":1,"noerr":0,"x":2640,"y":1450,"wires":[["cf422c2c.6d35"]]},{"id":"261d3ca4.0f2b24","type":"debug","z":"6dd5ca4d.d1958c","name":"BULB #2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":3480,"y":1450,"wires":[]},{"id":"f40638e7.a90598","type":"mqtt out","z":"6dd5ca4d.d1958c","name":"","topic":"","qos":"","retain":"","broker":"bbf26a6c.b7922","x":3470,"y":1490,"wires":[]},{"id":"5ae48904.55b0a8","type":"trigger","z":"6dd5ca4d.d1958c","name":"Indicator","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"8","extend":false,"units":"s","reset":"","bytopic":"all","outputs":1,"x":3060,"y":1530,"wires":[[]]},{"id":"d12a38bf.096b58","type":"debug","z":"6dd5ca4d.d1958c","name":"RAW BULB2 message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2520,"y":1280,"wires":[]},{"id":"d52483f1.8a59d8","type":"link out","z":"6dd5ca4d.d1958c","name":"","links":["347dd852.c38258","a8f558c1.17bcc"],"x":2605,"y":1590,"wires":[]},{"id":"d3a62ce0.cf6778","type":"switch","z":"6dd5ca4d.d1958c","name":"power","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"BULB-2/stat/POWER","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":2640,"y":1510,"wires":[["59c25162.732be"]]},{"id":"7cd6e58b.0faa74","type":"link out","z":"6dd5ca4d.d1958c","name":"","links":["5aa1f4c2.f97dc4"],"x":2605,"y":1250,"wires":[]},{"id":"7c74a5ec.6093c4","type":"delay","z":"6dd5ca4d.d1958c","name":"Delay","pauseType":"delay","timeout":"200","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2470,"y":1410,"wires":[["d08a95cf.e267","de9617f6.3aa4b8"]]},{"id":"d974d852.ba3458","type":"inject","z":"6dd5ca4d.d1958c","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Offline","payloadType":"str","x":2470,"y":1550,"wires":[["d08a95cf.e267"]]},{"id":"29b1c67.de7ec3a","type":"inject","z":"6dd5ca4d.d1958c","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Online","payloadType":"str","x":2470,"y":1590,"wires":[["d08a95cf.e267"]]},{"id":"7c03ce7c.dcfaa","type":"inject","z":"6dd5ca4d.d1958c","name":"Dis","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2470,"y":1510,"wires":[["4051e36d.ee7e5c"]]},{"id":"5f63f961.cdcbb","type":"link in","z":"6dd5ca4d.d1958c","name":"","links":["963dafc1.211fc8","1ec469a7.2569f6"],"x":3275,"y":1530,"wires":[["f40638e7.a90598"]]},{"id":"59c25162.732be","type":"change","z":"6dd5ca4d.d1958c","name":"Set FLOW","rules":[{"t":"set","p":"BULB2_STATE","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2860,"y":1510,"wires":[[]]},{"id":"de9617f6.3aa4b8","type":"change","z":"6dd5ca4d.d1958c","name":"Set FLOW","rules":[{"t":"set","p":"BULB2_LWT","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2660,"y":1550,"wires":[[]]},{"id":"544dd20.196943","type":"mqtt in","z":"6dd5ca4d.d1958c","name":"Bulb-2 *","topic":"BULB-2/tele/LWT","qos":"2","datatype":"auto","broker":"bbf26a6c.b7922","x":2470,"y":1370,"wires":[["34f1f3d2.646b7c","7c74a5ec.6093c4","58ba8156.89b418"]],"info":"This needs editing for different BULBS"},{"id":"34f1f3d2.646b7c","type":"debug","z":"6dd5ca4d.d1958c","name":"Bulb-2 LWT","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2860,"y":1280,"wires":[]},{"id":"58ba8156.89b418","type":"link out","z":"6dd5ca4d.d1958c","name":"","links":["9591f5de.83b88","f6016669.56664"],"x":2605,"y":1630,"wires":[]},{"id":"12c72eb4.18b9d9","type":"ui_group","z":"","name":"BULB-2","tab":"e92ac76d.c24e7","order":6,"disp":true,"width":"3","collapse":false},{"id":"bbf26a6c.b7922","type":"mqtt-broker","z":"","name":"MQTT host","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"SOM","birthQos":"0","birthRetain":"true","birthPayload":"BedPi UP","closeTopic":"EOM","closeQos":"0","closeRetain":"true","closePayload":"BedPi Shutting down","willTopic":"EOM","willQos":"0","willRetain":"false","willPayload":"BedPi COMMS FAIL"},{"id":"e92ac76d.c24e7","type":"ui_tab","z":"","name":"Real_World_Control","icon":"dashboard","order":13,"disabled":false,"hidden":false}]

There are parts missing for controlling the brightness, colour temperature and logging.
Those bits are a bit unneeded in finding the problem, I feel.

@Steve-Mcl,
Yeah, I did notice that.

It is being looking into. (Excuse the bad gramma)

Ok, now looking at using BULB-2/stat/RESULT rather than cmnd.....

That opens a whole other can of worms.

Rather than getting something I can easily filter, the message arrives in a JSON packet - I think.

So I get this:

{"POWER":"ON","Dimmer":70,"Color":"7241","White":70,"CT":280,"Channel":[45,25]}

I stick that through a JSON node so it is more usable I get:

{"POWER":"ON","Dimmer":70,"Color":"7241","White":70,"CT":280,"Channel":[45,25]}
(Ok, that didn't achieve anything in the visual side of things)

But now I can parse msg.payload as an ...... object?

But that throws me to confusion.

How do I switch that? (Or would it be route?)

I want to get the POWER, Dimmer, Colo(u)r Strike that, and CT.

Stuck.

Edit

Found a way with a function node and splitting the message into 3 parts and having 3 outputs.

You can select that the json should be parsed directly in the mqtt out node.

Yes, but I believe else where in this thread I was told to use the node to get ALL the messages.

And it saves me needing/using 3 MQTT nodes to get the three messages.
And I also want the LWT so really that is four.

Well for me a JSON node parses the string you posted above into a workable object no problem.
Otherwise use JSON.parse() in a function.

This is what I bashed together:

[{"id":"4ee597ca.33cee8","type":"debug","z":"6dd5ca4d.d1958c","name":"Dimmer","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2650,"y":2570,"wires":[]},{"id":"8b083c4c.80b528","type":"function","z":"6dd5ca4d.d1958c","name":"","func":"msg1 = {};\nmsg2 = {};\nmsg3 = {};\n\nvar a = msg.payload.POWER;\nvar b = msg.payload.Dimmer;\nvar c = msg.payload.CT;\nmsg1.payload = a;\nmsg2.payload = b;\nmsg3.payload = c;\nreturn [msg1,msg2,msg3];","outputs":3,"noerr":0,"initialize":"","finalize":"","x":2470,"y":2570,"wires":[["29f4c139.4d4d56"],["4ee597ca.33cee8"],["a04167b3.7cb828"]]},{"id":"a5eb3cb8.3abc","type":"json","z":"6dd5ca4d.d1958c","name":"","property":"payload","action":"","pretty":false,"x":2641,"y":2469,"wires":[["384d680a.4761f","28b7979a.392d8","8b083c4c.80b528"]]},{"id":"29f4c139.4d4d56","type":"debug","z":"6dd5ca4d.d1958c","name":"Power","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2640,"y":2540,"wires":[]},{"id":"a04167b3.7cb828","type":"debug","z":"6dd5ca4d.d1958c","name":"CT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2640,"y":2600,"wires":[]},{"id":"c7bcf5f2.a5a5b8","type":"switch","z":"6dd5ca4d.d1958c","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"BULB-2/stat/RESULT","vt":"str"},{"t":"eq","v":"BULB-2/tele/LWT","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":2501,"y":2469,"wires":[["2de12959.7929be","a5eb3cb8.3abc"],["4a36db0c.09896c"]]},{"id":"384d680a.4761f","type":"debug","z":"6dd5ca4d.d1958c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2781,"y":2429,"wires":[]},{"id":"28b7979a.392d8","type":"switch","z":"6dd5ca4d.d1958c","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":2790,"y":2470,"wires":[[]]},{"id":"3e3c45cd.1818a2","type":"mqtt in","z":"6dd5ca4d.d1958c","name":"Bulb-2 *","topic":"BULB-2/#","qos":"2","datatype":"auto","broker":"bbf26a6c.b7922","x":2371,"y":2469,"wires":[["c7bcf5f2.a5a5b8"]],"info":"This needs editing for different BULBS"},{"id":"2de12959.7929be","type":"debug","z":"6dd5ca4d.d1958c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2641,"y":2429,"wires":[]},{"id":"4a36db0c.09896c","type":"debug","z":"6dd5ca4d.d1958c","name":"LWT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2640,"y":2640,"wires":[]},{"id":"bbf26a6c.b7922","type":"mqtt-broker","z":"","name":"MQTT host","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"SOM","birthQos":"0","birthRetain":"true","birthPayload":"BedPi UP","closeTopic":"EOM","closeQos":"0","closeRetain":"true","closePayload":"BedPi Shutting down","willTopic":"EOM","willQos":"0","willRetain":"false","willPayload":"BedPi COMMS FAIL"}]

I've shown you mine. Please show me yours. :wink:

Edited to also include LWT.

1 Like

Looks good what you did. What is not working?

Oh, I am not that far yet.

That is just something I bashed together to establish a baseline for getting data from MQTT.

Testing is still a few steps ahead. :wink:

But thanks. It passes your scrutiny so that means I am not just flying blind with my expectations of what it does.

Maybe you have solved this already but I thought I should just give you an example how I would configure the things for a typical toggle button with features as outlined earlier above, coming back to your original image in the first posting above

It works like this:
I have a Shelly device controlling a lamp on/off by sending a msg using MQTT and I get the response back also via MQTT, so very similar to what you have

You click on the button, the background color & text is changed and the related command, based on the current state, is sent out to the device

When the response with the new state is coming back, we have to store this somewhere so it can be used in the function node to toggle correctly next time (we do not want the msg to pass thru the button node since this would cause a loop). In this simple example I just put it right into flow.state_deviceID, you need to do it per lamp. Maybe there are clever ways doing this generic to make it more efficient if you have many lamps. But anyway, we now have a variable per lamp keeping their current state

The incoming response is in addition also used to decide values of the other properties like the button background color and text

The delay node is just needed to make the whole demo more visible, the response is normally so fast that it happens "immediately"

[{"id":"1a23fdf6.a62292","type":"mqtt out","z":"6043200d.39617","name":"","topic":"shellies/shellydimmer-F3C070/light/0/set","qos":"","retain":"","broker":"2a019090.5ba4d","x":1230,"y":150,"wires":[]},{"id":"7472630e.af727c","type":"mqtt in","z":"6043200d.39617","name":"","topic":"shellies/shellydimmer-F3C070/light/0","qos":"2","datatype":"auto","broker":"2a019090.5ba4d","x":160,"y":150,"wires":[["a1dbf32f.28814"]]},{"id":"6034b3bc.1b663c","type":"ui_button","z":"6043200d.39617","name":"","group":"f453132d.ddeb9","order":6,"width":0,"height":0,"passthru":false,"label":"{{msg.txt}}","tooltip":"","color":"","bgcolor":"{{msg.background}}","icon":"","payload":"true","payloadType":"bool","topic":"","x":820,"y":150,"wires":[["72f83526.4a966c","fc893119.b4f73"]]},{"id":"27c7c158.7778fe","type":"change","z":"6043200d.39617","name":"","rules":[{"t":"set","p":"background","pt":"msg","to":"green","tot":"str"},{"t":"set","p":"txt","pt":"msg","to":"ON","tot":"str"},{"t":"set","p":"state_F3C070","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":80,"wires":[["6034b3bc.1b663c"]]},{"id":"c7e14300.5c42f","type":"change","z":"6043200d.39617","name":"","rules":[{"t":"set","p":"background","pt":"msg","to":"black","tot":"str"},{"t":"set","p":"txt","pt":"msg","to":"OFF","tot":"str"},{"t":"set","p":"state_F3C070","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":220,"wires":[["6034b3bc.1b663c"]]},{"id":"72f83526.4a966c","type":"function","z":"6043200d.39617","name":"","func":"var state = flow.get('state_F3C070')||'off';\n//node.warn(state);\n\nswitch (state) {\n    case 'off':\n        msg.payload = {\"turn\": \"on\", \"brightness\": 28};\n        break;\n    case 'on':\n        msg.payload = {\"turn\": \"off\"};\n        break;\n    default:\n        msg.payload = {\"turn\": \"off\"};\n        break;\n}\n\nnode.send(msg);","outputs":"1","noerr":0,"initialize":"","finalize":"","x":970,"y":150,"wires":[["1a23fdf6.a62292"]]},{"id":"1ebcac6a.089f04","type":"switch","z":"6043200d.39617","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":540,"y":150,"wires":[["27c7c158.7778fe"],["c7e14300.5c42f"]]},{"id":"fc893119.b4f73","type":"change","z":"6043200d.39617","name":"","rules":[{"t":"set","p":"background","pt":"msg","to":"orange","tot":"str"},{"t":"set","p":"txt","pt":"msg","to":"Command in progress","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":80,"wires":[["6034b3bc.1b663c"]]},{"id":"a1dbf32f.28814","type":"delay","z":"6043200d.39617","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":400,"y":150,"wires":[["1ebcac6a.089f04"]]},{"id":"2a019090.5ba4d","type":"mqtt-broker","z":"","name":"","broker":"192.168.0.240","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"f453132d.ddeb9","type":"ui_group","z":"","name":"Shelly","tab":"d1b6ac50.403fe","order":1,"disp":false,"width":"15","collapse":false},{"id":"d1b6ac50.403fe","type":"ui_tab","z":"","name":"Shelly","icon":"dashboard","order":14,"disabled":false,"hidden":false}]
1 Like

Thanks. I will look at it. Alas a couple of other things have happened and I am just now a bit overwhelmed with jobs.

But I do appreciate the code you posted. It will be looked at.

Just looked at it quickly. Great job! You got around a bit thing which was stumping me by saving the state as a global variable. (I'd use flow, but....)

Looks promising.

Thank you!
I believed I used flow variable, maybe I made a mistake but using flow was my intention. Anyway, using global might make sense if you for any reason would need to know/use the states as some kind of conditions for other stuff. Here I'm not fully happy, maybe a table in a database is better, maybe using context in this case is fully sufficient, I mean the states are getting updated regularly anyway during runtime so no need for persistence

Oh, sorry. It was flow, not global.

Just to admit I goofed.

Just a video showing the operation. Screen updates "as it happens" on all clients

I'm using MQTT to control my home lighting system. Here's a screenshot of the dashboard.

And here's part of the NR flow for just one of the lights.

The 'front_door' function node sends a payload of 0 or 1 to topic... cmd/node51/power

The 'update_light_state' node receives feedback from the SonOff S20 (flashed with Tasmota) and detects if the feedback is "OFF" or "ON" and operates the bulb status accordingly. The MQTT-In node has a topic setting of... stat/node51/RESULT

ScreenShot177

Here's the JavaScript in the 'update_light_state' function.

if (msg.payload.POWER == "OFF")
{
    msg.payload = '<i class="fa fa-lightbulb-o fa-2x nr-dashboard-dim"></i>';
    return msg;
}
else if (msg.payload.POWER == "ON")
{
    msg.payload = '<i class="fa fa-lightbulb-o fa-2x nr-dashboard-error"></i>';
    return msg;
}
2 Likes

Yes Dave, that's also a really good solution. Only thing missing, well if you are keen in having this, is the three-state-view shown by changing the button background accordingly like 'on'->'command in progress'->'off' and vice versa