Will do. It was more the having a changeable context based on the topic that's been confusing me. I can set and get stored data when naming the context myself, but just haven't been able to store it dynamically.
I suppose I'm maybe being a bit too ambitious to start off with, when I could just be using preexisting nodes, until I'm used to the more basic functions.
[{"id":"8e1afa9c.72c718","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"74191c84.d19164","type":"mqtt out","z":"8e1afa9c.72c718","name":"","topic":"","qos":"0","retain":"true","broker":"2f3156fc.9fdc8a","x":570,"y":420,"wires":[]},{"id":"a6afd3e3.c88ca","type":"inject","z":"8e1afa9c.72c718","name":"","topic":"home/upstairs/hall/motion","payload":"true","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":360,"wires":[["74191c84.d19164"]]},{"id":"e2cb9261.179d5","type":"inject","z":"8e1afa9c.72c718","name":"","topic":"home/upstairs/hall/motion","payload":"false","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":400,"wires":[["74191c84.d19164"]]},{"id":"a113d9cb.4e60a8","type":"inject","z":"8e1afa9c.72c718","name":"","topic":"home/upstairs/office/motion","payload":"true","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":440,"wires":[["74191c84.d19164"]]},{"id":"3fb7c4f9.9fa55c","type":"inject","z":"8e1afa9c.72c718","name":"","topic":"home/upstairs/office/motion","payload":"false","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":480,"wires":[["74191c84.d19164"]]},{"id":"5e419626.d3e1c8","type":"mqtt in","z":"8e1afa9c.72c718","name":"","topic":"+/+/+/motion/occupied","qos":"0","datatype":"auto","broker":"2f3156fc.9fdc8a","x":160,"y":220,"wires":[["564234a4.303d3c"]]},{"id":"59c09eeb.8336","type":"mqtt in","z":"8e1afa9c.72c718","name":"","topic":"+/+/+/motion/presence","qos":"0","datatype":"auto","broker":"2f3156fc.9fdc8a","x":160,"y":160,"wires":[["564234a4.303d3c"]]},{"id":"564234a4.303d3c","type":"function","z":"8e1afa9c.72c718","name":"","func":"var presence = msg.payload\nvar topic = msg.topic\n\nvar topicchange = {\n 'home/upstairs/office/motion/presence': 'light.office_light',\n 'home/upstairs/hall/motion/presence': 'light.upper_hallway_light',\n 'home/downstairs/hall/presence': 'light.lower_hallway_light',\n};\n\n\nfor(var newtopic in topicchange) {\n if(newtopic != msg.topic) continue;\n var newMsg = {};\n var occupied = context.get(topicchange[newtopic])\n newMsg.topic = topic\n newMsg.payload = {\n entity_id : topicchange[newtopic]\n }\n newMsg.occupied = occupied\n newMsg.presence = presence;\n return [newMsg];\n}\n\n\nif (msg.topic.includes(\"upstairs/hall/motion/occupied\")) {\n if (msg.payload == \"true\") {\n context.set('light.upper_hallway_light', msg.occupied = \"true\")\n }\n if (msg.payload == \"false\") {\n context.set('light.upper_hallway_light', msg.occupied = \"false\")\n }\n}\nif (msg.topic.includes(\"upstairs/office/motion/occupied\")) {\n if (msg.payload == \"true\") {\n context.set('light.office_light', msg.occupied = \"true\")\n }\n if (msg.payload == \"false\") {\n context.set('light.office_light', msg.occupied = \"false\")\n }\n}","outputs":1,"noerr":0,"x":530,"y":220,"wires":[["4e6e4884.8b5be8"]]},{"id":"4e6e4884.8b5be8","type":"debug","z":"8e1afa9c.72c718","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","x":690,"y":160,"wires":[]},{"id":"6e856aed.e3b024","type":"function","z":"8e1afa9c.72c718","name":"Room Presence/Occupation","func":"var msg1 = {\n topic : msg.topic+\"/presence\",\n payload : true\n}\nvar msg2 = {\n topic : msg.topic+\"/presence\",\n payload : false\n}\n\nvar resetentrancehall = {\n topic : \"home/downstairs/hall/motion\",\n payload : \"reset\"\n}\n\nvar resetdiningroom = {\n topic : \"home/downstairs/diningroom/motion\",\n payload : \"reset\"\n}\n\nvar resetkitchen = {\n topic : \"home/downstairs/kitchen/motion\",\n payload : \"reset\"\n}\n\nvar resetlivingroom = {\n topic : \"home/downstairs/livingroom/motion\",\n payload : \"reset\"\n}\n\nvar resetupstairshallway = {\n topic : \"home/upstairs/hall/motion\",\n payload : \"reset\"\n}\n\nvar resetdressingroom = {\n topic : \"home/upstairs/dressingroom/motion\",\n payload : \"reset\"\n}\n\nvar resetbedroom = {\n topic : \"home/upstairs/bedroom/motion\",\n payload : \"reset\"\n}\n\nvar resetoffice = {\n topic : \"home/upstairs/office/motion\",\n payload : \"reset\"\n}\n\nvar resettoilet = {\n topic : \"home/upstairs/toilet/motion\",\n payload : \"reset\"\n}\n\n// Entrance Hallway\n\nvar entrancehall = context.get('entrancehall') ;\n if (typeof entrancehall == \"undefined\")\n entrancehall = 0;\n\nif (msg.topic.includes(\"downstairs/hall\")) {\n if (msg.payload == \"true\") {\n msg.topic = msg.topic\n context.set('entrancehall',entrancehall+1)\n msg.payload = +entrancehall+1;\n node.status({\n fill: \"green\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg1, resetdiningroom, resetkitchen, resetlivingroom]];\n }\n if (msg.payload == \"false\") {\n msg.topic = msg.topic\n msg.payload = entrancehall\n node.status({\n fill: \"red\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg2]];\n }\n if (msg.payload == \"reset\") {\n context.set('entrancehall',0)\n msg.payload = 0\n return msg;\n }\n if (msg.payload == \"3\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = true\n return msg;\n }\n if (msg.payload == \"0\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = false\n return msg;\n }\n}\n\n// Dining Room\n\nvar diningroom = context.get('diningroom') ;\n if (typeof diningroom == \"undefined\")\n diningroom = 0;\n\nif (msg.topic.includes(\"downstairs/diningroom\")) {\n if (msg.payload == \"true\") {\n msg.topic = msg.topic\n context.set('diningroom',diningroom+1)\n msg.payload = +diningroom+1;\n node.status({\n fill: \"green\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg1, resetentrancehall]];\n }\n if (msg.payload == \"false\") {\n msg.topic = msg.topic\n msg.payload = diningroom\n node.status({\n fill: \"red\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg2]];\n }\n if (msg.payload == \"reset\") {\n context.set('diningroom',0)\n msg.payload = 0\n return msg;\n }\n if (msg.payload == \"3\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = true\n return msg;\n }\n if (msg.payload == \"0\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = false\n return msg;\n }\n}\n\n// Kitchen\n\nvar kitchen = context.get('kitchen') ;\n if (typeof kitchen == \"undefined\")\n kitchen = 0;\n\nif (msg.topic.includes(\"downstairs/kitchen\")) {\n if (msg.payload == \"true\") {\n msg.topic = msg.topic\n context.set('kitchen',kitchen+1)\n msg.payload = +kitchen+1;\n node.status({\n fill: \"green\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg1, resetentrancehall]];\n }\n if (msg.payload == \"false\") {\n msg.topic = msg.topic\n msg.payload = kitchen\n node.status({\n fill: \"red\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg2]];\n }\n if (msg.payload == \"reset\") {\n context.set('kitchen',0)\n msg.payload = 0\n return msg;\n }\n if (msg.payload == \"3\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = true\n return msg;\n }\n if (msg.payload == \"0\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = false\n return msg;\n }\n}\n\n// Living Room\n\nvar livingroom = context.get('livingroom') ;\n if (typeof livingroom == \"undefined\")\n livingroom = 0;\n\nif (msg.topic.includes(\"downstairs/livingroom\")) {\n if (msg.payload == \"true\") {\n msg.topic = msg.topic\n context.set('livingroom',livingroom+1)\n msg.payload = +livingroom+1;\n node.status({\n fill: \"green\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg1, resetentrancehall]];\n }\n if (msg.payload == \"false\") {\n msg.topic = msg.topic\n msg.payload = livingroom\n node.status({\n fill: \"red\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg2]];\n }\n if (msg.payload == \"reset\") {\n context.set('livingroom',0)\n msg.payload = 0\n return msg;\n }\n if (msg.payload == \"3\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = true\n return msg;\n }\n if (msg.payload == \"0\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = false\n return msg;\n }\n}\n\n// Upstairs Hallway\n\nvar upstairshallway = context.get('upstairshallway') ;\n if (typeof upstairshallway == \"undefined\")\n upstairshallway = 0;\n\nif (msg.topic.includes(\"upstairs/hall\")) {\n if (msg.payload == \"true\") {\n msg.topic = msg.topic\n context.set('upstairshallway',upstairshallway+1)\n msg.payload = +upstairshallway+1;\n node.status({\n fill: \"green\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg1, resetentrancehall, resetdressingroom, resetbedroom, resetoffice, resettoilet]];\n }\n if (msg.payload == \"false\") {\n msg.topic = msg.topic\n msg.payload = upstairshallway\n node.status({\n fill: \"red\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg2]];\n }\n if (msg.payload == \"reset\") {\n context.set('upstairshallway',0)\n msg.payload = 0\n return msg;\n }\n if (msg.payload == \"3\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = true\n return msg;\n }\n if (msg.payload == \"0\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = false\n return msg;\n }\n}\n\n// Dressing Room\n\nvar dressingroom = context.get('dressingroom') ;\n if (typeof dressingroom == \"undefined\")\n dressingroom = 0;\n\nif (msg.topic.includes(\"upstairs/dressingroom\")) {\n if (msg.payload == \"true\") {\n msg.topic = msg.topic\n context.set('dressingroom',dressingroom+1)\n msg.payload = +dressingroom+1;\n node.status({\n fill: \"green\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg1, resetupstairshallway]];\n }\n if (msg.payload == \"false\") {\n msg.topic = msg.topic\n msg.payload = dressingroom\n node.status({\n fill: \"red\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg2]];\n }\n if (msg.payload == \"reset\") {\n context.set('dressingroom',0)\n msg.payload = 0\n return msg;\n }\n if (msg.payload == \"3\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = true\n return msg;\n }\n if (msg.payload == \"0\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = false\n return msg;\n }\n}\n\n// Bedroom\n\nvar bedroom = context.get('bedroom') ;\n if (typeof bedroom == \"undefined\")\n bedroom = 0;\n\nif (msg.topic.includes(\"upstairs/bedroom\")) {\n if (msg.payload == \"true\") {\n msg.topic = msg.topic\n context.set('bedroom',bedroom+1)\n msg.payload = +bedroom+1;\n node.status({\n fill: \"green\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg1, resetupstairshallway]];\n }\n if (msg.payload == \"false\") {\n msg.topic = msg.topic\n msg.payload = bedroom\n node.status({\n fill: \"red\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg2]];\n }\n if (msg.payload == \"reset\") {\n context.set('bedroom',0)\n msg.payload = 0\n return msg;\n }\n if (msg.payload == \"3\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = true\n return msg;\n }\n if (msg.payload == \"0\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = false\n return msg;\n }\n}\n\n// Office\n\nvar office = context.get('office') ;\n if (typeof office == \"undefined\")\n office = 0;\n\nif (msg.topic.includes(\"upstairs/office\")) {\n if (msg.payload == \"true\") {\n msg.topic = msg.topic\n context.set('office',office+1)\n msg.payload = +office+1;\n node.status({\n fill: \"green\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg1, resetupstairshallway]];\n }\n if (msg.payload == \"false\") {\n msg.topic = msg.topic\n msg.payload = office\n node.status({\n fill: \"red\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg2]];\n }\n if (msg.payload == \"reset\") {\n context.set('office',0)\n msg.payload = 0\n return msg;\n }\n if (msg.payload == \"3\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = true\n return msg;\n }\n if (msg.payload == \"0\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = false\n return msg;\n }\n}\n\n// Toilet\n\nvar toilet = context.get('toilet') ;\n if (typeof toilet == \"undefined\")\n toilet = 0;\n\nif (msg.topic.includes(\"upstairs/toilet\")) {\n if (msg.payload == \"true\") {\n msg.topic = msg.topic\n context.set('toilet',toilet+1)\n msg.payload = +toilet+1;\n node.status({\n fill: \"green\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg1, resetupstairshallway]];\n }\n if (msg.payload == \"false\") {\n msg.topic = msg.topic\n msg.payload = toilet\n node.status({\n fill: \"red\",\n shape: \"dot\",\n text: msg.topic\n });\n return [[msg, msg2]];\n }\n if (msg.payload == \"reset\") {\n context.set('toilet',0)\n msg.payload = 0\n return msg;\n }\n if (msg.payload == \"3\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = true\n return msg;\n }\n if (msg.payload == \"0\") {\n msg.topic = msg.topic+\"/occupied\"\n msg.payload = false\n return msg;\n }\n}","outputs":1,"noerr":0,"x":360,"y":100,"wires":[["7366e0a7.a83e6"]]},{"id":"7366e0a7.a83e6","type":"mqtt out","z":"8e1afa9c.72c718","name":"","topic":"","qos":"0","retain":"true","broker":"2f3156fc.9fdc8a","x":570,"y":100,"wires":[]},{"id":"99f77908.9003d8","type":"mqtt in","z":"8e1afa9c.72c718","name":"","topic":"+/+/+/motion","qos":"0","datatype":"auto","broker":"2f3156fc.9fdc8a","x":130,"y":100,"wires":[["6e856aed.e3b024"]]},{"id":"dcbb3401.f29598","type":"comment","z":"8e1afa9c.72c718","name":"Manually inputted context.set in the above function","info":"","x":530,"y":260,"wires":[]},{"id":"2f3156fc.9fdc8a","type":"mqtt-broker","z":"","name":"","broker":"192.168.1.243","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
Here's basically what I'm working with at the moment. So I'm able to take the context data depending on the topic, but not assign it, so I've put them in manually. There's probably a lot of mistakes in this, but it does what I wanted it to do.
Basically, if a room detects presence 3 times, it shows up at the end that it's occupied, so as not to try and turn out any lights. Simple stuff really, but I'm just making it overly complicated by not using other nodes.
I'll take a look at your code and see if there's anything I can take from it, as I just want to understand how it works, more than actually getting it to work. I can see how it works, thanks to all the great tool-tips, it's just figuring out how they can be applied elsewhere. It's all well and good copying and pasting, but that's not going to help me in the long term.