Get online airquality data  [SOLVED]

Moderator: leecollings

elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Get online airquality data

Post by elmortero »

I have no airquality sensor but was interested in getting the info online.

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
}
User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Get online airquality data

Post by capman »

I have used this script but have this error in the log.

Status: dzVents: Error (2.4.7): ...var/scripts/dzVents/generated_scripts/Luchtkwaliteit.lua:40: attempt to index field 'co' (a nil value)

Someone now where the error is ? I use latest beta on synology. Dzvents version 2.4.7
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Get online airquality data

Post by elmortero »

if you paste the URL in a browser with your data do you get a json result?
replace X,Y and Z http://api.waqi.info/feed/geo:XXXX;YYYY/?token=ZZZZZ

If you get result, is there a value for co ?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Get online airquality data

Post by waaren »

capman wrote: Friday 27 July 2018 21:41 I have used this script but have this error in the log.

Status: dzVents: Error (2.4.7): ...var/scripts/dzVents/generated_scripts/Luchtkwaliteit.lua:40: attempt to index field 'co' (a nil value)

Someone now where the error is ? I use latest beta on synology. Dzvents version 2.4.7
The error is that the response data does not contain a value for co and the script does not check for that.
Good part is that this value is not used elsewhere in the script so you can do a couple of things to prevent the error stopping the script execution.

put -- at the start of line 40 to make it a comment line

or

delete line 40

or

build in a check like

Code: Select all

if response.json.data.iaqi.co ~= nil and response.json.data.iaqi.co ~= nil then
     local colevel = tonumber(response.json.data.iaqi.co.v)
else
    print('**CalidAireV2 failed to fetch co info')
    local colevel = 0
end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Get online airquality data

Post by waaren »

elmortero wrote: Friday 27 July 2018 22:02 if you paste the URL in a browser with your data do you get a json result?
replace X,Y and Z http://api.waqi.info/feed/geo:XXXX;YYYY/?token=ZZZZZ

If you get result, is there a value for co ?
Just checked but value for co is not there

Code: Select all

{
"status": "ok",
"data": {
"aqi": 72,
"idx": 5284,
"attributions": [
{
"url": "http://www.luchtmeetnet.nl/",
"name": "RIVM - Rijksinstituut voor Volksgezondheid en Milieum, Landelijk Meetnet Luchtkwaliteit"
}
],
"city": {
"geo": [
xx.89361,
y.48759
],
"name": "xxxxxxxxx, Rotterdam Zuid",
"url": "http://aqicn.org/city/netherland/rotterdam-zuid/xxxxxxxx/"
},
"dominentpol": "o3",
"iaqi": {
"no2": {
"v": 10.9
},
"o3": {
"v": 72
},
"p": {
"v": 1007.1
},
"pm10": {
"v": 18
},
"pm25": {
"v": 45
},
"t": {
"v": 31.9
},
"w": {
"v": 5.5
},
"wg": {
"v": 8.5
}
},
"time": {
"s": "2018-07-27 20:00:00",
"tz": "+01:00",
"v": 1532721600
}
}
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Get online airquality data

Post by elmortero »

For my location it is.. :-)
But your previous comment is correct, the data is not used in the script.
In fact, anything else than the general AQI index and ozone is unused. I added that to fetch all values for them to be available.
The idea was to read the dominant polutor and then use the value of that one. But that value is already in the aqius variable.
The lines that populate dom, colevel, no2, so2, pm10 and pm25 can be removed from the script.
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Get online airquality data

Post by EdwinK »

So, with the output from http://api.waqi.info/feed/geo:51.860069 ... ?token=API I could those readings in other alerts? What do I need to change in this script to view those?
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Get online airquality data

Post by waaren »

EdwinK wrote: Saturday 28 July 2018 8:24 So, with the output from http://api.waqi.info/feed/geo:51.860069 ... ?token=API I could those readings in other alerts? What do I need to change in this script to view those?
@EdwinK,

there are many ways to do that but if you want to be able to look at the values of the other polluters over time in a domoticz graph you could
add for every value that you want to show a virtual device with type "Custom sensor" and change the line

Code: Select all

local colevel = tonumber(response.json.data.iaqi.co.v)	-- level of CO
in the script with this code snippet

Code: Select all

local coDevice = domoticz.devices('co')                 -- virtual sensor of the type "Custom Sensor"	
if response.json.data.iaqi.co ~= nil and response.json.data.iaqi.co ~= nil then
    co.updateCustomSensor(tonumber(response.json.data.iaqi.co.v))
else
    print('**CalidAireV2 failed to fetch co info')
end
and do this for all polluters of which you want to store the values in a virtual sensor.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Get online airquality data

Post by capman »

I have also no 'co' output. This is the result.

Code: Select all

{
"status":"ok",
"data":{
"aqi":57,
"idx":8903,
"attributions":[
{
"url":"http://www.irceline.be/en/",
"name":"IRCEL-CELINE - Belgian Interregional Environment Agency"}],
"city":{"geo":[xxxxxxxx,yyyyyyyy],
"name":"xxxxxxxxxx",
"url":"http://aqicn.org/city/belgium/fla/xxxxxxxxx/"},
"dominentpol":"pm25",
"iaqi":{"h":{"v":91.2},
"no2":{"v":8.7},
"p":{"v":1006.9},
"pm10":{"v":20},
"pm25":{"v":57},
"t":{"v":21.2},
"w":{"v":3.5}},
"time":{"s":"2018-07-28 09:00:00",
"tz":"+01:00","v":1532768400}}}
But i have also no '03' output that you use for the ozon. Maybe it's another value ? Or I can't use the script :cry:
Last edited by capman on Sunday 29 July 2018 8:48, edited 1 time in total.
User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Get online airquality data

Post by capman »

But on the site I have this.
airquality.JPG
airquality.JPG (155.38 KiB) Viewed 6187 times
Maybe I can also use it as temp , wind, humidity , pressure sensor in place of WU.
The other values means.
PM2.5 = Fine particulate matter
PM10 = Respirable particulate matter
NO2 = Nitrogen dioxide

On the bottom you have the forecast calendar , and there is a ozone row.
Maybe the PM10 I can use, because this is not healthy when it's to high ;)
Last edited by capman on Saturday 28 July 2018 11:07, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Get online airquality data

Post by waaren »

capman wrote: Saturday 28 July 2018 10:22 I have also no 'co' output. ... But i have also no '03' output that you use for the ozon. Maybe it's another value ? Or I can't use the script :cry:
The station closest to your lat / long setting does not measure O3. The nearest station for you that does measure O3 is Schoten.

If you use http://api.waqi.info/feed/geo:51.25;4.5 ... =xxxxxxxxx

you will get info from that station
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Get online airquality data

Post by capman »

I give this a try , just to test the script. Thanks.
Last edited by capman on Sunday 29 July 2018 8:51, edited 1 time in total.
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Get online airquality data

Post by elmortero »

waaren wrote: Saturday 28 July 2018 11:03 The nearest station for you that does measure O3 is Schoten.
Indeed, according to this site http://www.vmm.be/data/ozon-actuele-waa ... hrijdingen Schoten is your nearest station that provides Ozone info.
(Waaren is way faster in his replies than I am :-) )
User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Get online airquality data

Post by capman »

I think that I just moving then to Schoten :D .
BTW , the script is working when I use the lat and lon for Schoten
working.JPG
working.JPG (37.72 KiB) Viewed 6170 times
I gone try to edit the script for the PM10 value. This is also some importing value for the air quality.
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Get online airquality data

Post by elmortero »

capman wrote: Saturday 28 July 2018 11:22 I gone try to edit the script for the PM10 value. This is also some importing value for the air quality.
The script already gets that value, you just need to create a sensor and update it with the value that is stored in the variable pm25
User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

Re: Get online airquality data

Post by capman »

But I need to change this also ? Because you're using values for the ozone (o3) and not for the respirable particulate matter (pm10).
I only gone use the pm10 value.

Code: Select all

	-- 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
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Get online airquality data

Post by elmortero »

capman wrote: Saturday 28 July 2018 12:13 But I need to change this also ? Because you're using values for the ozone (o3) and not for the respirable particulate matter (pm10).
I only gone use the pm10 value.
This line (if you copied it from the original script) fetches the PM10 value:
local pm10 = tonumber(response.json.data.iaqi.pm10.v)

Create a Custom sensor, like the one you have created for Ozon
for example : local PM10sensor = domoticz.devices('PM10') -- virtual sensor of the type "Custom Sensor"

Then add this to your script (somewhere after the line I just quoted)

if pm10 ~= nil then
PM10sensor.updateCustomSensor(pm10)
end
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Get online airquality data

Post by EdwinK »

waaren wrote: Saturday 28 July 2018 10:19
EdwinK wrote: Saturday 28 July 2018 8:24 So, with the output from http://api.waqi.info/feed/geo:51.860069 ... ?token=API I could those readings in other alerts? What do I need to change in this script to view those?
@EdwinK,

there are many ways to do that but if you want to be able to look at the values of the other polluters over time in a domoticz graph you could
add for every value that you want to show a virtual device with type "Custom sensor" and change the line

Code: Select all

removded to keep this clean 
and do this for all polluters of which you want to store the values in a virtual sensor.

I think I better wait till someone more smarter with scripting has done this ;)
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Get online airquality data

Post by elmortero »

EdwinK wrote: Saturday 28 July 2018 12:55 I think I better wait till someone more smarter with scripting has done this ;)
Do you want it to be for PM10 only?
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Get online airquality data

Post by elmortero »

For PM10 only (with alert sensor):

Code: Select all

return {
	on = {
		timer = { 'every 6 minutes' },
		httpResponses = { 'PM10' } -- matches callback string below
	},
	
	execute = function(domoticz, triggerItem)
local lat = 'Your_Latitude'
local long = 'Your_longitude'
local apikey = 'YOU_API_KEY'
    local PM10sensor = domoticz.devices('PM10') 				-- virtual sensor of the type "Custom sensor"
    local prevPM10 = PM10sensor.rawData[1]						-- get the current value of the Alert
	local pmalert = domoticz.devices('PM10-Alert')
		if (triggerItem.isTimer) then
			domoticz.openURL({
				url = 'http://api.waqi.info/feed/geo:'..lat..';'..long..'/?token='..apikey,
				method = 'GET',
				callback = 'PM10'
			})
		elseif (triggerItem.isHTTPResponse) then

	local response = triggerItem
		if (response.ok and response.isJSON) then
		local pm10 = tonumber(response.json.data.iaqi.pm10.v)
			
        if pm10 ~= nil and pm10 ~= prevPM10 then 
		 PM10sensor.updateCustomSensor(pm10)
        end

		if pm10 ~= prevpm10 then			-- before updating the sensor check if the value has changed (in order to avoid unneeded updating, 
				if pm10 <= 100 then level = domoticz.ALERTLEVEL_GREEN; nfo = "Good"
				elseif pm10 > 100 and pm10 <= 200 then level = domoticz.ALERTLEVEL_YELLOW; nfo = "Polluted"
				elseif pm10 > 200 and pm10 <= 300 then level = domoticz.ALERTLEVEL_ORANGE; nfo = "Very Polluted"
				elseif pm10 > 300 then level =  domoticz.ALERTLEVEL_RED; nfo = "Alarming"
				end
			pmalert.updateAlertSensor(level, nfo)	-- if you want just on sensor with alert level and value (as text) replace nfo with o3
		end

			else
				print('**PM10failed to fetch info')
			end
		end
	end
}
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests