# Netatmo rain information from public stations: regex or JSONata?

**URL:** <https://discourse.nodered.org/t/netatmo-rain-information-from-public-stations-regex-or-jsonata/31849>\
**Category:** General\
**Created:** [21 August 2020 17:30 UTC](https://discourse.nodered.org/t/netatmo-rain-information-from-public-stations-regex-or-jsonata/31849 "2020-08-21T17:30:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefan24](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefan24/32/23075_2.png) [@stefan24](https://discourse.nodered.org/u/stefan24)\
**Post date:** [21 August 2020 17:30 UTC](https://discourse.nodered.org/t/netatmo-rain-information-from-public-stations-regex-or-jsonata/31849/1 "2020-08-21T17:30:10Z")

</div>

I am able to read netatmo weather data collected by other netatmo stations in my region and also filter them, if they contain rain data. One of them in JSON format:

```auto
{"_id":"70:ee:50:28:00:00","place":{"location":[7.71234,48.51234],"timezone":"Europe/Paris","country":"FR","altitude":111,"city":"xxx","street":"Rue des xxxx"},"mark":12,"measures":{"02:00:00:28:ae:00":{"res":{"1598026423":[36.3,56]},"type":["temperature","humidity"]},"70:ee:50:28:a7:00":{"res":{"1598026467":[1017.3]},"type":["pressure"]},"05:00:00:04:9e:00":{"rain_60min":0,"rain_24h":0,"rain_live":0,"rain_timeutc":1598026455}},"modules":["05:00:00:04:9e:00","02:00:00:28:ae:00"],"module_types":{"05:00:00:04:9e:00":"NAModule3","02:00:00:28:ae:00":"NAModule1"}}

```

I just want to read the following data:

```auto
rain_60min
rain_24h
rain_live

```

but the measures "block" ist no array.

I have managed to use a function and generate the variable name as a string:

```auto
for(var i=0; i<msg.payload.modules.length; i++)
{
if (msg.payload.modules[i].substr(0,5) === "05:00" ) 
    {
      deb.payload = "msg.payload.measures."+msg.payload.modules[i]+".rain_60min"
    }
}

```

= `"msg.payload.measures.05:00:00:04:9e:00.rain_60min"`

but how do I use this string to read the variable? I thought of a regular expression, which changes the 50:00:xx:xx:xx into a 50, so that I can use always the same variable name

`msg.payload.measures.50.rain_60min`

But where could I use a regular expression in Node-RED?

---

<div class="post-metadata">

**Author:** ![stefan24](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefan24/32/23075_2.png) [@stefan24](https://discourse.nodered.org/u/stefan24)\
**Post date:** [21 August 2020 19:12 UTC](https://discourse.nodered.org/t/netatmo-rain-information-from-public-stations-regex-or-jsonata/31849/2 "2020-08-21T19:12:37Z")

</div>

I tried to use a JSON node, a change node, which searches for the string `05:00:??:??:??:??` and replaces it with `rain`, then another JSON node to get again Javascript objects.  
But it replaces only the `05:00` part with `rain`.

Boy, I will HAVE to learn RegEx and/or JSONata.

---

<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 August 2020 20:42 UTC](https://discourse.nodered.org/t/netatmo-rain-information-from-public-stations-regex-or-jsonata/31849/3 "2020-08-21T20:42:08Z")

</div>

Does this do it for you

```auto
for (const prop in msg.payload.measures) {
    if (msg.payload.measures.hasOwnProperty(prop)) {
        if (prop.substr(0,5) === "05:00") {
            msg.payload = msg.payload.measures[prop]
            break
        }
    }
}

```

That ends up with msg.payload containing the object `{ rain_60min: 0, rain_24h: 0, rain_live: 0, rain_timeutc: 1598026455 }`

---

<div class="post-metadata">

**Author:** ![stefan24](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefan24/32/23075_2.png) [@stefan24](https://discourse.nodered.org/u/stefan24)\
**Post date:** [21 August 2020 20:56 UTC](https://discourse.nodered.org/t/netatmo-rain-information-from-public-stations-regex-or-jsonata/31849/4 "2020-08-21T20:56:36Z")

</div>

Wow! I would never have found that solution!!

In the meantime I found the right RegEx for my string: `05:00:..:..:..:..`  
(trial and error with this online RegEx tester: [https://regex101.com/](https://regex101.com/))  
But with your function code I can omit both of the JSON nodes and the change node with the RegEx.

Thanks!!

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [22 August 2020 05:12 UTC](https://discourse.nodered.org/t/netatmo-rain-information-from-public-stations-regex-or-jsonata/31849/5 "2020-08-22T05:12:00Z")

</div>

This can be done very easily with jsonata in a change node, for example I read all stations in my region and get the average rain\_live like:

```auto
$average(**.rain_live)

```

This will only read them if they exist.

 ![Screenshot 2020-08-22 at 07.10.47](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/813aed4165d953e42001e71ed22a123f6963bbce.jpeg)

---

<div class="post-metadata">

**Author:** ![stefan24](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefan24/32/23075_2.png) [@stefan24](https://discourse.nodered.org/u/stefan24)\
**Post date:** [22 August 2020 07:29 UTC](https://discourse.nodered.org/t/netatmo-rain-information-from-public-stations-regex-or-jsonata/31849/6 "2020-08-22T07:29:11Z")

</div>

This solution is also awesome!  
I added a $max JSONata expression, which works, too!!  
In my case, I also would like to have the country+city, but ....

`(place.country&'-'&place.city&': '&**.rain_live&' (@'&**.rain_timeutc&')')`

= `"DE-Berlin: 0 (@1598026552)"`

Argh!! It took hours to get a similar result in my first post and finally gave up. 🙈

And instead of asking if there is a way to format the timeutc in a readable format just with JSONata, I used  
[https://try.jsonata.org/](https://try.jsonata.org/) and some google searches:

`(place.country&'-'&place.city&': '&**.rain_live&' ('&$fromMillis(**.rain_timeutc*1000,'[D01].[M01]. [H01]:[m01]','+0200')&')')`

= `"DE-Berlin: 0 (21.08. 18:15)"`

Thank you, both!!

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [22 August 2020 07:42 UTC](https://discourse.nodered.org/t/netatmo-rain-information-from-public-stations-regex-or-jsonata/31849/7 "2020-08-22T07:42:21Z")

</div>

There is a [netatmo node](https://flows.nodered.org/node/node-red-contrib-netatmo) (get public data) that gives the option to get all stations within a square of a given location (see [api documentation](https://dev.netatmo.com/apidocumentation/weather#getpublicdata) as well).

To format the time, jsonata (only within node-red) has the moment.js library built-in `$moment()`, you can easily format dates directly with that function.

---

<div class="post-metadata">

**Author:** ![stefan24](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefan24/32/23075_2.png) [@stefan24](https://discourse.nodered.org/u/stefan24)\
**Post date:** [22 August 2020 09:00 UTC](https://discourse.nodered.org/t/netatmo-rain-information-from-public-stations-regex-or-jsonata/31849/8 "2020-08-22T09:00:29Z")

</div>

From this netatmo node I got my data.

---

<div class="post-metadata">

**Author:** ![stefan24](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefan24/32/23075_2.png) [@stefan24](https://discourse.nodered.org/u/stefan24)\
**Post date:** [23 August 2020 18:29 UTC](https://discourse.nodered.org/t/netatmo-rain-information-from-public-stations-regex-or-jsonata/31849/9 "2020-08-23T18:29:07Z")

</div>

BTW: I just managed to calculate the distance in km from my home and the public netatmo stations.  
Add a change node and set a new msg.distance variable (or whatever) to this JSONata code:

```auto
( $lat1 := payload.place.location[1]; $lon1 := payload.place.location[0]; $lonh := 9.12345; /* Home */ $lath := 50.12345; $pi := 3.1415926535897932384626; $product := function($a, $b) { $a * $b }; $factorial := function($n) { $n = 0 ? 1 : $reduce([1..$n], $product) }; $sin := function($x){ $cos($x - $pi/2)}; $cos := function($x){ $x > $pi ? $cos($x - 2 * $pi) : $x < -$pi ? $cos($x + 2 * $pi) : $sum([0..12].($power(-1, $) * $power($x, 2*$) / $factorial(2*$))) }; $round($sqrt((111.3 * $cos(($lat1 + $lath) / 2 * 0.01745) * $abs(($lon1 - $lonh))) * (111.3 * $cos(($lat1 + $lath) / 2 * 0.01745) * $abs(($lon1 - $lonh))) + (111.3 * $abs(($lat1 - $lath))) * (111.3 * $abs(($lat1 - $lath)))),1))	

```

Just change `$lonh := 9.12345; $lath := 50.12345;` with your home longitude and latitude data  
and if you use the same netatmo data leave the variables for $lat and $lon1, otherwise put your data or variable instead of xx1 and xx2:

`$lat1 := xx1; $lon1 := xx2;`

Maybe the calculation does not work, if you have negative lat or lon values (?). I checked it with the public netatmo stations in my region and the distances seem to be valid.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [23 August 2020 19:25 UTC](https://discourse.nodered.org/t/netatmo-rain-information-from-public-stations-regex-or-jsonata/31849/10 "2020-08-23T19:25:48Z")

</div>

If you set the lat\_ne, lon\_ne,lat\_sw, lon\_sw you already know the distance ?

---

<div class="post-metadata">

**Author:** ![stefan24](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefan24/32/23075_2.png) [@stefan24](https://discourse.nodered.org/u/stefan24)\
**Post date:** [26 August 2020 19:26 UTC](https://discourse.nodered.org/t/netatmo-rain-information-from-public-stations-regex-or-jsonata/31849/11 "2020-08-26T19:26:51Z")

</div>

With these values I set a rectangle, where these public stations are in. But I do not know, how far away every single station is, whose values are returned.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 September 2020 19:26 UTC](https://discourse.nodered.org/t/netatmo-rain-information-from-public-stations-regex-or-jsonata/31849/12 "2020-09-09T19:26:52Z")

</div>

Closed on 9 September 2020 19:26 UTC

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