Skip to content

Commit e8c36c8

Browse files
authored
Merge pull request #195 from MallocArray/aqi_sensor
Use new ESPHome sensor for calculating AQI
2 parents e8a9f1a + aeacc34 commit e8c36c8

5 files changed

Lines changed: 25 additions & 167 deletions

packages/sensor_pms5003_extended_life.yaml

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -74,38 +74,10 @@ sensor:
7474
return result;
7575
}
7676
77-
- platform: template
78-
# Depends on another sensor providing an ID of pm_2_5 such as a pms5003
77+
- platform: aqi
78+
# https://esphome.io/components/sensor/aqi/
7979
name: "PM 2.5 AQI"
8080
id: pm_2_5_aqi
81-
device_class: aqi
82-
update_interval: 5 min
83-
icon: "mdi:air-filter"
84-
accuracy_decimals: 0
85-
filters:
86-
- skip_initial: 1 # Need valid data from PM 2.5 sensor before able to calculate
87-
lambda: |-
88-
// 2024 update from EPA https://www.epa.gov/system/files/documents/2024-02/pm-naaqs-air-quality-index-fact-sheet.pdf
89-
// https://en.wikipedia.org/wiki/Air_quality_index#Computing_the_AQI
90-
// Borrowed from https://github.com/kylemanna/sniffer/blob/master/esphome/sniffer_common.yaml
91-
if (id(pm_2_5).state <= 9.0) {
92-
// good
93-
return((50.0 - 0.0) / (9.0 - 0.0) * (id(pm_2_5).state - 0.0) + 0.0);
94-
} else if (id(pm_2_5).state <= 35.4) {
95-
// moderate
96-
return((100.0 - 51.0) / (35.4 - 9.1) * (id(pm_2_5).state - 9.1) + 51.0);
97-
} else if (id(pm_2_5).state <= 55.4) {
98-
// usg
99-
return((150.0 - 101.0) / (55.4 - 35.5) * (id(pm_2_5).state - 35.5) + 101.0);
100-
} else if (id(pm_2_5).state <= 125.4) {
101-
// unhealthy
102-
return((200.0 - 151.0) / (125.4 - 55.5) * (id(pm_2_5).state - 55.5) + 151.0);
103-
} else if (id(pm_2_5).state <= 225.4) {
104-
// very unhealthy
105-
return((300.0 - 201.0) / (225.4 - 125.5) * (id(pm_2_5).state - 125.5) + 201.0);
106-
} else if (id(pm_2_5).state <= 325.4) {
107-
// hazardous
108-
return((500.0 - 301.0) / (325.4 - 225.5) * (id(pm_2_5).state - 225.5) + 301.0);
109-
} else {
110-
return(500);
111-
}
81+
pm_2_5: pm_2_5
82+
pm_10_0: pm_10_0
83+
calculation_type: AQI

packages/sensor_pms5003_uncorrected.yaml

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,10 @@ sensor:
2323
filters:
2424
update_interval: 30s
2525

26-
27-
- platform: template
28-
# Depends on another sensor providing an ID of pm_2_5 such as a pms5003
26+
- platform: aqi
27+
# https://esphome.io/components/sensor/aqi/
2928
name: "PM 2.5 AQI"
3029
id: pm_2_5_aqi
31-
device_class: aqi
32-
update_interval: 5 min
33-
icon: "mdi:air-filter"
34-
accuracy_decimals: 0
35-
filters:
36-
- skip_initial: 1 # Need valid data from PM 2.5 sensor before able to calculate
37-
lambda: |-
38-
// 2024 update from EPA https://www.epa.gov/system/files/documents/2024-02/pm-naaqs-air-quality-index-fact-sheet.pdf
39-
// https://en.wikipedia.org/wiki/Air_quality_index#Computing_the_AQI
40-
// Borrowed from https://github.com/kylemanna/sniffer/blob/master/esphome/sniffer_common.yaml
41-
if (id(pm_2_5).state <= 9.0) {
42-
// good
43-
return((50.0 - 0.0) / (9.0 - 0.0) * (id(pm_2_5).state - 0.0) + 0.0);
44-
} else if (id(pm_2_5).state <= 35.4) {
45-
// moderate
46-
return((100.0 - 51.0) / (35.4 - 9.1) * (id(pm_2_5).state - 9.1) + 51.0);
47-
} else if (id(pm_2_5).state <= 55.4) {
48-
// usg
49-
return((150.0 - 101.0) / (55.4 - 35.5) * (id(pm_2_5).state - 35.5) + 101.0);
50-
} else if (id(pm_2_5).state <= 125.4) {
51-
// unhealthy
52-
return((200.0 - 151.0) / (125.4 - 55.5) * (id(pm_2_5).state - 55.5) + 151.0);
53-
} else if (id(pm_2_5).state <= 225.4) {
54-
// very unhealthy
55-
return((300.0 - 201.0) / (225.4 - 125.5) * (id(pm_2_5).state - 125.5) + 201.0);
56-
} else if (id(pm_2_5).state <= 325.4) {
57-
// hazardous
58-
return((500.0 - 301.0) / (325.4 - 225.5) * (id(pm_2_5).state - 225.5) + 301.0);
59-
} else {
60-
return(500);
61-
}
30+
pm_2_5: pm_2_5
31+
pm_10_0: pm_10_0
32+
calculation_type: AQI

packages/sensor_pms5003t.yaml

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -106,38 +106,10 @@ sensor:
106106
return result;
107107
}
108108
109-
- platform: template
110-
# Depends on another sensor providing an ID of pm_2_5 such as a pms5003
109+
- platform: aqi
110+
# https://esphome.io/components/sensor/aqi/
111111
name: "PM 2.5 AQI"
112112
id: pm_2_5_aqi
113-
update_interval: 5 min
114-
device_class: aqi
115-
icon: "mdi:air-filter"
116-
accuracy_decimals: 0
117-
filters:
118-
- skip_initial: 1 # Need valid data from PM 2.5 sensor before able to calculate
119-
lambda: |-
120-
// 2024 update from EPA https://www.epa.gov/system/files/documents/2024-02/pm-naaqs-air-quality-index-fact-sheet.pdf
121-
// https://en.wikipedia.org/wiki/Air_quality_index#Computing_the_AQI
122-
// Borrowed from https://github.com/kylemanna/sniffer/blob/master/esphome/sniffer_common.yaml
123-
if (id(pm_2_5).state <= 9.0) {
124-
// good
125-
return((50.0 - 0.0) / (9.0 - 0.0) * (id(pm_2_5).state - 0.0) + 0.0);
126-
} else if (id(pm_2_5).state <= 35.4) {
127-
// moderate
128-
return((100.0 - 51.0) / (35.4 - 9.1) * (id(pm_2_5).state - 9.1) + 51.0);
129-
} else if (id(pm_2_5).state <= 55.4) {
130-
// usg
131-
return((150.0 - 101.0) / (55.4 - 35.5) * (id(pm_2_5).state - 35.5) + 101.0);
132-
} else if (id(pm_2_5).state <= 125.4) {
133-
// unhealthy
134-
return((200.0 - 151.0) / (125.4 - 55.5) * (id(pm_2_5).state - 55.5) + 151.0);
135-
} else if (id(pm_2_5).state <= 225.4) {
136-
// very unhealthy
137-
return((300.0 - 201.0) / (225.4 - 125.5) * (id(pm_2_5).state - 125.5) + 201.0);
138-
} else if (id(pm_2_5).state <= 325.4) {
139-
// hazardous
140-
return((500.0 - 301.0) / (325.4 - 225.5) * (id(pm_2_5).state - 225.5) + 301.0);
141-
} else {
142-
return(500);
143-
}
113+
pm_2_5: pm_2_5
114+
pm_10_0: pm_10_0
115+
calculation_type: AQI

packages/sensor_pms5003t_extended_life.yaml

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -106,38 +106,10 @@ sensor:
106106
return result;
107107
}
108108
109-
- platform: template
110-
# Depends on another sensor providing an ID of pm_2_5 such as a pms5003
109+
- platform: aqi
110+
# https://esphome.io/components/sensor/aqi/
111111
name: "PM 2.5 AQI"
112112
id: pm_2_5_aqi
113-
update_interval: 5 min
114-
device_class: aqi
115-
icon: "mdi:air-filter"
116-
accuracy_decimals: 0
117-
filters:
118-
- skip_initial: 1 # Need valid data from PM 2.5 sensor before able to calculate
119-
lambda: |-
120-
// 2024 update from EPA https://www.epa.gov/system/files/documents/2024-02/pm-naaqs-air-quality-index-fact-sheet.pdf
121-
// https://en.wikipedia.org/wiki/Air_quality_index#Computing_the_AQI
122-
// Borrowed from https://github.com/kylemanna/sniffer/blob/master/esphome/sniffer_common.yaml
123-
if (id(pm_2_5).state <= 9.0) {
124-
// good
125-
return((50.0 - 0.0) / (9.0 - 0.0) * (id(pm_2_5).state - 0.0) + 0.0);
126-
} else if (id(pm_2_5).state <= 35.4) {
127-
// moderate
128-
return((100.0 - 51.0) / (35.4 - 9.1) * (id(pm_2_5).state - 9.1) + 51.0);
129-
} else if (id(pm_2_5).state <= 55.4) {
130-
// usg
131-
return((150.0 - 101.0) / (55.4 - 35.5) * (id(pm_2_5).state - 35.5) + 101.0);
132-
} else if (id(pm_2_5).state <= 125.4) {
133-
// unhealthy
134-
return((200.0 - 151.0) / (125.4 - 55.5) * (id(pm_2_5).state - 55.5) + 151.0);
135-
} else if (id(pm_2_5).state <= 225.4) {
136-
// very unhealthy
137-
return((300.0 - 201.0) / (225.4 - 125.5) * (id(pm_2_5).state - 125.5) + 201.0);
138-
} else if (id(pm_2_5).state <= 325.4) {
139-
// hazardous
140-
return((500.0 - 301.0) / (325.4 - 225.5) * (id(pm_2_5).state - 225.5) + 301.0);
141-
} else {
142-
return(500);
143-
}
113+
pm_2_5: pm_2_5
114+
pm_10_0: pm_10_0
115+
calculation_type: AQI

packages/sensor_pms5003t_uncorrected.yaml

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,39 +52,10 @@ sensor:
5252
window_size: 30
5353
send_every: 30
5454

55-
56-
- platform: template
57-
# Depends on another sensor providing an ID of pm_2_5 such as a pms5003
55+
- platform: aqi
56+
# https://esphome.io/components/sensor/aqi/
5857
name: "PM 2.5 AQI"
5958
id: pm_2_5_aqi
60-
update_interval: 5 min
61-
device_class: aqi
62-
icon: "mdi:air-filter"
63-
accuracy_decimals: 0
64-
filters:
65-
- skip_initial: 1 # Need valid data from PM 2.5 sensor before able to calculate
66-
lambda: |-
67-
// 2024 update from EPA https://www.epa.gov/system/files/documents/2024-02/pm-naaqs-air-quality-index-fact-sheet.pdf
68-
// https://en.wikipedia.org/wiki/Air_quality_index#Computing_the_AQI
69-
// Borrowed from https://github.com/kylemanna/sniffer/blob/master/esphome/sniffer_common.yaml
70-
if (id(pm_2_5).state <= 9.0) {
71-
// good
72-
return((50.0 - 0.0) / (9.0 - 0.0) * (id(pm_2_5).state - 0.0) + 0.0);
73-
} else if (id(pm_2_5).state <= 35.4) {
74-
// moderate
75-
return((100.0 - 51.0) / (35.4 - 9.1) * (id(pm_2_5).state - 9.1) + 51.0);
76-
} else if (id(pm_2_5).state <= 55.4) {
77-
// usg
78-
return((150.0 - 101.0) / (55.4 - 35.5) * (id(pm_2_5).state - 35.5) + 101.0);
79-
} else if (id(pm_2_5).state <= 125.4) {
80-
// unhealthy
81-
return((200.0 - 151.0) / (125.4 - 55.5) * (id(pm_2_5).state - 55.5) + 151.0);
82-
} else if (id(pm_2_5).state <= 225.4) {
83-
// very unhealthy
84-
return((300.0 - 201.0) / (225.4 - 125.5) * (id(pm_2_5).state - 125.5) + 201.0);
85-
} else if (id(pm_2_5).state <= 325.4) {
86-
// hazardous
87-
return((500.0 - 301.0) / (325.4 - 225.5) * (id(pm_2_5).state - 225.5) + 301.0);
88-
} else {
89-
return(500);
90-
}
59+
pm_2_5: pm_2_5
60+
pm_10_0: pm_10_0
61+
calculation_type: AQI

0 commit comments

Comments
 (0)