Stuck with how to parse weather information in `function` node

Folks, I am a bit stuck with how to get something done with a message.

I am using the openweather node to get the weather and detect if it is RAINING NOW and how much.

The information is in msg.payload.rain and there is three modes of operation.
A, B and C (and Q, which I'll get to later)
B is easy. It gets the weather at that time and nothing else.
A and C are average weather readings from either mid-night or sunrise.

And that's where the problem starts.

Let's go with the mid-night one.
There will be a few messages averaged to get the final value.

I added the two extra options as it may be raining from mid-night (or dawn) but stop by mid-day and there is no rain. So with 'no rain', the plants get watered, which is kind of silly as they have already received enough water for that day.

So there is a threshold where there is enough rain fallen, and getting that value - as an average is driving me mad.

Example:
The cut off is 0.3mm rain.
Rain detected at 01:00 = 0.1mm
Rain detected at 04:00 = 0.1mm
Rain detected at 09:00 = 0.1mm
All other readings were 0

So the plants don't need watering.

I understand I only count non-zero values.
But the average is where I'm falling over.

Maybe I should be using TOTAL rather than average. I get that.
But I'm just wanting to ask if there is an elephant I'm not seeing.

Thanks.

OH!

The Q mode.

So I have it set to mode B.

But it is raining and looking like it s going to clear before mid-day.
I press a button and it gets the rainfall NOW and if it is greater than the 0.3mm, it is marked as to be skipped.
(a value of > 0.3 is then sent when the value is requested.)

Clear?

Which API does the openweathermap node access?

I don't use that node, instead I use an http request node to access v3 "onecall" which includes a total forecast mm of rain for today as msg.payload.daily[0].rain.

1 Like

I know this is not looking good for me, but I don't understand:

On your mentioning the forecast.... Yeah. I'm wanting the actual amount of rain DETECTED falling. Not the forecast. That can be too problematic in itself.

I am working now on just using a running total.
But it would seem I have taken a lot of stupid pills just recently and I am making a dog's breakfast of even doing that.

Perhaps Openweathermap knows the actual amount of rain that was reported at the nearest weather station but it certainly doesn't know how much has fallen at your location.
I suspect you either need a personal weather station of your own, with a rain measurement, or else a soil moisture sensor, or both.

Openweathermap has at least two APIs. The more recent one is called "Onecall API 3". You have to give a credit card to get an API key, but there is a generous free tier. There is also an older one which returns different data and needs a different API key.

Oh, ok.

Yes, I have an API Key for it.

But I am using it to get the weather NOW.
I (maybe) should look if they have a backward looking option too.

That way I can call it once (at mid day) and be told how much rain has fallen today.
Though it would be problematic with the sunrise option. As it may have bucketed down from midnight but stopped at sunrise.....

I think I'd better not waste your (and/or other people's) time with this.
Sorry.
I know I've kind of been down this path before recently and kinda failed with that one too.

How is 2026 looking so far?

I was asleep when it started.
Kinda wet and rainy from the reports though.

I'm getting 0.12mm rain falling every hour since midnight from what I'm seeing in the flow.

This is the code which I hope works.

I have done some initial testing and it seems to be doing the right thing/s.

Though time will tel..

//  Get `mode` to determine how the rain is parsed.
//
//  A -> From midnight
//  B -> Normal
//  C -> From sunrise

//  Handle reset message.
let total_rain = context.get("total_rain") || 0
if (msg.reset == "reset")
{
    context.set("total_rain",0)
    node.status({})
    msg.payload = {}
    msg.payload.rain = 0        //      2026 01 01 added to wipe anything in the queue.
    return msg
}

const mode = flow.get("mode") || "B"
//node.warn("Mode is " + mode)

let rain = msg.payload.rain

if (mode == "B")
{
    node.status({text:mode})
    return msg  //  nothing to do in this mode.
}

total_rain = total_rain + rain
context.set("total_rain",total_rain)

node.warn("Rain now is " + rain)
node.warn("Total rain so far is " + total_rain)

node.status({text:mode + ' ' + total_rain})

msg.payload.rain = total_rain
//msg.rain_now = total_rain     //  This isn't used (2026 01 01)

return msg