If you post config snippets you always should use the function here to paste preformatted stuff or else the indents get lost.
For the question I can’t think of an easy solution right now…buuut you could use a input boolean for the switch and 3 automations. One for switching on, one for switching off and another one that triggers the switch depending on the mqtt topic.
I may over engineer here and there might be a easy solution.