Every time I think I have it nutted out, MQTT
throws another curve ball at me and just doesn't do things consistently.
(Slight back story)
Multiple machines controlling single device.
(Sorry, it is actually becoming quite long)
For the sake of simplifying this is a breakdown of what happens on each machine.
This is only a functional example. Not the real code.
As the button
is a pushbutton, the icon needs to be altered if another machine changes the state of the external device.
That way the button shows the correct indication.
Of course I didn't foresee the problem: If I turn the device on (or off) on one device, and try to turn it off (or on) on another there is a problem. The toggle
node isn't reflecting the same real condition of the device. Therefore a two time button press is needed.
That isn't too much of a problem. I modify the toggle
node and connect from the decode message
node to the toggle
node also.
That way both the button node and the toggle
node are on the same page for the device's status.
That if fine. One computer is on 24/7. The other isn't. It is this machine and it gets turned on and off many times a day.
Now, the past few days I have had to roll out that modification to all machines concerned and it seems to be working.
I had to mess around with the MQTT
settings about messages being kept (retained) or not.
(End back story - I hope)
To add another layer of truth I also included code so if the device is physically turned off, it disables the buttons on all machines.
This is done with the LWT in MQTT.
After a lot of oops moments rolling out the above, I am still not seeing the correct things on the machines.
This is the screen of the machine which is on 24/7:
Which is wrong.
This is the from this machine which has only just been turned on this morning:
Which is more accurate - but still slightly wrong.
When the bulb is physically powered down, a LWT
message is sent and it tells MQTT that it is dead.
That message is used to put the ban
icon on the button
and disable it.
Last night I physically turned off the bulb. I did that because this machine was turned off and the other machine which is on 24/7 is headless.
During testing I was seeing the LWT
message/s coming through and the buttons
changing state obligingly.
Until last night and then the machine which is always on didn't see the message.
(Why? -- semi rhetorical)
A while ago I had to make a special MQTT in
node and set the topic to the entire path for the LWT
of the device.
This didn't really help.
This is an extract of the actual code that detects the LWT.
[{"id":"9478ca91.86f89","type":"mqtt in","z":"26262ba1.62dcbc","name":"Bulb-2 *","topic":"BULB-2/tele/LWT","qos":"2","datatype":"auto","broker":"378c0403.8cda04","x":2480,"y":1540,"wires":[["33bdf784.1f12e8","eb79a878.978f78","17be4a17.f26556","18542f8a.4235b8"]],"info":"This needs editing for different BULBS"},{"id":"17be4a17.f26556","type":"delay","z":"26262ba1.62dcbc","name":"Delay","pauseType":"delay","timeout":"200","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2480,"y":1580,"wires":[["47b5cbbd.2089dc","372c717c.ac1446"]]},{"id":"47b5cbbd.2089dc","type":"switch","z":"26262ba1.62dcbc","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":2650,"y":1580,"wires":[["c04aa13e.9d3ca","b53edd19.caae"],["1cda007c.85c458"]]},{"id":"1cda007c.85c458","type":"function","z":"26262ba1.62dcbc","name":"BAN","func":"msg = {icon: '<font color = \"red\"><i class=\"fa fa-ban fa-3x\"></i></font>'};\nreturn msg;","outputs":1,"noerr":0,"x":2650,"y":1620,"wires":[["ec8dc91b.795888"]]},{"id":"c04aa13e.9d3ca","type":"change","z":"26262ba1.62dcbc","name":"Enable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":2860,"y":1580,"wires":[["9ccce8b4.a581d"]]},{"id":"ec8dc91b.795888","type":"change","z":"26262ba1.62dcbc","name":"Disable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":2860,"y":1620,"wires":[["9ccce8b4.a581d","de51f5b3.e7b2"]]},{"id":"9ccce8b4.a581d","type":"ui_button","z":"26262ba1.62dcbc","name":"BULB#2","group":"c9a39d1d.9fa798","order":2,"width":"1","height":"3","passthru":false,"label":"{{msg.icon}}","tooltip":"","color":"","bgcolor":"{{msg.background}}","icon":"","payload":"X","payloadType":"str","topic":"","x":3070,"y":1580,"wires":[["8318d073.9248c"]]},{"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","birthRetain":"true","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"c9a39d1d.9fa798","type":"ui_group","z":"","name":"BULB-2","tab":"aa487daa.33c1c","order":5,"disp":true,"width":"3","collapse":false},{"id":"aa487daa.33c1c","type":"ui_tab","z":"","name":"Real_World_Control","icon":"dashboard","order":3,"disabled":false,"hidden":false}]
Both machines have this code. I posted this as there is a bit of explanation in one of the comment
nodes. See the one with a *
in its name.
So given both machines have the same code:
Why is it that this machine which was turned on this morning showing the correct state of the bulb when the one which is on 24/7 isn't?
The LWT message must have been sent, as it was being sent earlier last night when I was testing the flow.
Here too is a show from MQTT explorer
I'm going off line for a few hours.
It isn't I am doing a runner - asking a question and not replying.
But I am just missing something why the machine which is on 24/7 is showing the wrong information/state of the button
.