Check for rain coming [x] hours and alert in node red, please help me handle the data

I’m new to HA and node red, and have very little experience in programming. For some godforsaken reason I decided that for my first real project I wanted to get an alert when a window is opened and rain is expected in the coming 12 hours.

I didn’t find a template for this, so I decided to try creating this in one red. After a lot of trial and error I have gotten to the point where I make an API request and end up with a bunch of data in an array.

Now I just need to handle the array, but I’m not sure how exactly to do this.

My flow looks like this:

[{"id":"ae7ea8bfe37d7db7","type":"http request","z":"e28683cf.db011","name":"Api request","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/18.067/lat/59.327/data.json","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":250,"y":180,"wires":[["99e9c87c.453f18"]]}]

(The coordinates in this flow just point to Stockholm, for sharing purposes)

The output data looks like this:

And this is the documentation for the API for SMHI (swedish weather institute):
https://opendata.smhi.se/apidocs/metfcst/parameters.html

So I´m able to successfully make an API request and get an object/array with values in it. The structure seems to be that timeseries[0] is the first hour, and under timeseries.parameters we have the data I want - pcat. This value seems to change from 0 (no rain) to 6 ( heavy rain) which is how I’ll evaluate if rain is expected.

At this stage, I want to do something like this:

> Get test.timeseries[0-11].parameters[1]     # 12 hours worth of data
> Loop through timeseries 0-11 and check timeseries[0-11].parameters[1].value     # check the mcat parameter
   > If all parameters [1] == 0 then    # No rain in the coming 12 hours, do nothing.
 > Else ## Rain, proceed with the flow. 

This is based on my understanding of the documentation that when rain is expected in any way pcat will change to value 1-6 depending on category.

I have no idea how to do this in practice, and hoping someone could help me finish this (to the point described above where I can continue the flow if rain is detected, I can keep experimenting with notifications if I can just get to that point).

Also, there are probably a lot of nonsense in my current NR flow, I won’t be able to answer why I did certain things because this was just a lot of trial and error from a year ago.

The split and join nodes are typically what you would use when dealing with arrays.

import https://pastebin.com/raw/Z9hesXfM

Thank you!

This was extremely helpful. I’ll look more into some tutorials on it later to help me understand why, for example, you first split the timeseries and then the parameters.

For anything else reading this and trying something similiar:
I made a small change to “keep only if pcat > x” to check the

payload.timeSeries.parameters.values[0]

instead of

payload.timeSeries.parameters.level

to make it check the correct variable. “Values” is a array in every parameter holding the correct value (1-6) but every values array only holds one number, the value itself. I wasn’t sure if I should type “values[0]” or simply “values” but atm both seem to work.

I’ll paste the current flow here, it’s not complete but it might also help someone else later.

https://pastebin.com/tk0BxaT9

Edit: Just when I thought I’d started to understand the flows, I tried creating a switch at the end to carry on the flow if a value is detected, I also tried creating an “else” condition if rain is not detected but for some reason that “else” condition never triggers the debug node. If anyone knows why I’d be happy to hear it.

Edit2: Found the answer, the else condition doesn’t trigger because if there is no rain the flow is stopped at the “keep only if pcat > X” node. If you want do create a flow when no rain is expected then add and else to that node instead.