Last active
March 6, 2023 04:50
-
-
Save wolfg1969/faf540a15a59fab4007c56fd33fd392d to your computer and use it in GitHub Desktop.
HeWeather API v7 parser for Domoticz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=====[ | |
https://dev.qweather.com/docs/api/air/air-now/ | |
curl -L -X GET --compressed 'https://api.qweather.com/v7/air/now?location=101010100&key=YOUR_KEY' | |
{ | |
"code": "200", | |
"updateTime": "2021-02-16T14:42+08:00", | |
"fxLink": "http://hfx.link/2ax4", | |
"now": { | |
"pubTime": "2021-02-16T14:00+08:00", | |
"aqi": "28", | |
"level": "1", | |
"category": "优", | |
"primary": "NA", | |
"pm10": "28", | |
"pm2p5": "5", | |
"no2": "3", | |
"so2": "2", | |
"co": "0.2", | |
"o3": "76" | |
}, | |
"station": [ | |
... | |
], | |
"refer": { | |
"sources": [ | |
"cnemc" | |
], | |
"license": [ | |
"commercial license" | |
] | |
} | |
} | |
--]=====] | |
s = request['content'] | |
print("s="..s) | |
local deviceId = 22 | |
local aqi = domoticz_applyJsonPath(s, '.now.aqi') | |
local pm10 = domoticz_applyJsonPath(s, '.now.pm10') | |
local pm25 = domoticz_applyJsonPath(s, '.now.pm2p5') | |
local qlty = domoticz_applyJsonPath(s, '.now.category') | |
local primary = domoticz_applyJsonPath(s, '.now.primary') | |
local update = domoticz_applyJsonPath(s, '.now.pubTime') | |
local alert_level = 0 -- 0=gray, 1=green, 2=yellow, 3=orange, 4=red | |
aqi_value = tonumber(aqi) | |
if aqi_value >=0 and aqi_value<=50 then | |
alert_level = 1 | |
elseif aqi_value >=51 and aqi_value<=100 then | |
alert_level = 2 | |
elseif aqi_value >=101 and aqi_value<=150 then | |
alert_level = 3 | |
elseif aqi_value >=151 then | |
alert_level = 4 | |
end | |
local short_qlty_desc = qlty .. " " .. aqi .. ", 主要污染物:" .. primary | |
local long_qlty_desc = short_qlty_desc .. ", 发布时间:" .. update | |
print("aqi=" .. aqi .. " pm10=" .. pm10 .. " pm25=" .. pm25 .." qlty=" .. qlty) | |
domoticz_updateDevice(24, 0, aqi) | |
domoticz_updateDevice(25, 0, pm25) | |
domoticz_updateDevice(26, 0, pm10) | |
domoticz_updateDevice(27, 0, long_qlty_desc) | |
domoticz_updateDevice(28, alert_level, short_qlty_desc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[=====[ | |
https://dev.qweather.com/docs/api/weather/weather-now/ | |
curl -L -X GET --compressed 'https://api.qweather.com/v7/weather/now?location=101010100&key=YOUR_KEY' | |
{ | |
"code": "200", | |
"updateTime": "2020-06-30T22:00+08:00", | |
"fxLink": "http://hfx.link/2ax1", | |
"now": { | |
"obsTime": "2020-06-30T21:40+08:00", | |
"temp": "24", | |
"feelsLike": "26", | |
"icon": "101", | |
"text": "多云", | |
"wind360": "123", | |
"windDir": "东南风", | |
"windScale": "1", | |
"windSpeed": "3", | |
"humidity": "72", | |
"precip": "0.0", | |
"pressure": "1003", | |
"vis": "16", | |
"cloud": "10", | |
"dew": "21" | |
}, | |
"refer": { | |
"sources": [ | |
"QWeather", | |
"NMC", | |
"ECMWF" | |
], | |
"license": [ | |
"commercial license" | |
] | |
} | |
} | |
--]=====] | |
s = request['content'] | |
print("s="..s) | |
local temp = domoticz_applyJsonPath(s, '.now.temp') | |
local hum = tonumber(domoticz_applyJsonPath(s, '.now.humidity')) | |
local pres = domoticz_applyJsonPath(s, '.now.pressure') | |
local condCode = tonumber(domoticz_applyJsonPath(s, '.now.icon')) | |
local humStat = 0 | |
if hum < 25 then | |
humStat = 2 | |
elseif hum > 60 then | |
humStat = 3 | |
elseif hum >= 25 and hum <= 60 then | |
humStat = 1 | |
end | |
local cond = 0 | |
if condCode == 100 then | |
cond = 1 | |
elseif condCode == 103 then | |
cond = 2 | |
elseif condCode == 101 then | |
cond = 3 | |
elseif condCode >= 300 and condCode < 400 then | |
cond = 4 | |
end | |
print("temp=" .. temp .. " hum=" .. hum .. " pres=" .. pres .." cond=" .. cond) | |
domoticz_updateDevice(22, 0, temp .. ";" .. hum .. ";" .. humStat .. ";" .. pres .. ";" .. cond) | |
-- feel temp | |
local fl = domoticz_applyJsonPath(s, '.now.feelsLike') | |
domoticz_updateDevice(38, 0, fl) | |
--[=====[ | |
wind | |
/json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=WB;WD;WS;WG;22;24 | |
IDX = id of your device (This number can be found in the devices tab in the column "IDX") | |
WB = Wind bearing (0-359) | |
WD = Wind direction (S, SW, NNW, etc.) | |
WS = 10 * Wind speed [m/s] | |
WG = 10 * Gust [m/s] | |
22 = Temperature | |
24 = Temperature Windchill | |
--]=====] | |
local wb = domoticz_applyJsonPath(s, '.now.wind360') | |
local wd = domoticz_applyJsonPath(s, '.now.windDir') | |
local ws = domoticz_applyJsonPath(s, '.now.windSpeed') -- km/h | |
ws = 10 * tonumber(ws) * 1000 / 3600 | |
--local wg = domoticz_applyJsonPath(s, '.HeWeather5[0].now.wind.sc') | |
local wg = "0" | |
domoticz_updateDevice(39, 0, wb .. ";" .. wd .. ";" .. ws .. ";" .. wg .. ";".. temp .. ";" .. fl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment