# Calculate rainfall per 24h

**URL:** https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167
**Category:** General
**Created:** [21 October 2018 11:17 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167 "2018-10-21T11:17:35Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@Florian](https://discourse.nodered.org/u/Florian)
#### Post date: [21 October 2018 11:17 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/1 "2018-10-21T11:17:35Z")

</div>

(english below)

Hallo,

ich empfange von einer Wetterstation das Payload (payload.rainfall.total.value) für die aktuelle Regenmenge. Leider besteht keine Möglichkeit, die Regenmenge für einen Tag (24h) zu erfassen und dann an das Dashboard zu übergeben.

Hat jemand einen Idee, wie so ein Flow aussehen müsste? Leider habe ich keine Programmierkentnisse .

Vielen Dank!

PS: bin schon älter … bitte verständlich erklären 🙂

Hello,

I receive from a weather station the payload (payload.rainfall.total.value) for the current rainfall. Unfortunately it is not possible to record the amount of rain for one day (24h) and then transfer it to the dashboard.

Does anyone have an idea how such a flow should look like? Unfortunately I don't have any programming knowledge .

Thanks a lot!

PS: I am older ... please explain 🙂

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 October 2018 12:34 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/2 "2018-10-21T12:34:40Z")

</div>

Do you need the rainfall historically or just for the current day?

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [21 October 2018 12:57 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/3 "2018-10-21T12:57:23Z")

</div>

To do as a one-off, all you need is to accumulate each incoming value to a global/flow variable then use that in your dashboard.

Of course, that only really works if you want to reset the value at a given point in time (e.g. midnight), you will need a separate flow for that.

If you want a running 24hr total, you will have to keep all of the values that occured in the last 24hrs and recalculate the total on each new entry after dropping the oldest. There are array helpers in JavaScript that will help. I think you can do it with JSONata too.

If, however, you want historic data, you should look at using InfluxDB which is a timeseries database. With that, you can dump your new values into the database and it will automatically calculate the running totals for you.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 October 2018 13:01 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/4 "2018-10-21T13:01:41Z")

</div>

> [@Florian](#):
>
> I receive from a weather station the payload (payload.rainfall.total.value) for the current rainfall.

Also can you clarify what that means as it apparently means different things to @TotallyInformation and myself. Does it mean the total rain that has fallen since the weather station was first switched on?

---

<div class="post-metadata">

### Author: ![Florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@Florian](https://discourse.nodered.org/u/Florian)
#### Post date: [21 October 2018 13:39 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/5 "2018-10-21T13:39:26Z")

</div>

I'd be happy for the current day, Colin.

---

<div class="post-metadata">

### Author: ![Florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@Florian](https://discourse.nodered.org/u/Florian)
#### Post date: [21 October 2018 13:41 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/6 "2018-10-21T13:41:04Z")

</div>

No. The weather station only reports the current amount of rain. Starting with the insertion of the battery with Zero. Starts at zero and adds each rain to it. For example the value in the payload is 740 and if it rains 2 litres then it is 742. So I always have only the total value of the rain, but I want to know how much it has rained in the last 24 hours.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 October 2018 13:54 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/7 "2018-10-21T13:54:41Z")

</div>

In the last 24 hours or since midnight?

---

<div class="post-metadata">

### Author: ![Florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@Florian](https://discourse.nodered.org/u/Florian)
#### Post date: [21 October 2018 13:55 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/8 "2018-10-21T13:55:59Z")

</div>

Yes, Colin

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [21 October 2018 14:09 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/9 "2018-10-21T14:09:54Z")

</div>

So you will need to store the value. You can store values using context variables see:

[https://nodered.org/docs/writing-functions#storing-data](https://nodered.org/docs/writing-functions#storing-data)

Try importing this flow and see how you go

```auto
[{"id":"9a549ce5.8953","type":"inject","z":"9726fcf.8cbf3","name":"Start of the day","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"x":170,"y":400,"wires":[["4cdf847.94f0afc"]]},{"id":"4cdf847.94f0afc","type":"change","z":"9726fcf.8cbf3","name":"Get last days rain, and reset dailyrain","rules":[{"t":"set","p":"payload","pt":"msg","to":"dailyrain","tot":"global"},{"t":"set","p":"dailyrain","pt":"global","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":400,"wires":[["1905712c.98e9f7"]]},{"id":"96447045.2e2438","type":"debug","z":"9726fcf.8cbf3","name":"Last Days Rain","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":900,"y":380,"wires":[]},{"id":"6e94878e.cf0e38","type":"debug","z":"9726fcf.8cbf3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":890,"y":200,"wires":[]},{"id":"ccb4cf8b.5dd2c","type":"inject","z":"9726fcf.8cbf3","name":"","topic":"","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":240,"wires":[["4cf1d5d4.d4a894"]]},{"id":"bc23345e.cd6118","type":"function","z":"9726fcf.8cbf3","name":"","func":"\n//grab previous value from global variable for total rain\npreviousrainfall = global.get(\"totalrainfall\")||0;\n\n//how much rain since the last update\nperiodrainfall = msg.payload - previousrainfall;\n\n//grab previous value from global variable for daily rain\ndailyrainfall = global.get(\"dailyrain\")||0;\n\ndailyrainfall = dailyrainfall + periodrainfall;\n\n//update the global variables.\nglobal.set(\"dailyrain\",dailyrainfall );\nglobal.set(\"totalrainfall\", msg.payload);\n\nmsg.payload = \" Total Rain =\"+msg.payload+\" Period Rain\"+periodrainfall+\" Rain Today\"+dailyrainfall\n\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":240,"wires":[["6e94878e.cf0e38"]]},{"id":"4cf1d5d4.d4a894","type":"function","z":"9726fcf.8cbf3","name":"total rainfall input","func":"\n//mimicing teh total rainfall input\n\npreviousrainfall = global.get(\"previousrainfall\")||0;\nmsg.payload = msg.payload + previousrainfall;\nglobal.set(\"previousrainfall\", msg.payload)\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":240,"wires":[["bc23345e.cd6118","5721e159.00dc4"]]},{"id":"5721e159.00dc4","type":"debug","z":"9726fcf.8cbf3","name":"\"Total Rainfall input\"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":500,"y":280,"wires":[]},{"id":"a98eaf45.542de","type":"comment","z":"9726fcf.8cbf3","name":"Click input to simulate rain guage total rainfall input","info":"","x":230,"y":180,"wires":[]},{"id":"5d4f1eaa.04b2b8","type":"comment","z":"9726fcf.8cbf3","name":"Click to simulate start of day calculation","info":"","x":210,"y":360,"wires":[]},{"id":"1905712c.98e9f7","type":"function","z":"9726fcf.8cbf3","name":"msg.payload = \"Total rain today is \"+msg.payload","func":"msg.payload = \"Total rain today is \"+msg.payload\nreturn msg;","outputs":1,"noerr":0,"x":900,"y":480,"wires":[["96447045.2e2438"]]}]

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 October 2018 14:54 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/10 "2018-10-21T14:54:05Z")

</div>

> [@Florian](#):
>
> Yes, Colin

Which, last 24 hours or since midnight?

---

<div class="post-metadata">

### Author: ![Florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@Florian](https://discourse.nodered.org/u/Florian)
#### Post date: [21 October 2018 16:49 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/11 "2018-10-21T16:49:00Z")

</div>

Thank you!

With this flow I receive the values of the rain meter:

[{"id":"98c56e21.f629a","type":"rfx-sensor","z":"facd4112.fe1c28","name":"","port":"61217f11.fab2d","topicSource":"single","topic":"RAIN3/0x800C","x":393.0173797607422,"y":238.01042366027832,"wires":[["39b7b0ce.8015b"]]},{"id":"39b7b0ce.8015b","type":"change","z":"facd4112.fe1c28","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.rainfall.total.value","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":636.0173530578613,"y":236.01039028167725,"wires":[["e1441486.f59e78","24e737f9.491348","cfb8bd18.694d"]]},{"id":"61217f11.fab2d","type":"rfxtrx-port","port":"/dev/ttyUSB0"}]

How would I have to integrate that into your flow, that every day at 0.01 o'clock the starting value is taken over and then with rain the value is calculated each time? "("Total Rain is current :"). So start with a value at 0 o'clock and then update again and again with rain until 23:59 o'clock ...  
Complicated, I know 😉

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [21 October 2018 17:53 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/12 "2018-10-21T17:53:39Z")

</div>

Take a look at the inject and see what it is injecting and then look at the "total rainfall input" function node.  
The first two lines mimic a message that is always increasing ( eg your total rainfall)

The best way to learn is for you to have a go and see how far you get.

Theres a great guide for how to work with messages here: [https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

---

<div class="post-metadata">

### Author: ![Florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@Florian](https://discourse.nodered.org/u/Florian)
#### Post date: [21 October 2018 18:54 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/13 "2018-10-21T18:54:08Z")

</div>

The learning process is not so easy because English is not my native language and I always have to translate everything with difficulty.  
I thank you for your help so far!

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [21 October 2018 20:32 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/14 "2018-10-21T20:32:55Z")

</div>

I'm just experiencing the same problem, as I'm moving my flow from Wunderground to Darksky.

When pulling forecast data for the following 7 days, there is nothing in the Darksky api to indicate the accumulated rainfall over the 24hr period.  
I thought I'd found the answer in `precipAccumulation` but apparently it relates to snow accumulations.

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [22 October 2018 01:20 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/15 "2018-10-22T01:20:25Z")

</div>

Florian,

With a problem like this it is easiest to break it down into steps and write it out in Pseudo code, then you can code/design each step as you go

1. Every minute check time
2. If time = 00:01 - reset global variable - RainValueatStartofDay, get value from Weatherstation for TotalRainfall, store in Global Vairable RainValueatStartofDay
3. Every other minute - talk to the weather station and get the total rainfall
4. If TotalRainfall \> RainValueatStartofDay, then Subtract RainValueatStartofDay from TotalRainfall present result to user - we have had X amount of rain so far today
5. At 00:00 Do final calculation for day and Store in file - We have had a total of X amount of Rain today.

Then loop as above

Craig

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [22 October 2018 01:21 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/16 "2018-10-22T01:21:20Z")

</div>

Obviously once you understand this - you can look at Nodes that are available to do the heavy lifiting for you

The inject node will let you make something happen at a specific time each day, another inject node can repeat and action every x minutes

Then either a function node or a change node would do most of the rest for you

Craig

---

<div class="post-metadata">

### Author: ![Florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@Florian](https://discourse.nodered.org/u/Florian)
#### Post date: [22 October 2018 05:32 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/17 "2018-10-22T05:32:15Z")

</div>

Hello all helpers.

thank you for your help, but if you can't program at all and the english knowledge is not available due to age, it gets too complicated.

Thank you very much for your help and all the best!

---

<div class="post-metadata">

### Author: ![Florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@Florian](https://discourse.nodered.org/u/Florian)
#### Post date: [2 November 2018 07:58 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/18 "2018-11-02T07:58:15Z")

</div>

hello ukmoose,

your code works great! The amount of rain is recognized correctly for the last 24h. Super work from you!

In the debug window it looks like this:

2.11.2018, 08:46:38node: fdddfc10.68cde8  
RAIN3/0x800C : msg.payload : string[43]  
" Total Rain =772.1 Period Rain7 Rain Today7"

Now it would be great if I could get my own "gauge" for each of the values "Total Rain", "Period Rain" and "Rain Today" via the dashboard. How can I separate and output the individual values?

Thanks for your help! I start to understand it and now it's fun again! Despite the age 🙂

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [2 November 2018 09:00 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/19 "2018-11-02T09:00:56Z")

</div>

The "writing functions" page in the docs [https://nodered.org/docs/writing-functions](https://nodered.org/docs/writing-functions), explains how to have multiple outputs from the function node. So you could send each different value to a different output and on to a dashboard graph node.

---

<div class="post-metadata">

### Author: ![Florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@Florian](https://discourse.nodered.org/u/Florian)
#### Post date: [2 November 2018 09:16 UTC](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167/20 "2018-11-02T09:16:56Z")

</div>

Thank you!

Looks very complicated the function "writing-functions"!

Can you give me an example for a value? With this I could follow the steps.

That would be very nice!

[Next page](https://discourse.nodered.org/t/calculate-rainfall-per-24h/4167.md?page=2)
