Help needed to code a HTTP GET inside a function

Hi all,

I'm trying to run a HTTP GET and return a parsed JSON object in the function, to use some the data generated in and in the output of the function.

Let me explain - I have a water flow meter attached to our Rachio irrigation system. Right now I read the pulses and post to a graph every few seconds, with a generic topic of "garden". What I want to do is, when watering occurs, the first time the value exceeds zero, I need to fire the http request at the Rachio API to retrieve the Zone being watered. I then want to use the Zone from the API as the Topic being posted with the value to the graph. For every subsequent value over zero, I will use the Zone number already retrieved.

So I feel that I can handle everything pretty well within the Function - or is there a better way ?

Thanks in advance

Certainly the node-red way would be to use an http node to perform the GET, not to do it inside the function.

1 Like

Yes, since this is a low-code platform with built in http request node, then use the low code / ready built nodes.

Low code flow...
chrome_YtXzWf3Wwz

Demo flow (use `CTRL+I) to import

[{"id":"4dd498cbf5b431aa","type":"inject","z":"b4c3cd7fb36660b3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"measure_value","payload":"-5","payloadType":"num","x":1190,"y":120,"wires":[["c0a797ef8383c98c"]]},{"id":"c0a797ef8383c98c","type":"switch","z":"b4c3cd7fb36660b3","name":"> 0 ?","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1370,"y":160,"wires":[["cfafdd44217d9489"],["c171a0ed072099c1"]]},{"id":"9fb80e67a4652622","type":"inject","z":"b4c3cd7fb36660b3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"measure_value","payload":"1","payloadType":"num","x":1200,"y":160,"wires":[["c0a797ef8383c98c"]]},{"id":"57a8fb62f0e49183","type":"inject","z":"b4c3cd7fb36660b3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"measure_value","payload":"4","payloadType":"num","x":1200,"y":200,"wires":[["c0a797ef8383c98c"]]},{"id":"cfafdd44217d9489","type":"change","z":"b4c3cd7fb36660b3","name":"set true","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1520,"y":140,"wires":[["976a2b44444096d6"]]},{"id":"c171a0ed072099c1","type":"change","z":"b4c3cd7fb36660b3","name":"set false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1520,"y":180,"wires":[["976a2b44444096d6"]]},{"id":"976a2b44444096d6","type":"rbe","z":"b4c3cd7fb36660b3","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":1660,"y":160,"wires":[["90ac5696be6ced86"]]},{"id":"7d0f416fa10654c4","type":"debug","z":"b4c3cd7fb36660b3","name":"debug 71","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1660,"y":280,"wires":[]},{"id":"90ac5696be6ced86","type":"switch","z":"b4c3cd7fb36660b3","name":"true ?","property":"payload","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":1330,"y":280,"wires":[["89f6852d6a17ec34"]]},{"id":"89f6852d6a17ec34","type":"http request","z":"b4c3cd7fb36660b3","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://jsonplaceholder.typicode.com/todos/","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[{"keyType":"Accept","keyValue":"","valueType":"application/json","valueValue":""}],"x":1490,"y":280,"wires":[["7d0f416fa10654c4"]]},{"id":"f312555c2a97776b","type":"comment","z":"b4c3cd7fb36660b3","name":"Only runs HTTP Req 1st time value is > 0","info":"","x":1260,"y":80,"wires":[]}]

Many thanks for that.

Since I will be using the http to just get a topic, I need to still retain the measure_value. So should this work OK ?

You could move the measurement payload to say msg.measurement and it should still be available after the http request node.

In this way @E1cid ?
image

The other way round.
set msg.measure
to msg.payload
Then in your function node the measurement will be available in msg.measure.
Presuming the measurement is in msg.payload.

1 Like

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