Openweathermap - how to compare the values inside an array

Hello guys,
I'm using openweathermap node and get 5 day data.
Now I'm only interested in clouds object inside the array. Is there a way to extract the lowest value?

in the picture you can see the first value of 40
Now I need a function to search for clouds.all value in all 40 objects and pick the lowest one to return it.

Hi sfedo,

Thats were Jsonata expressions shine, with very little code
In a Change node try $min(msg.payload.clouds.all)

Example Flow:

[{"id":"fe803e82.c6981","type":"inject","z":"8889c53d.0c1208","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"dt\":1620054000,\"main\":{},\"weather\":[],\"clouds\":{\"all\":83},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620054200,\"main\":{},\"weather\":[],\"clouds\":{\"all\":23},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620054400,\"main\":{},\"weather\":[],\"clouds\":{\"all\":33},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620054500,\"main\":{},\"weather\":[],\"clouds\":{\"all\":55},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620064000,\"main\":{},\"weather\":[],\"clouds\":{\"all\":73},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"}]","payloadType":"json","x":300,"y":340,"wires":[["94b1e917.13e86","e2dec559.5440b"]]},{"id":"94b1e917.13e86","type":"change","z":"8889c53d.0c1208","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$min(msg.payload.clouds.all)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":340,"wires":[["23c1a312.0a086c"]]},{"id":"e2dec559.5440b","type":"debug","z":"8889c53d.0c1208","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":370,"y":260,"wires":[]},{"id":"23c1a312.0a086c","type":"debug","z":"8889c53d.0c1208","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":340,"wires":[]}]

PS. you want the value returned or the whole object that has the lowest cloud.all value?

wow thats really cool, now I would like to have the index of this value? :slight_smile:

hehe .. and thats were i start having problems with Jsonata and default to javascript :sweat_smile:

[{"id":"fe803e82.c6981","type":"inject","z":"8889c53d.0c1208","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"dt\":1620054000,\"main\":{},\"weather\":[],\"clouds\":{\"all\":83},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620054200,\"main\":{},\"weather\":[],\"clouds\":{\"all\":23},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620054400,\"main\":{},\"weather\":[],\"clouds\":{\"all\":33},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620054500,\"main\":{},\"weather\":[],\"clouds\":{\"all\":55},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620064000,\"main\":{},\"weather\":[],\"clouds\":{\"all\":73},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"}]","payloadType":"json","x":300,"y":340,"wires":[["e2dec559.5440b","1497fe3c.30f472"]]},{"id":"e2dec559.5440b","type":"debug","z":"8889c53d.0c1208","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":370,"y":260,"wires":[]},{"id":"1497fe3c.30f472","type":"function","z":"8889c53d.0c1208","name":"","func":"let minValue = msg.payload[0].clouds.all // start with 1st value\nlet minIndex = 0\n\nmsg.payload.forEach( (el, index) => {\n    \n    if (el.clouds.all < minValue ) {\n        minValue = el.clouds.all\n        minIndex = index\n    }\n})\n\nmsg.minValue = minValue\nmsg.minIndex = minIndex\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":340,"wires":[["979e8a95.db4678"]]},{"id":"979e8a95.db4678","type":"debug","z":"8889c53d.0c1208","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":680,"y":340,"wires":[]}]
($lowest := $min($.payload.clouds.all);
$map(
   payload,
   function ($v,$i){
       $v.clouds.all = $lowest ? $i
   }
))

would return the index.
But would it not be best to return the whole object that has the lowest cloud.all

($lowest := $min($.payload.clouds.all);	
payload[$.clouds.all = $lowest])	

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.