/** | |
* PM2.5/PM10 Air Quality Particulate Handler for Node-Red | |
* | |
* * | |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | |
* in compliance with the License. You may obtain a copy of the License at: | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed | |
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License | |
* for the specific language governing permissions and limitations under the License. | |
* | |
*/ | |
metadata { | |
definition(name: "airquality-nodered", namespace: "kellyland", author: "Michael Kelly") { | |
capability "Air Quality Sensor" | |
capability "Refresh" | |
capability "Sensor" | |
} | |
tiles { | |
valueTile("pm25", "device.pm25", width: 1, height: 1, decoration: "flat") { | |
state "val", label: '${currentValue}\nPM2.5', unit:"µg/m³", | |
backgroundColors:[ | |
[value: 12.0, color: "#00e400"], | |
[value: 35.4, color: "#ff0"], | |
[value: 55.4, color: "#f90"], | |
[value: 150.4, color: "#f00"], | |
[value: 250.4, color: "#540099"], | |
[value: 500.4, color: "#800000"] | |
] | |
} | |
valueTile("pm10", "device.pm10", width: 1, height: 1, decoration: "flat") { | |
state "val", label: '${currentValue}\nPM10', unit:"µg/m³", | |
backgroundColors:[ | |
[value: 54, color: "#00e400"], | |
[value: 154, color: "#ff0"], | |
[value: 254, color: "#f90"], | |
[value: 354, color: "#f00"], | |
[value: 424, color: "#540099"], | |
[value: 604, color: "#800000"] | |
] | |
} | |
valueTile("pm25_outside", "device.pm25_outside", width: 1, height: 1, decoration: "flat") { | |
state "val", label: '${currentValue}\nMartinez', unit:"AQI", | |
backgroundColors:[ | |
[value: 54, color: "#00e400"], | |
[value: 154, color: "#ff0"], | |
[value: 254, color: "#f90"], | |
[value: 354, color: "#f00"], | |
[value: 424, color: "#540099"], | |
[value: 604, color: "#800000"] | |
] | |
} | |
standardTile("refresh", "device.airQuality", inactiveLabel: true, decoration: "flat") { | |
state "default", label: '', action: "refresh.refresh", icon: "st.secondary.refresh" | |
} | |
main (["pm25"]) | |
details(["pm25","pm10", "pm25_outside", "refresh"]) | |
} | |
} | |
def initialize() { | |
log.debug "Initializing" | |
runEvery5Minutes(getAirQualityData); | |
} | |
def refresh() { | |
log.debug "Refreshing" | |
getAirQualityData(); | |
} | |
// parse events into attributes | |
def parse(String description) { | |
log.debug "Parsing '${description}'" | |
} | |
def getAirQualityData() { | |
getHomeAirQualityData(); | |
getOutsideAirQualityData(); | |
} | |
def getOutsideAirQualityData() { | |
def params = [ | |
uri: "http://www.airnowapi.org/aq/", | |
path: "observation/zipCode/current/", | |
query:[format:"application/json", zipCode:<<"ZIPCO">>, distance:"25", API_KEY:<<"XXXXXXXXXXXXXXXXXXXXXX">>], | |
contentType: 'application/json' | |
]; | |
try { | |
httpGet(params) {resp -> | |
log.debug "${path} data: ${resp.data[1]}" | |
if(resp.data[1].ParameterName=="PM2.5") { | |
sendEvent(name:"pm25_outside", value:resp.data[1].AQI+"\n"+resp.data[1].Category.Name); | |
} | |
} | |
} catch (e) { | |
log.error "error: $e" | |
} | |
} | |
def getHomeAirQualityData() { | |
def params = [ | |
uri: "http://XXXX.XXXXX:XXXX/air/", | |
path: "quality", | |
contentType: 'application/json' | |
] | |
try { | |
httpGet(params) {resp -> | |
log.debug "${path} data: ${resp.data.pms}" | |
sendEvent(name:"pm25", value:resp.data.pms.two); | |
sendEvent(name:"pm10", value:resp.data.pms.ten); | |
} | |
} catch (e) { | |
log.error "error: $e" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment