Extracting info from API

Hi i need to extract some data from an API here http://data.channelcoast.org/observations/waves/latest?key=6cefd36d8e12a4dead4cf06d4dbd09c0 using a HTTP request i need to get the data of the latest wave height (ms:hs) for Porthleven - i then want to control an RGB LED thats lights blue if height is below 2m, green if its between 3 and 4 and red if its over 4 - i can probably manage the LED bit with some functions but i cant seem to access the path for that value...can anyone help me?

1 Like

@surfershort - is that your personal API key posted above?
If so, it's generally not a good idea to publicly reveal it.

2 Likes

it looks like it is comma separated

so i would start by using a http request node to get the data into Node-RED

Then try a csv node to get it into a javascript object.

Have you read the working with messages page in the docs?

1 Like

When you output the http node to an xml node and feed it into a function node:

g = msg.payload["wfs:FeatureCollection"]["gml:featureMember"]
var results = [];

var toSearch = "Porthleven";

for(var i=0; i<g.length; i++) {
  for(let key in g[i]) {
    if(g[i]['ms:waves'][0]['ms:sensor'][0].indexOf(toSearch)!=-1) {
      results.push(g[i]);
    }
  }
}
return {payload:{results:results,sensor:results[0]["ms:waves"][0]["ms:sensor"][0],hs:results[0]["ms:waves"][0]["ms:hs"][0]}}

Output:

Try this flow:

[{"id":"eaae278b.53071","type":"inject","z":"24c36d28.96ea9a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":216,"y":192,"wires":[["c2315666.b13f3"]]},{"id":"31f9a814.eaa6","type":"function","z":"24c36d28.96ea9a","name":"","func":"g = msg.payload[\"wfs:FeatureCollection\"][\"gml:featureMember\"]\n\nvar results = [];\n\nvar toSearch = \"Porthleven\";\n\nfor(var i=0; i<g.length; i++) {\n  for(let key in g[i]) {\n    if(g[i]['ms:waves'][0]['ms:sensor'][0].indexOf(toSearch)!=-1) {\n      results.push(g[i]);\n    }\n  }\n}\nreturn {payload:{results:results,sensor:results[0][\"ms:waves\"][0][\"ms:sensor\"][0],hs:parseFloat(results[0][\"ms:waves\"][0][\"ms:hs\"][0])}}\n\n//payload[\"wfs:FeatureCollection\"][\"gml:featureMember\"][0][\"ms:waves\"][0][\"ms:sensor\"][0]","outputs":1,"noerr":0,"x":674,"y":192,"wires":[["5c39eba7.a567ac"]]},{"id":"c2315666.b13f3","type":"http request","z":"24c36d28.96ea9a","name":"","method":"GET","ret":"txt","paytoqs":false,"url":"http://data.channelcoast.org/observations/waves/latest?key=6cefd36d8e12a4dead4cf06d4dbd09c0","tls":"","persist":false,"proxy":"","authType":"","x":382,"y":192,"wires":[["9aa5a06.3bc9de"]]},{"id":"9aa5a06.3bc9de","type":"xml","z":"24c36d28.96ea9a","name":"","property":"payload","attr":"","chr":"","x":530,"y":192,"wires":[["31f9a814.eaa6"]]},{"id":"5c39eba7.a567ac","type":"debug","z":"24c36d28.96ea9a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":838,"y":192,"wires":[]}]
1 Like

[quote="bakman2, post:4, topic:16884"]

[{"id":"eaae278b.53071","type":"inject","z":"24c36d28.96ea9a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":216,"y":192,"wires":[["c2315666.b13f3"]]},{"id":"31f9a814.eaa6","type":"function","z":"24c36d28.96ea9a","name":"","func":"g = msg.payload[\"wfs:FeatureCollection\"][\"gml:featureMember\"]\n\nvar results = [];\n\nvar toSearch = \"Porthleven\";\n\nfor(var i=0; i<g.length; i++) {\n  for(let key in g[i]) {\n    if(g[i]['ms:waves'][0]['ms:sensor'][0].indexOf(toSearch)!=-1) {\n      results.push(g[i]);\n    }\n  }\n}\nreturn {payload:{results:results,sensor:results[0][\"ms:waves\"][0][\"ms:sensor\"][0],hs:parseFloat(results[0][\"ms:waves\"][0][\"ms:hs\"][0])}}\n\n//payload[\"wfs:FeatureCollection\"][\"gml:featureMember\"][0][\"ms:waves\"][0][\"ms:sensor\"][0]","outputs":1,"noerr":0,"x":674,"y":192,"wires":[["5c39eba7.a567ac"]]},{"id":"c2315666.b13f3","type":"http request","z":"24c36d28.96ea9a","name":"","method":"GET","ret":"txt","paytoqs":false,"url":"http://data.channelcoast.org/observations/waves/latest?key=6cefd36d8e12a4dead4cf06d4dbd09c0","tls":"","persist":false,"proxy":"","authType":"","x":382,"y":192,"wires":[["9aa5a06.3bc9de"]]},{"id":"9aa5a06.3bc9de","type":"xml","z":"24c36d28.96ea9a","name":"","property":"payload","attr":"","chr":"","x":530,"y":192,"wires":[["31f9a814.eaa6"]]},{"id":"5c39eba7.a567ac","type":"debug","z":"24c36d28.96ea9a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":838,"y":192,"wires":[]}]

[/quote]Hi Paul sorry its just the demo one from here https://www.channelcoast.org/ccoresources/api/

Thanks

Amazing! Thanks man! Perfect :slightly_smiling_face:

nice, could you mark it solved under my "solution" ?

Is it me, or has the option to mark a post as the solution disappeared??

Edit - New post created 'Mark a post as solution' missing

1 Like

You only see it for your own topics i think ?

1 Like