Skip to content

Commit

Permalink
Temporary fix for Wunderground invalid forecast data
Browse files Browse the repository at this point in the history
  • Loading branch information
Moonbase59 committed Jun 17, 2018
1 parent f405a67 commit f0ab780
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/install-raspberry-pi.md
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ StudioDisplay normally shows colors/backgrounds/daylight phases/times and the we
Let’s assume we found a New York weather station on Wunderground’s [Wundermap](https://www.wunderground.com/wundermap?lat=40.75&lon=-74&zoom=8&pin=&rad=1&rad.type=00Q&wxsn=0&svr=0&cams=0&sat=0&riv=0&mm=0&hur=0&apiref=b27828e10245d1a1) and it’s ID is `KNYNEWYO899` (Manhattan West):

```bash
mosquitto_pub -h studiodisplay1 -t weather/1/set/pws -m KNYNEWYO899
mosquitto_pub -h studiodisplay1 -t weather/1/set/pws -m KNYNEWYO899
```

![New York weather](images/new-york-weather.png)
Expand Down
47 changes: 31 additions & 16 deletions python/mqtt-weather-wunderground.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ def on_message(mosq, obj, msg):
config.set(my_section, configitem, msg.payload)
# update weather directly, don’t wait for next update
wunderground_get_weather()
wunderground_get_astronomy()
# skip astronomy for now
# wunderground_get_astronomy()
wunderground_get_forecast()
else:
logger.info("Ignoring unknown configuration item " + configitem)
Expand Down Expand Up @@ -536,18 +537,35 @@ def wunderground_get_forecast():
return
logger.info("Getting Weather Underground data from " + wu_url)

try:
response = urllib2.urlopen(wu_url)
except urllib2.URLError as e:
logger.error('URLError: ' + str(wu_url) + ': ' + str(e.reason))
return None
except Exception:
import traceback
logger.error('Exception: ' + traceback.format_exc())
return None

parsed_json = json.load(response)
response.close()
# As of June, 2018, Wunderground seem extremely unreliable returning the forecast.
# Often the value of json['forecast']['txt_forecast']['date'] is an empty string
# and all values junk, including the simple forecast "epoch" dates
# which lie back sveral months in the past!
#
# Currently, the only way to prevent bad forecasts seems to be checking for
# an empty date and retry the call – using up calls per minute and day :-(

# FIXME: This might get us into en endless loop and eat up our allowed hits/day!

forecast_date = ""

while not forecast_date:
try:
response = urllib2.urlopen(wu_url)
except urllib2.URLError as e:
logger.error('URLError: ' + str(wu_url) + ': ' + str(e.reason))
return None
except Exception:
import traceback
logger.error('Exception: ' + traceback.format_exc())
return None

parsed_json = json.load(response)
response.close()
forecast_date = str(parsed_json['forecast']['txt_forecast']['date'])
if not forecast_date:
logger.info("Got invalid forecast data, retrying in 10s …")
time.sleep(10) # TODO: make this configurable

day = {}
for d in range (0, 4): # 0-3
Expand Down Expand Up @@ -617,8 +635,5 @@ def wunderground_get_forecast():
# skip astronomy, it’s unreliable
# we now do it in mqtt-astronomy, or using SunCalc in index.html
# wunderground_get_astronomy()
# we are allowd 10 requests/minute using the free developer account, so pause
time.sleep(6)
wunderground_get_forecast()
time.sleep(config.getfloat(my_section, 'updaterate'))
pass

0 comments on commit f0ab780

Please sign in to comment.