Mqtt monitoring / syncing states, need bit of help

Should it be fully symetrical? So if either one goes off or on then the other should follow it? If so then you might like to try this flow which is rather simpler and does not use the context so maybe is less likely to suffer from subtle edge case timing issues.


The inject buttons allow it to be tested without mqtt.

[{"id":"24ee01a6.0d516e","type":"mqtt in","z":"6f30c519.8daccc","name":"Hall Stairs","topic":"home/hallfloor/light/stat/POWER2","qos":"0","datatype":"utf8","broker":"62798fd.c796c7","x":90,"y":397,"wires":[["cd7e2522.8ee08"]]},{"id":"a04f3d03.87969","type":"mqtt in","z":"6f30c519.8daccc","name":"Stairs (slave)","topic":"home/staircase/light/stat/POWER","qos":"0","datatype":"utf8","broker":"62798fd.c796c7","x":99,"y":446,"wires":[["cd7e2522.8ee08"]]},{"id":"cd7e2522.8ee08","type":"join","z":"6f30c519.8daccc","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":301,"y":427,"wires":[["16071cb.120d5e3"]]},{"id":"16071cb.120d5e3","type":"function","z":"6f30c519.8daccc","name":"Follow","func":"// given payload containing \n// {\"home/staircase/light/stat/POWER\": \"ON\" or \"OFF\", \"home/hallfloor/light/stat/POWER2\": \"ON\" or \"OFF\"}\n// msg.topic indicates whether the most recent message was from POWER or POWER2\n// take no action if the two states are the same\nif (msg.payload[\"home/staircase/light/stat/POWER\"] == msg.payload[\"home/hallfloor/light/stat/POWER2\"]) {\n    // the same so do nothing\n    msg = null\n} else {\n    // they are different, we want to set the one that is not the one that triggered this message\n    // to the same value as the one that triggered the message\n    if (msg.topic === \"home/staircase/light/stat/POWER\") {\n        // The staircase state has changed, set the hall to follow it\n        msg.payload = msg.payload[\"home/staircase/light/stat/POWER\"]\n        msg.topic = \"home/hallfloor/light/cmnd/POWER2\"\n    } else {\n        // The hall state has changed, set the staircase to follow it\n        msg.payload = msg.payload[\"home/hallfloor/light/stat/POWER2\"]\n        msg.topic = \"home/staircase/light/cmnd/POWER\"        \n    }\n}\nreturn msg;","outputs":1,"noerr":0,"x":437,"y":426,"wires":[["4e0c41e8.7a0418","7181f77a.26d4f8"]]},{"id":"7181f77a.26d4f8","type":"mqtt out","z":"6f30c519.8daccc","name":"CMND","topic":"","qos":"","retain":"","broker":"62798fd.c796c7","x":612,"y":425,"wires":[]},{"id":"a0628f2c.0378c","type":"inject","z":"6f30c519.8daccc","name":"","topic":"home/hallfloor/light/stat/POWER2","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":95,"y":353,"wires":[["cd7e2522.8ee08"]]},{"id":"f81c07e6.4f4a38","type":"inject","z":"6f30c519.8daccc","name":"","topic":"home/hallfloor/light/stat/POWER2","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":95,"y":308,"wires":[["cd7e2522.8ee08"]]},{"id":"b1a61cef.817a4","type":"inject","z":"6f30c519.8daccc","name":"","topic":"home/staircase/light/stat/POWER","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":96,"y":550,"wires":[["cd7e2522.8ee08"]]},{"id":"4299d546.db7cbc","type":"inject","z":"6f30c519.8daccc","name":"","topic":"home/staircase/light/stat/POWER","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":98,"y":504,"wires":[["cd7e2522.8ee08"]]},{"id":"4e0c41e8.7a0418","type":"debug","z":"6f30c519.8daccc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":595,"y":503,"wires":[]},{"id":"62798fd.c796c7","type":"mqtt-broker","z":"","name":"MQTT","broker":"10.10.1.10","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

In fact I fed the inject buttons into your flow and it did not seem to be behaving as I thought it should, so maybe I misunderstand something.