Mqtt monitoring / syncing states, need bit of help

There may well be ways of refactoring the code for the case with three inputs. I will leave that as an exercise for the reader.