Skip to content

Commit 7ea3f5d

Browse files
committed
Made some additional tweaks to rain detection script
1 parent f6aed45 commit 7ea3f5d

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ $ npm install node-red-contrib-dutch-weather
2424

2525
For all nodes, you'll need to create at least one configuration. Drag one of the exposed nodes to your flow and set it up just like all other config nodes. After that, you can use the event emitters in your own code.
2626

27+
## Release 2.0.6
28+
29+
* After some time the rain-node was not giving any output anymore; the time in between working and not varies from various minutes to hours. Previously the script required both Buienradar and Meteoplaza to respond with a valid response when asking for the current rain state; this is changed so that only 1 of the systems would need to provide accurate values.
30+
2731
## Release 2.0.3
2832

2933
* `dutch-weather-sun-position` (and) `dutch-weather-solar-events` **removed**: There is a much better node-red node you can use: [node-red-contrib-sun-position](https://flows.nodered.org/node/node-red-contrib-sun-position/)

lib/WeatherLogic.js

+34-18
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,30 @@ class WeatherLogic extends EventEmitter {
9393
* @return object Combined object to return
9494
*/
9595
rainStatePayload(atTime, inMinutes, buienRadar, buienAlarm) {
96-
var isRaining, probability, state;
96+
var isRaining = ((buienRadar >= RAIN_LIGHT) || (buienAlarm >= RAIN_LIGHT));
97+
var state = (isRaining === true) ? 'raining' : 'dry';
98+
99+
// Calculate probability
100+
var probability = 0;
101+
if (isRaining === true) {
102+
if ((buienRadar !== -1) && (buienRadar >= RAIN_LIGHT)) {
103+
probability+= 50;
104+
}
97105

98-
if ((buienRadar >= RAIN_LIGHT) || (buienAlarm >= RAIN_LIGHT)) {
99-
isRaining = true;
100-
probability = (((buienRadar + buienAlarm) / 2) > RAIN_LIGHT) ? 100 : 50;
106+
if ((buienAlarm !== -1) && (buienAlarm >= RAIN_LIGHT)) {
107+
probability+= 50;
108+
}
101109
} else {
102-
isRaining = false;
103-
probability = (((buienRadar + buienAlarm) / 2) < RAIN_LIGHT) ? 100 : 50;
110+
if ((buienRadar !== -1) && (buienRadar < RAIN_LIGHT)) {
111+
probability+= 50;
112+
}
113+
114+
if ((buienAlarm !== -1) && (buienAlarm < RAIN_LIGHT)) {
115+
probability+= 50;
116+
}
104117
}
105-
state = (isRaining === true) ? 'raining' : 'dry';
106118

107-
// debug
119+
// Create message
108120
var dt = this.iso8601dateTime(atTime);
109121
var msg = (probability == 100) ? 'It ' : 'There is a ' + probability + '% chance that it ';
110122
msg+= ((inMinutes > 0) ? 'will be' : 'is') + ' ' + state;
@@ -169,6 +181,16 @@ class WeatherLogic extends EventEmitter {
169181
var atTime = new Date(after.getTime() + (inMinutes * MINUTE));
170182

171183
// Get forecasts for both providers
184+
var buienradarPrecipitation = -1;
185+
try {
186+
buienradarPrecipitation = await this.Buienradar.getForecast(atTime);
187+
buienradarPrecipitation = buienradarPrecipitation.mmh;
188+
} catch(e) {
189+
// In case this fails, we log it and use '-1' as value
190+
this.emit('rain-error', 'Failed getting prediction for Buienradar for time ' + atTime.toISOString() + ': ' + JSON.stringify(e));
191+
buienradarPrecipitation = -1;
192+
}
193+
172194
var buienalarmPrecipitation = -1;
173195
try {
174196
buienalarmPrecipitation = await this.Buienalarm.getForecast(atTime);
@@ -179,19 +201,13 @@ class WeatherLogic extends EventEmitter {
179201
buienalarmPrecipitation = -1;
180202
}
181203

182-
var buienradarPrecipitation = -1;
183-
try {
184-
buienradarPrecipitation = await this.Buienradar.getForecast(atTime);
185-
buienradarPrecipitation = buienradarPrecipitation.mmh;
186-
} catch(e) {
187-
// In case this fails, we log it and use '-1' as value
188-
this.emit('rain-error', 'Failed getting prediction for Buienradar for time ' + atTime.toISOString() + ': ' + JSON.stringify(e));
189-
buienradarPrecipitation = -1;
204+
// In case we don't have a valid result at all, don't compare values
205+
if ((buienradarPrecipitation === -1) && (buienalarmPrecipitation === -1)) {
206+
continue;
190207
}
191208

192-
var stateAtTime = this.rainStatePayload(atTime, inMinutes, buienradarPrecipitation, buienalarmPrecipitation);
193-
194209
// First loop we set the 'current' state
210+
var stateAtTime = this.rainStatePayload(atTime, inMinutes, buienradarPrecipitation, buienalarmPrecipitation);
195211
if (!tempState.hasOwnProperty('now')) {
196212
tempState.now = stateAtTime;
197213
}

0 commit comments

Comments
 (0)