This is what I came up with. It provides the general air quality index (AQI), but also separate values for the various polutors that are measured in order to get the index.
Code: Select all
--[[
data from http://waqi.info/
An API key is required and can be aquired here ==> http://aqicn.org/data-platform/token/
I used two sensors for the Ozon value - mainly as a test - but you can use just the alert sensor and put the ozon value in the text
This separation was done to have a numeric value instead of text so it can be displayed as a graph
this script requires dzVents 2.4.0 or higher
If you are on a previous version and cannot upgrade please refer to BakSeeDaa's solution for fetching data from external servers
in this posthttp://www.domoticz.com/forum/viewtopic.php?f=59&t=19327
]]
return {
on = {
timer = { 'every 6 minutes' },
httpResponses = { 'waqi' } -- matches callback string below
},
execute = function(domoticz, triggerItem)
local lat = 'Your_Latitude'
local long = 'Your_longitude'
local apikey = 'YOU_API_KEY'
local AirC = domoticz.devices('AirC') -- virtual sensor of the type "Air Quality"
local prevAirC = AirC.co2 -- get the current value of the Air Quality sensor
local ozone = domoticz.devices('Ozon-Index') -- virtual sensor of the type "Alert"
local ozon = domoticz.devices('Ozon') -- virtual sensor of the type "Custom Sensor"
local prevo3 = tonumber(ozon.rawData[1])
if (triggerItem.isTimer) then
domoticz.openURL({
url = 'http://api.waqi.info/feed/geo:'..lat..';'..long..'/?token='..apikey,
method = 'GET',
callback = 'waqi'
})
elseif (triggerItem.isHTTPResponse) then
local response = triggerItem
if (response.ok and response.isJSON) then
-- print('CalidAireV2 triggered by Callback')
local aqius = tonumber(response.json.data.aqi) -- combined air quality
local dom = tostring(response.json.data.dominentpol) -- shows the dominant polutor
local colevel = tonumber(response.json.data.iaqi.co.v) -- level of CO
local no2 = tonumber(response.json.data.iaqi.no2.v) -- level of NO2
local o3 = tonumber(response.json.data.iaqi.o3.v) -- level of Ozone
local so2 = tonumber(response.json.data.iaqi.so2.v) -- level of SO2
local pm10 = tonumber(response.json.data.iaqi.pm10.v) -- partical matter of particles < 10µm
local pm25 = tonumber(response.json.data.iaqi.pm25.v) -- partical matter of particles < 2.5µm
if aqius ~= nil then --and aqius ~= prevAirC
AirC.updateAirQuality(aqius)
end
ozon.updateCustomSensor(o3) -- write the numerical value to the custom sensor
-- get the alert level. Based on info from:
-- https://en.wikipedia.org/wiki/Air_quality_index
if o3 ~= prevo3 then -- before updating the sensor check if the value has changed (in order to avoid unneeded updating, and
-- flooding the log
local o3n = tonumber(o3)
if o3n <= 100 then level = domoticz.ALERTLEVEL_GREEN; nfo = "Excellent"
elseif o3n > 100 and o3n <= 168 then level = domoticz.ALERTLEVEL_YELLOW; nfo = "Polluted"
elseif o3n > 168 and o3n <= 208 then level = domoticz.ALERTLEVEL_ORANGE; nfo = "Poor"
elseif o3n > 208 then level = domoticz.ALERTLEVEL_RED; nfo = "Alarming"
end
ozone.updateAlertSensor(level, 'nfo') -- if you want just on sensor with alert level and value (as text) replace nfo with o3
end
else
print('**CalidAireV2 failed to fetch info')
end
end
end
}