Lua code to query current local wind speed (Success)

My location gets occasional high winds so I’ve always wanted to avoid farmbot movement. With the Lua capability this is now easy! Just sign up for a free account on Weather Stack. Here is what worked for me to query the wind speed as an integer:

url="http://api.weatherstack.com/current?access_key=<API_KEY>&query=<ZIP_CODE>"

res, err = http({ url = url, method = 'GET'})

if err then
    send_message("error", "ERROR: " .. inspect(err), {"toast"})
else
    wind_speed = json.decode(res.body).current.wind_speed
   
    send_message("info", "Wind Speed:" .. tostring(wind_speed))
end
2 Likes

That’s a great use case @steffan . I too have been using a weather API for some personal FarmBot usage. I have been meaning to add this one to the examples documentation but have not gotten around to it yet.

The snippet below will check the weather for Chicago and display a toast about the current conditions. This could easily be adapted to other use cases (an exercise that I will leave to the reader).

resp, err = http({
    verb="GET",
    url="http://wttr.in/Chicago,Illinois\?format=j1"
})

if err then
  return send_message("error", "Weather error.")
end
weather = json.decode(resp.body)
temp = tonumber(weather.current_condition[1].FeelsLikeC)

if temp > 22 then
  send_message("info", "It's warm today. Temp (C): " .. temp, "toast")
else
  send_message("info", "It's cold today. Temp (C): " .. temp, "toast")
end

In my case, I used wttr.in as it does not require registration or API keys. I will be sure to check out Weather Stack to see how they compare!

EDIT: It appears that Weather Stack offers historical weather information, which could easily be integrated for things like not watering after a rainy day, watering more on dry days, etc… Thanks again for sharing!

2 Likes

wttr.in uses a service that currently has a huge bug. That URL you are using for Chicago is returning weather info for a place called Mccormickville. The JSON output always returns the next town over rather than the actual town queried.

1 Like

Seems the bug is fixed in the data provider after 2021-03-01 (?) according to that GitHub Issue.

If you check Rick’s link you can see that it doesn’t give data for Chicago.

Right. But the geographic locality is basically Chicago central ! ( according to Mr Google Maps )
55 East Erie Street has the approx. coords given.

    "nearest_area": [
        {
            "areaName": [
                {
                    "value": "Mccormickville"
                }
            ],
            "country": [
                {
                    "value": "United States of America"
                }
            ],
            "latitude": "41.894",
            "longitude": "-87.626",
            "population": "0",
            "region": [
                {
                    "value": "Illinois"
                }
            ],
            "weatherUrl": [
                {
                    "value": ""
                }
            ]
        }
    ],
    "request": [
        {
            "query": "Lat 41.88 and Lon -87.62",
            "type": "LatLon"
        }
    ],
    "weather": [
 

McCormickville is (was) a neighborhood in Chicago. Chicago is a big city, so it is often split into smaller neighborhoods. McCormickville still has a “Chicago, IL” mailing address.

If you pick a smaller city, such as “Blooimgdale, IL”, you might get the weather for “Addison, IL”, but that is probably close enough for the sake of gardening (there are no microclimates in Illinois).