Please help simplify this. Multiple IFs to one Output

Ok, clearly I’m not familiar with JS super well; but what this flow does is grab all the properties of my Ecobee Thermostat’s Outside Weather conditions and then start filtering them off to ultimately end up at a function node that complies the statement i want my Echo device to read back to me.

This is definitely over complicating this — I’m sure of it. I’d like to simplify this flow.

Most people suggest i just use switches over functions, but I don’t want a switch node 5” tall with all these outputs in my flow.

Could i maybe combine all those function nodes into one with IF/ELSE IF statements? I did try this, but i was lost how to code it properly.

Any advice?

Thanks.


[{"id":"335737ad.1a0418","type":"switch","z":"b66e483.15a8eb8","name":"Raining?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"raining","vt":"str"},{"t":"eq","v":"rainy","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":440,"y":440,"wires":[["fba14caf.090db8"],["fba14caf.090db8"],["f0d69251.a2a27"]]},{"id":"fba14caf.090db8","type":"switch","z":"b66e483.15a8eb8","name":"Temp Suggestion =","property":"data.attributes.temperature","propertyType":"msg","rules":[{"t":"gte","v":"29","vt":"str"},{"t":"lte","v":"15","vt":"str"},{"t":"lte","v":"10","vt":"str"},{"t":"lte","v":"1","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":5,"x":630,"y":360,"wires":[["ff0d1714.bb5348"],["37d0fd60.3ffce2"],["bc61b2b9.09ca78"],["16c917db.eabcf"],["45c5cf66.e347e8"]]},{"id":"f0d69251.a2a27","type":"switch","z":"b66e483.15a8eb8","name":"Temp Suggestion =","property":"data.attributes.temperature","propertyType":"msg","rules":[{"t":"gte","v":"29","vt":"str"},{"t":"lte","v":"15","vt":"str"},{"t":"lte","v":"10","vt":"str"},{"t":"lte","v":"1","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":5,"x":630,"y":560,"wires":[["1dc33845.cf419"],["62ad0afb.48e24c"],["6596742d.5baab4"],["f6b75bc0.692d4"],["76a31530.d8ae0c"]]},{"id":"ff0d1714.bb5348","type":"function","z":"b66e483.15a8eb8","name":"Very Hot (+29c) and Raining >>","func":"msg.payload = {\n    data: {\"message\": \"Bring an umbrella Jon, also, its hot as balls. Currently its \" + msg.payload + \", and very hot at \" + msg.data.attributes.temperature + \" degrees outside. Ha, enjoy your walk\"}};\nreturn msg;\n\n","outputs":1,"noerr":0,"x":890,"y":280,"wires":[["b327215b.d6c9e"]]},{"id":"37d0fd60.3ffce2","type":"function","z":"b66e483.15a8eb8","name":"Chilly (15c - 10c) and Raining >>","func":"msg.payload = {\n    data: {\"message\": \"Bring an Umbrella. its a tad chilly and raining. Currently, its \" + msg.payload + \", and \" + msg.data.attributes.temperature + \" degrees outside. Make the best of this walk\"}};\nreturn msg;\n\n","outputs":1,"noerr":0,"x":920,"y":320,"wires":[["b327215b.d6c9e"]]},{"id":"bc61b2b9.09ca78","type":"function","z":"b66e483.15a8eb8","name":"Cold (10c - 1c) and Raining >>","func":"msg.payload = {\n    data: {\"message\": \"You'll need an umbrella jon. Its Cold  and raining outside. Id wear a hoodie. Currently its \" + msg.payload + \", and \" + msg.data.attributes.temperature + \" degrees outside. Enjoy your walk guys\"}};\nreturn msg;\n\n","outputs":1,"noerr":0,"x":890,"y":360,"wires":[["b327215b.d6c9e"]]},{"id":"16c917db.eabcf","type":"function","z":"b66e483.15a8eb8","name":"Freezing (below 1c) and Raining >>","func":"msg.payload = {\n    data: {\"message\": \"Its cold as balls, and fucking raining. Wear a jacket and bring an umbrella. Currently its \" + msg.payload + \", and \" + msg.data.attributes.temperature + \" degrees outside. Fuck this shit, move to the bahamas\"}};\nreturn msg;\n\n","outputs":1,"noerr":0,"x":920,"y":400,"wires":[["b327215b.d6c9e"]]},{"id":"45c5cf66.e347e8","type":"function","z":"b66e483.15a8eb8","name":"Nice temp (15c - 28c) and Raining >>","func":"msg.payload = {\n    data: {\"message\": \"It's a Nice temperature, but bring an umbrella Jon. Its currently \" + msg.payload + \", and \" + msg.data.attributes.temperature + \" degrees outside. Enjoy your walk guys\"}};return msg;\n\n","outputs":1,"noerr":0,"x":910,"y":440,"wires":[["b327215b.d6c9e"]]},{"id":"1dc33845.cf419","type":"function","z":"b66e483.15a8eb8","name":"Very Hot (+29c)  >>","func":"msg.payload = {\n    data: {\"message\": \"Bust out your skimpiest thong Jon, is very hot. Currently its \" + msg.payload + \", and very hot at \" + msg.data.attributes.temperature + \" degrees outside. ill have the A C blasting for you guys when you return\"}};\nreturn msg;\n\n","outputs":1,"noerr":0,"x":850,"y":480,"wires":[["b327215b.d6c9e"]]},{"id":"62ad0afb.48e24c","type":"function","z":"b66e483.15a8eb8","name":"Chilly (15c - 10c) >>","func":"msg.payload = {\n    data: {\"message\": \"its a tad chilly willy. Currently, its \" + msg.payload + \", and chilly at \" + msg.data.attributes.temperature + \" degrees outside\"}};\nreturn msg;\n\n","outputs":1,"noerr":0,"x":880,"y":520,"wires":[["b327215b.d6c9e"]]},{"id":"6596742d.5baab4","type":"function","z":"b66e483.15a8eb8","name":"Cold (10c - 1c)  >>","func":"msg.payload = {\n    data: {\"message\": \"Its Cold out, Id wear a hoodie. Currently its \" + msg.payload + \", and cold at \" + msg.data.attributes.temperature + \" degrees outside\"}};\nreturn msg;\n\n","outputs":1,"noerr":0,"x":850,"y":560,"wires":[["b327215b.d6c9e"]]},{"id":"f6b75bc0.692d4","type":"function","z":"b66e483.15a8eb8","name":"Freezing (below 1c)  >>","func":"msg.payload = {\n    data: {\"message\": \"Its cold as balls, Wear a jacket bitch, or just pack up and move to the bahamas. Currently its \" + msg.payload + \", and freezing  at \" + msg.data.attributes.temperature + \" degrees outside\"}};\nreturn msg;\n\n","outputs":1,"noerr":0,"x":880,"y":600,"wires":[["b327215b.d6c9e"]]},{"id":"76a31530.d8ae0c","type":"function","z":"b66e483.15a8eb8","name":"Nice temp (15c - 28c)  >>","func":"msg.payload = {\n    data: {\"message\": \"its a Nice temperature outside. Its currently \" + msg.payload + \", and \" + msg.data.attributes.temperature + \" degrees. Have a great walk you two\"}};\nreturn msg;\n\n","outputs":1,"noerr":0,"x":870,"y":640,"wires":[["b327215b.d6c9e"]]},{"id":"7d21809c.cdedb8","type":"api-current-state","z":"b66e483.15a8eb8","name":"Current Weather Conditions","server":"41c2e4cc.1954cc","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"weather.ecobee4_thermostat","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":220,"y":440,"wires":[["335737ad.1a0418"]]},{"id":"271665df.9bdc6a","type":"inject","z":"b66e483.15a8eb8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":380,"wires":[["7d21809c.cdedb8"]]},{"id":"b327215b.d6c9e","type":"debug","z":"b66e483.15a8eb8","name":"Off to Alexa to read back >>","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1360,"y":440,"wires":[]},{"id":"41c2e4cc.1954cc","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

You could put all the messages into a two dimensional array, 5 by 2, where the first index 0 to 4 represents the four temperature ranges and the second index 0, 1 represents raining or not raining. Then in the code work out the temperature index using something like (this is just pseudo code to save me typing)

if temperature <= 1 then t = 0
else if temperature <=10 then t = 1
etc

then similarly for rain

if rainy || raining then r = 0
else r = 1

Then you can get the response using answer = answers[t][r]

I like your flow. It is pretty easy to understand what you want to do.

I would just recommend to double check the config of your switch nodes. You probably want to check numbers (rather than strings) and want to stop after the first match. Probably below config would works better in your flow.

Anyway, if you want to have less nodes in the flow an alternative to what Colin proposed above could be the use of an object like below:

let dic = {
    "raining" : {
        "VeryHot" : `Bring an umbrella Jon, also, its hot as balls. Currently its ${msg.payload.rain}, and very hot at ${msg.payload.temperature} degrees outside. Ha, enjoy your walk`,
        "Chilly" :  `Bring an Umbrella. its a tad chilly and raining. Currently, its ${msg.payload.rain}, and ${msg.payload.temperature} degrees outside. Make the best of this walk`,
        "Cold" : `Raining-Cold`,
        "Freezing" : `Raining-Freezing`,
        "Nice" : `Raining-Nice`
    },
    "rainy" : {
        "VeryHot" : `Rainy-VeryHot`,
        "Chilly" :  `Rainy-Chilly`,
        "Cold" : `Rainy-Cold`,
        "Freezing" : `Rainy-Freezing`,
        "Nice" : `Rainy-Nice`
    }
};

msg.payload = dic[msg.payload.rain][msg.stringtemp];

return msg;

Flow:

[{"id":"5694db85.d50784","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"7ba84406.0401ec","type":"inject","z":"5694db85.d50784","name":"","topic":"rain","payload":"rainy","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":140,"wires":[["3d784bf9.509fe4"]]},{"id":"e827b6b4.0c0db8","type":"function","z":"5694db85.d50784","name":"Message Select","func":"let dic = {\n    \"raining\" : {\n        \"VeryHot\" : `Bring an umbrella Jon, also, its hot as balls. Currently its ${msg.payload.rain}, and very hot at ${msg.payload.temperature} degrees outside. Ha, enjoy your walk`,\n        \"Chilly\" :  `Bring an Umbrella. its a tad chilly and raining. Currently, its ${msg.payload.rain}, and ${msg.payload.temperature} degrees outside. Make the best of this walk`,\n        \"Cold\" : `Raining-Cold`,\n        \"Freezing\" : `Raining-Freezing`,\n        \"Nice\" : `Raining-Nice`\n    },\n    \"rainy\" : {\n        \"VeryHot\" : `Rainy-VeryHot`,\n        \"Chilly\" :  `Rainy-Chilly`,\n        \"Cold\" : `Rainy-Cold`,\n        \"Freezing\" : `Rainy-Freezing`,\n        \"Nice\" : `Rainy-Nice`\n    }\n};\n\nmsg.payload = dic[msg.payload.rain][msg.stringtemp];\n\nreturn msg;","outputs":1,"noerr":0,"x":1480,"y":180,"wires":[["dee44fc4.a3b04"]]},{"id":"2df8c6ba.66ab2a","type":"inject","z":"5694db85.d50784","name":"","topic":"rain","payload":"raining","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":100,"wires":[["3d784bf9.509fe4"]]},{"id":"3914ac4e.c89ca4","type":"inject","z":"5694db85.d50784","name":"","topic":"temperature","payload":"40","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":260,"wires":[["5f43cdb1.1c6cd4"]]},{"id":"24c8207f.6de4","type":"inject","z":"5694db85.d50784","name":"","topic":"temperature","payload":"25","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":300,"wires":[["5f43cdb1.1c6cd4"]]},{"id":"1b4b0024.b2434","type":"inject","z":"5694db85.d50784","name":"","topic":"temperature","payload":"12","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":340,"wires":[["5f43cdb1.1c6cd4"]]},{"id":"f226f807.6e0048","type":"inject","z":"5694db85.d50784","name":"","topic":"temperature","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":380,"wires":[["5f43cdb1.1c6cd4"]]},{"id":"351bc9ac.920f76","type":"inject","z":"5694db85.d50784","name":"","topic":"temperature","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":420,"wires":[["5f43cdb1.1c6cd4"]]},{"id":"3d784bf9.509fe4","type":"link out","z":"5694db85.d50784","name":"","links":["63e6d676.846f78"],"x":375,"y":120,"wires":[]},{"id":"d4eaa0c7.f687a","type":"join","z":"5694db85.d50784","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":670,"y":220,"wires":[["1eb3d6d5.bcf259"]]},{"id":"5f43cdb1.1c6cd4","type":"link out","z":"5694db85.d50784","name":"","links":["63e6d676.846f78"],"x":375,"y":320,"wires":[]},{"id":"63e6d676.846f78","type":"link in","z":"5694db85.d50784","name":"","links":["3d784bf9.509fe4","5f43cdb1.1c6cd4"],"x":495,"y":220,"wires":[["d4eaa0c7.f687a"]]},{"id":"dee44fc4.a3b04","type":"debug","z":"5694db85.d50784","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1650,"y":180,"wires":[]},{"id":"1eb3d6d5.bcf259","type":"switch","z":"5694db85.d50784","name":"Temp Suggestion =","property":"payload.temperature","propertyType":"msg","rules":[{"t":"gte","v":"29","vt":"num"},{"t":"lte","v":"15","vt":"num"},{"t":"lte","v":"10","vt":"num"},{"t":"lte","v":"1","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":5,"x":850,"y":220,"wires":[["baa68210.d6918"],["aa274e68.46ee6"],["7168aa98.3ab1b4"],["c7f77b7b.e68d28"],["fccd352e.c3e6d8"]]},{"id":"baa68210.d6918","type":"change","z":"5694db85.d50784","name":"Very Hot (+29c) ","rules":[{"t":"set","p":"stringtemp","pt":"msg","to":"VeryHot","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":140,"wires":[["b0b44b60.d07688"]]},{"id":"aa274e68.46ee6","type":"change","z":"5694db85.d50784","name":"Chilly (15c - 10c)","rules":[{"t":"set","p":"stringtemp","pt":"msg","to":"Chilly","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1090,"y":180,"wires":[["b0b44b60.d07688"]]},{"id":"7168aa98.3ab1b4","type":"change","z":"5694db85.d50784","name":"Cold (10c - 1c)","rules":[{"t":"set","p":"stringtemp","pt":"msg","to":"Cold","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":220,"wires":[["b0b44b60.d07688"]]},{"id":"c7f77b7b.e68d28","type":"change","z":"5694db85.d50784","name":"Freezing (below 1c)","rules":[{"t":"set","p":"stringtemp","pt":"msg","to":"Freezing","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1090,"y":260,"wires":[["b0b44b60.d07688"]]},{"id":"fccd352e.c3e6d8","type":"change","z":"5694db85.d50784","name":"Nice temp (15c - 28c) ","rules":[{"t":"set","p":"stringtemp","pt":"msg","to":"Nice","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":300,"wires":[["b0b44b60.d07688"]]},{"id":"b0b44b60.d07688","type":"link out","z":"5694db85.d50784","name":"","links":["88f6bfed.ea5f1"],"x":1295,"y":180,"wires":[]},{"id":"88f6bfed.ea5f1","type":"link in","z":"5694db85.d50784","name":"","links":["b0b44b60.d07688"],"x":1355,"y":180,"wires":[["e827b6b4.0c0db8"]]}]