Python Program Pulling Values

I’ve been using my weatherflow and a few other of my smart home devices to learn something about python. So, please be kind if you find my code inefficient. Still, I thought some might find some interest in how I grab the weatherflow data and parse out the REST api results. I opted to use REST instead of websockets because I don’t think an always-connected flow of data.

This program pulls data from my charge controller hooked up to a solar array. Every minute, it polls for data from the charge controller. Then, it grabs the weatherflow data (solar radiation, uv index and brightness). I log all these values to a MariaDB database.

The polling program runs on a Raspberry Pi 4. The MariaDB is located on a Zorin Linux laptop.

My code will show a simple way to parse out the json without getting overly complex.

Any suggestions for improving what I have here are welcome!

3 Likes

Hi @o2bnMaine, thanks for posting your code. It is always helpful to see how others have approached the task of parsing data from the REST API.

I’ve had a quick look at your code and have a suggestion to make the parsing even easier. The response from the WF API is actually serialized JSON content. The details of what this means doesn’t matter, but importantly you can use the JSON decoder in the Requests module to convert the response from the API into a Python dictionary. The you can interrogate the API response using the field names directly.

So once you have got the API response using web_output = requests.get(url), convert it into a Python dictionary using web_output = web_output.json(). You can then extract the latest list of observations using something like Obs = web_output['obs'][0]. To get to the individual observations you then just use the index numbers given in the API docs:

Sky (type=“obs_sky”)
Observation Layout
0 - Epoch (seconds UTC)
1 - Illuminance (lux)
2 - UV (index)
3 - Rain Accumulation (mm)
4 - Wind Lull (m/s)
5 - Wind Avg (m/s)
6 - Wind Gust (m/s)
7 - Wind Direction (degrees)
8 - Battery (volts)
9 - Report Interval (minutes)
10 - Solar Radiation (W/m^2)
11 - Local Day Rain Accumulation (mm)
12 - Precipitation Type (0 = none, 1 = rain, 2 = hail)
13 - Wind Sample Interval (seconds)
14 - Rain Accumulation Final (Rain Check) (mm)
15 - Local Day Rain Accumulation Final (Rain Check) (mm)
16 - Precipitation Analysis Type (0 = none, 1 = Rain Check with user display on, 2 = Rain Check with user display off)

So for the fields you want, the final part of the code would look something like

solar_radiation_array=Obs[10]
uv_index_array=Obs[2]
brightness_array=Obs[1]

Hope this makes sense and is helpful!

4 Likes

That’s great to know! Thanks!!

In a prior attempt at figuring this out, I was looking at parsing the json, but I wasn’t getting anywhere. I gave up and never came back to it. I’ll look at this though as it would be helpful in other projects I have going on.

I have another program running that sends me wind alerts when the wind is strong enough for me to windsurf. That was the real reason I wanted a weather station. :wink: Everything else is gravy.

3 Likes