Another solution would be this:
[{"id":"5010e08.b51242","type":"function","z":"cc639dbb.119d5","name":"T-Flipflop","func":"var currentState = context.get('state') || \"on\";\n\n\nif (currentState == \"on\")\n{\n context.set(\"state\",\"off\");\n msg.payload = \"off\";\n}\nelse if (currentState == \"off\")\n{\n context.set(\"state\",\"on\");\n msg.payload = \"on\"; \n}\nelse\n{\n return;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":400,"y":180,"wires":[["119ed7f6.6a5288"]]},{"id":"9a200f12.51c8c","type":"server-state-changed","z":"cc639dbb.119d5","name":"test switch","server":"44b2605f.5d41","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.test_switch","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":100,"y":120,"wires":[["336833a4.08971c"]]},{"id":"336833a4.08971c","type":"switch","z":"cc639dbb.119d5","name":"only when on","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":250,"y":120,"wires":[["5010e08.b51242","2f606a03.e9a0c6"]]},{"id":"2f606a03.e9a0c6","type":"delay","z":"cc639dbb.119d5","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":440,"y":120,"wires":[["29ed3cda.0cba74"]]},{"id":"29ed3cda.0cba74","type":"api-call-service","z":"cc639dbb.119d5","name":"tor test switch off","server":"44b2605f.5d41","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.test_switch","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":640,"y":120,"wires":[[]]},{"id":"119ed7f6.6a5288","type":"debug","z":"cc639dbb.119d5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":180,"wires":[]},{"id":"44b2605f.5d41","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"on","connectionDelay":false,"cacheJson":false}]
EDIT:
this is a good guide about node red variables:
http://www.steves-internet-guide.com/node-red-variables/