One function, cant get any simpler.
[{"id":"27a1ab00.0b57f6","type":"inject","z":"c9ca6ac7.25b568","name":"fake weather overcast heavy calm","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"cloud\":\"overcast\",\"rain\":\"heavy\",\"wind\":\"calm\"}","payloadType":"json","x":700,"y":100,"wires":[["60e11c6.aa954e4"]]},{"id":"60e11c6.aa954e4","type":"function","z":"c9ca6ac7.25b568","name":"Get icon from weather","func":"//A 3 deep lookup. Level1=coulds, level2=rain, level3=wind \nvar icons = {\n overcast: {//clouds\n heavy: {//rain\n calm: \"icon1\", slight: \"icon2\", strong: \"icon3\", gusty: \"icon4\"//winds \n },\n moderate: {\n calm: \"icon5\", slight: \"icon6\", strong: \"icon7\", gusty: \"icon8\"\n },\n rain: {\n calm: \"icon9\", slight: \"icon10\", strong: \"icon11\", gusty: \"icon12\"\n },\n light: {\n calm: \"icon13\", slight: \"icon14\", strong: \"icon15\", gusty: \"icon16\"\n },\n },\n broken: {//clouds\n heavy: {//rain\n calm: \"icon17\", slight: \"icon18\", strong: \"icon19\", gusty: \"icon20\"\n },\n moderate: {\n calm: \"icon21\", slight: \"icon22\", strong: \"icon23\", gusty: \"icon24\"\n },\n rain: {\n calm: \"icon25\", slight: \"icon26\", strong: \"icon27\", gusty: \"icon28\"\n },\n light: {\n calm: \"icon29\", slight: \"icon30\", strong: \"icon31\", gusty: \"icon32\"\n },\n },\n scattered: {\n heavy: {\n calm: \"icon33\", slight: \"icon34\", strong: \"icon35\", gusty: \"icon36\"\n },\n moderate: {\n calm: \"icon37\", slight: \"icon38\", strong: \"icon39\", gusty: \"icon40\"\n },\n rain: {\n calm: \"icon41\", slight: \"icon42\", strong: \"icon43\", gusty: \"icon44\"\n },\n light: {\n calm: \"icon45\", slight: \"icon46\", strong: \"icon47\", gusty: \"icon48\"\n },\n },\n few: {\n heavy: {\n calm: \"icon49\", slight: \"icon50\", strong: \"icon51\", gusty: \"icon52\"\n },\n moderate: {\n calm: \"icon53\", slight: \"icon54\", strong: \"icon55\", gusty: \"icon56\"\n },\n rain: {\n calm: \"icon57\", slight: \"icon58\", strong: \"icon59\", gusty: \"icon60\"\n },\n light: {\n calm: \"icon61\", slight: \"icon62\", strong: \"icon63\", gusty: \"icon64\"\n },\n }\n}\n\ntry {\n msg.payload.icon = icons[msg.payload.cloud][msg.payload.rain][msg.payload.wind];\n} catch (e) {\n msg.payload.icon = \"default_icon\"\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1020,"y":100,"wires":[["8a183408.53dbf8"]]},{"id":"eabea2b8.fd9a5","type":"inject","z":"c9ca6ac7.25b568","name":"fake weather scattered rain gusty","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"cloud\":\"scattered\",\"rain\":\"rain\",\"wind\":\"gusty\"}","payloadType":"json","x":690,"y":140,"wires":[["60e11c6.aa954e4"]]},{"id":"f7cdef6d.bd38a","type":"inject","z":"c9ca6ac7.25b568","name":"fake weather few light calm","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"cloud\":\"few\",\"rain\":\"light\",\"wind\":\"strong\"}","payloadType":"json","x":670,"y":180,"wires":[["60e11c6.aa954e4"]]},{"id":"8a183408.53dbf8","type":"debug","z":"c9ca6ac7.25b568","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload.icon","statusType":"msg","x":1050,"y":140,"wires":[]},{"id":"e867e80e.eb51e8","type":"inject","z":"c9ca6ac7.25b568","name":"fake weather black cats-n-dogs hoollie (bad data)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"cloud\":\"black\",\"rain\":\"cats-n-dogs\",\"wind\":\"hoollie\"}","payloadType":"json","x":740,"y":220,"wires":[["60e11c6.aa954e4"]]},{"id":"b7747690.2b8d78","type":"comment","z":"c9ca6ac7.25b568","name":"Using injects as test data","info":"","x":670,"y":60,"wires":[]}]
The concept...
- Make a lookup JS object (or as you might think of it - some JSON)
- the object has 3 levels of
cloud.rain.wind
with every possible combination (64 of them)
- access the required item using bracket notation instead of dots
dot notation examples...
var the_icon = icons.overcast.heavy.gusty;
var another_icon = icons.overcast.moderate.strong;
bracket notation example (note it is EXACTLY the same as the familiar dot notation above)...
var the_icon = icons["overcast"]["heavy"]["gusty"];
var another_icon = icons["overcast"]["moderate"]["strong"];
so using the bracket notation we can dynamically lookup the icon for msg.payload.cloud, msg.payload.rain, msg.payload.wind...
msg.payload.icon = icons[msg.payload.cloud][msg.payload.rain][msg.payload.wind];