Wunderground weather in Domoticz with Python

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 for Wunderground. 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):

The virtual devices I created for the Weather service.

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.

Let’s 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.

Now for some python code:

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.

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!

 

Leave a Reply

Your email address will not be published. Required fields are marked *