So, last week out of nowhere my standard Wunderground integration stopped working. I tried to figure out what was going on, but it would take me too long, I re-added the service, but all to no avail. So I decided I was probably better off just writing a Python Script to do it myself. For the following code to work I am working without authentication from local calls to the Domoticz device, you can add these in your settings:
For the first bit to get this to work, just follow the instructions on the Wiki to get your API key. Then you need to add virtual devices to your setup. First you create a Dummy Hardware device and then you add virtual sensors. I added the following (click for large):
Also to learn a bit more of python I created a domoticz.py file with a couple of simple functions: updateDevice, readDevice and getSunRiseSet. The first two are obvious and the last will become clear why later.
Lets start by just reading the data. You need the station you want. Now this can be tricky or simple. I would suggest to go to the documentation and find the data/geolookup API calls, you can find those here. If you use the IP based call and just paste the URL in your browser, you will get a response with a list of stations in there too. What you’re looking for is the ID of the station you like. You can also use any other of the API calls, as long as you find the ID of the station you prefer (maybe a big organisation’s instead of the hobbyist down the street). For me I went with a station that is in the next town, but supplies a great source of data. Add ‘pws:’ to the ID in the code to get the right call.
import urllib, json
import domoticz, datetime
station = 'pws:IDEBILT9'
wundergroundkey = 'XXXXXXXX' # YOUR API KEY HERE
kmhtoms = 0.277777777778
url = 'http://api.wunderground.com/api/'+wundergroundkey+'/conditions/q/'+station+'.json'
response = urllib.urlopen(url)
data = json.loads(response.read())
# sometimes the station sends odd values. When all these conditions are met, the station is wrong and this script should quit.
# usually this would otherwise result in UV scores at night, and temp spikes or drops, but all the data seems off during this.
if float(data['current_observation']['UV']) is 6.0 and float(data['current_observation']['temp_c']) is 11.4:
print 'Data seems off, not processing.'
The first bits are easy, then a constant to convert information, then I grab the json data and the last bit is a bit of filtering out some spikes I noticed.
All pretty darn easy. The next bit is a bit of data cutting and pasting, but using my domoticz.py all relatively easy.
#set the variables for all wind data
WB = str(data['current_observation']['wind_degrees']) #bearing
WD = str(data['current_observation']['wind_dir']) #direction
WS = str(data['current_observation']['wind_kph']*kmhtoms) #speed
WG = str(float(data['current_observation']['wind_gust_kph'])*kmhtoms) #gusts
WT = str(data['current_observation']['temp_c']) #wind temp
WC = str(data['current_observation']['feelslike_c']) #chill
#update UV, no values at night..
sun = domoticz.getSunRiseSet()
now = datetime.datetime.now()
sunset = now.replace(hour=int(sun['Sunset'][:2]), minute=int(sun['Sunset'][3:5]), second=0, microsecond=0)
sunrise = now.replace(hour=int(sun['Sunrise'][:2]), minute=int(sun['Sunrise'][3:5]), second=0, microsecond=0)
if now > sunset or now < sunrise :
print 'It\'s nighttime, overruling any UV values.'
data['current_observation']['UV'] = 0
domoticz.updateDevice(212,0,str(data['current_observation']['UV'])+';0')#the 0 at the end is required!
#update Rain, make sure it's not accidentally negative
precip1 = max(float(0), float(data['current_observation']['precip_1hr_metric']));
preciptoday = max(float(0), float(data['current_observation']['precip_today_metric']));
# pressure ignore impossible results.
baro = int(data['current_observation']['pressure_mb'])
if baro > 940 and baro < 1080 :
print 'Baro Value seems not OK:'+str(baro)
Just read through it and change the ID of your virtual devices to the corresponding IDs in your device overview. I added comment on why and what we’re updating so this should be straight forward, you can download both the files here: Python Wunderground Domoticz
Just make sure you change the IP to whatever the IP is of your Domoticz server or localhost if you run it on the same device.
The getSunRiseSet is used for the UV index to filter out spikes during the night. Unless we have some astrological wonder, I don’t think UV should be above 0 during the night.
The last bit to get this to work is to setup a cron job. Make sure you don’t call the cron too often, you only have 500 calls a day. To my math I worked out I can call about once every three minutes to be on the safe side.
If you have any comments or suggested improvements, let me know!