I like to use the function node, but I'm not a perfect javascript programmer (not even close to perfect indeed )
[{"id":"898a956a.a6fde","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"867723a2.bdcbc8","type":"inject","z":"898a956a.a6fde","name":"toggle switch","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":220,"wires":[["99e5d35f.e94da8"]]},{"id":"99e5d35f.e94da8","type":"function","z":"898a956a.a6fde","name":"","func":"// get status from context\nvar s = flow.get(\"athome\");\n\n// check if variable exists\nif (typeof s == 'undefined') {\n // set to 0 if not exist\n s = false;\n}\n\n// toggle status\ns = !s;\n// save status to context\nflow.set(\"athome\", s);\n\n// so now what? are we at home\nvar msg1;\nvar msg2;\nvar msg3;\nvar msg4;\nif (s) {\n // turn on shelly 1\n msg1 = { payload:{\"turn\":\"on\"} };\n msg2 = { payload:{} };\n msg3 = { payload:{} };\n msg4 = { payload:{} };\n} else {\n msg1 = { payload:{\"turn\":\"off\"} };\n msg2 = { payload:{\"turn\":\"off\"} };\n msg3 = { payload:{\"turn\":\"off\"}};\n msg4 = { payload:{\"turn\":\"off\"} };\n}\nreturn [msg1, msg2, msg3, msg4];\n\n","outputs":4,"noerr":0,"initialize":"","finalize":"","x":400,"y":220,"wires":[["6b3f6848.1a9a08"],["6b3f6848.1a9a08"],["6b3f6848.1a9a08"],["6b3f6848.1a9a08"]]},{"id":"6b3f6848.1a9a08","type":"debug","z":"898a956a.a6fde","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":220,"wires":[]}]
This example has an inject node that is like a toggle switch. It toggles the status and then the function will output 4 payloads. I thing I have the same payload to toggle my shelly at home, but I'm not sure (not at home at the moment). You just have to connect each output with the corresponding mqtt node.
For sure there is another, possible easier way to do that