25.02.2022, 08:31
yr.no provides free detailed forecast API.
This example will check if there will be snow in the next day. You only need to adjust your location (latitude/longitude) and the status object address. There might be some limits on how often the API can be accessed so create a scheduled script that runs every 3 hours or so.
This example will check if there will be snow in the next day. You only need to adjust your location (latitude/longitude) and the status object address. There might be some limits on how often the API can be accessed so create a scheduled script that runs every 3 hours or so.
Code:
latitude = 60.3894
longitude = 5.33
status = '1/1/1'
json = require('json')
http = require('socket.http')
args = 'lat=' .. latitude .. '&lon=' .. longitude
url = 'https://api.met.no/weatherapi/locationforecast/2.0/compact?' .. args
mac = 0
io.readfile('/sys/class/net/eth0/address'):gsub('%x%x', function(v)
mac = mac * 256 + tonumber(v, 16)
end)
http.USERAGENT = 'LM ' .. mac
res, err = http.request(url)
data = json.pdecode(res)
if type(data) == 'table' then
date = os.date('*t')
date.day = date.day + 1
time = os.time(date)
timestamp = os.date('%Y-%m-%d', time)
snow = false
for i, entry in ipairs(data.properties.timeseries) do
if entry.time:sub(1, #timestamp) == timestamp then
next1h = entry.data.next_1_hours
code = ''
if type(next1h) == 'table' and type(next1h.summary) == 'table' then
code = next1h.summary.symbol_code or ''
end
if code:find('snow') then
snow = true
break
end
end
end
grp.checkwrite(status, snow)
end