Extract information from API

Hi, I'm trying to extract data from a GET request from the following site:
https://cluj-napoca.pulse.eco/rest/current

I only need the information from a specific sensorId.
For example I need the info from: 522ad249-96b8-47b7-a63e-8dba638cef6e and the output payload to display PM10, PM25, Humidity, Temperature, Pressure, etc.

Welcome to the forum
That looks like JSON data, feed it into a JSON node and it should convert it to a javascript object so you can access the elements you want

A simple example of how to do http request return a json object and filter for sensorId.

[{"id":"55f374bb.7d3eb4","type":"inject","z":"7760f563.ea0324","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":2080,"wires":[["318c812a.d34f46"]]},{"id":"318c812a.d34f46","type":"http request","z":"7760f563.ea0324","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://cluj-napoca.pulse.eco/rest/current","tls":"","persist":false,"proxy":"","authType":"","x":280,"y":2080,"wires":[["b77ed1a0.fe285"]]},{"id":"b77ed1a0.fe285","type":"change","z":"7760f563.ea0324","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$filter(payload, function($v){$v.sensorId =\"522ad249-96b8-47b7-a63e-8dba638cef6e\"})","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":2060,"wires":[["e1e90f78.6aa54"]]},{"id":"e1e90f78.6aa54","type":"debug","z":"7760f563.ea0324","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":2060,"wires":[]}]

It looks good, but I need to join all the filtered information into a single message. What I want to do, is to get info for pm25, pm10 into an message like: pm25: "value", pm10: "value"

look at the template node - that is designed to help you do that.

Or do it in the change node, template will be more readable.

[{"id":"6a64d05.3bc4a3","type":"change","z":"7760f563.ea0324","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$merge(\t   $map(\t       $filter(\t           payload,\t           function($v){\t               $v.sensorId =\"522ad249-96b8-47b7-a63e-8dba638cef6e\"\t   }\t)\t,\t       function($v){{$v.type: $v.value}}\t   )\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":1000,"wires":[["2eab5727.a113f"]]}]

Here is a very similar flow using a Function node

[{"id":"efaea6e1.c7a46","type":"inject","z":"d345a010.8f36d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":180,"wires":[["72bef589.4425dc"]]},{"id":"72bef589.4425dc","type":"http request","z":"d345a010.8f36d8","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://cluj-napoca.pulse.eco/rest/current","tls":"","persist":false,"proxy":"","authType":"","x":490,"y":180,"wires":[["c07a7573.a818c8"]]},{"id":"c07a7573.a818c8","type":"function","z":"d345a010.8f36d8","name":"","func":"let arr = msg.payload\nlet pm25 = arr.find(el => el.sensorId == '522ad249-96b8-47b7-a63e-8dba638cef6e' && el.type == 'pm25')\nlet pm10 = arr.find(el => el.sensorId == '522ad249-96b8-47b7-a63e-8dba638cef6e' && el.type == 'pm10')\n\nlet newMsg = {\n    \"payload\": arr,\n    \"pm25\" : pm25.value,\n    \"pm10\" : pm10.value\n};\n\nreturn newMsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":680,"y":180,"wires":[["4f633983.ecbeb"]]},{"id":"4f633983.ecbeb","type":"debug","z":"d345a010.8f36d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":180,"wires":[]}]

ps. i noticed that your sensorId 522ad249-96b8-47b7-a63e-8dba638cef6e doesnt return
Humidity, Temperature, Pressure ? Are those stored under a different device id ?

ps2. Does your API always return one PM25 and PM10 respectively with that id ?

522ad249-96b8-47b7-a63e-8dba638cef6e was an example, also this API address returns the current readings from the sensors

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