forked from ajfriesen/ESPHome-AirGradient
-
Notifications
You must be signed in to change notification settings - Fork 2
/
air-gradient-open-air.yaml
775 lines (712 loc) · 28 KB
/
air-gradient-open-air.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# Airgradient Open Air Outdoor v1.1 presoldered
#
# @llamagecko, @Hendrik, @spectrumjade, and @ex-nerd from AirGradient forum
# did most of the work for this integration, see the following thread:
# https://forum.airgradient.com/t/outdoor-monitor-esphome-configuration/823
#
# Upstream AirGradient Firmware can be found here:
# https://github.com/airgradienthq/arduino/blob/master/examples/DIY_OUTDOOR_C3/DIY_OUTDOOR_C3.ino
#
substitutions:
id: "1"
devicename: "airgradient-open-air"
# WARNING: upper_devicename is component of SSID which is limited to 32 characters
upper_devicename: "AirGradient Open Air"
# Only trigger the particle sensor every 3 minutes so it can go into sleep mode and extend its operational lifetime.
# Here as a substitution mostly to ensure consistent use in this file (several calculations rely on this 3min value).
pm_update_interval: "3min"
globals:
- id: aqi_delay_mins
type: int
restore_value: no
# AQI is calculated over a 24 hour minimum, but EPA says it's acceptable to
# report at 75%, or 18 hours: https://forum.airnowtech.org/t/aqi-calculations-overview-ozone-pm2-5-and-pm10/168
initial_value: '1080'
- id: nowcast_delay_mins
type: int
restore_value: no
# NowCast is calculated over a 12 hour period
initial_value: '720'
- id: pm_2_5_hourly_avg
type: std::vector<float>
restore_value: no
- id: pm_10_0_hourly_avg
type: std::vector<float>
restore_value: no
esphome:
name: "${devicename}-${id}"
friendly_name: "${upper_devicename} ${id}"
# Automatically add the mac address to the name
# so you can use a single firmware for all devices
# name_add_mac_suffix: true
platform: esp32
board: esp32-c3-devkitm-1
# Enable logging?
logger:
baud_rate: 0
# Enable Home Assistant API (API password is deprecated in favor of encryption key)
# https://esphome.io/components/api.html
api:
encryption:
key: !secret home_assistant_encryption_key
ota:
password: !secret ota_password
wifi:
networks:
- ssid: !secret wifi_ssid
password: !secret wifi_password
reboot_timeout: 15min
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "${upper_devicename} Hotspot"
password: !secret fallback_ssid_password
# The captive portal is a fallback mechanism for when connecting to the configured WiFi fails.
# https://esphome.io/components/captive_portal.html
captive_portal:
# Used to support POST request to send data to AirGradient
# https://esphome.io/components/http_request.html
http_request:
# Creates a simple web server on the node that can be accessed through any browser
# https://esphome.io/components/web_server.html
# Turn on the webserver so we can connect and look at raw stats
web_server:
port: 80
include_internal: true
# Create a switch for safe_mode in order to flash the device
# Solution from this thread:
# https://community.home-assistant.io/t/esphome-flashing-over-wifi-does-not-work/357352/1
switch:
- platform: safe_mode
name: "Flash Mode (Safe Mode)"
icon: "mdi:cellphone-arrow-down"
button:
- platform: restart
id: button_restart
name: "Restart"
disabled_by_default: true
output:
- platform: gpio
id: watchdog
pin: GPIO2
script:
- id: calculate_aqi
mode: restart # restart script if called while it's still running (because we run it after pm2.5 and pm10 sensors)
then:
- lambda: |
// AQI is calculated over a 24 hour minimum, but EPA says it's acceptable to
// report at 75%, or 18 hours: https://forum.airnowtech.org/t/aqi-calculations-overview-ozone-pm2-5-and-pm10/168
int hours_to_wait = 18;
// https://en.wikipedia.org/wiki/Air_quality_index#Computing_the_AQI
int aqi_2_5 = -1;
int size_2_5 = id(pm_2_5_hourly_avg).size();
if (size_2_5 >= hours_to_wait) {
float sum = 0.0;
for (int i = 0; i < size_2_5; i++) {
sum += id(pm_2_5_hourly_avg)[i];
}
float pm25 = sum / (float)size_2_5;
if (pm25 < 12.0) {
aqi_2_5 = (50.0 - 0.0) / (12.0 - 0.0) * (pm25 - 0.0) + 0.0;
} else if (pm25 < 35.4) {
aqi_2_5 = (100.0 - 51.0) / (35.4 - 12.1) * (pm25 - 12.1) + 51.0;
} else if (pm25 < 55.4) {
aqi_2_5 = (150.0 - 101.0) / (55.4 - 35.5) * (pm25 - 35.5) + 101.0;
} else if (pm25 < 150.4) {
aqi_2_5 = (200.0 - 151.0) / (150.4 - 55.5) * (pm25 - 55.5) + 151.0;
} else if (pm25 < 250.4) {
aqi_2_5 = (300.0 - 201.0) / (250.4 - 150.5) * (pm25 - 150.5) + 201.0;
} else if (pm25 < 350.4) {
aqi_2_5 = (400.0 - 301.0) / (350.4 - 250.5) * (pm25 - 250.5) + 301.0;
} else if (pm25 < 500.4) {
aqi_2_5 = (500.0 - 401.0) / (500.4 - 350.5) * (pm25 - 350.5) + 401.0;
} else {
aqi_2_5 = 500; // everything higher is just counted as 500
}
} else {
ESP_LOGD("custom", "Skipping pm2.5 AQI calculation. %d hours remaining.", hours_to_wait - size_2_5);
}
int aqi_10_0 = -1;
int size_10_0 = id(pm_10_0_hourly_avg).size();
if (size_10_0 >= hours_to_wait) {
float sum = 0.0;
for (int i = 0; i < size_10_0; i++) {
sum += id(pm_10_0_hourly_avg)[i];
}
float pm10 = sum / (float)size_10_0;
if (pm10 < 54.0) {
aqi_10_0 = (50.0 - 0.0) / (54.0 - 0.0) * (pm10 - 0.0) + 0.0;
} else if (pm10 < 154.0) {
aqi_10_0 = (100.0 - 51.0) / (154.0 - 55.0) * (pm10 - 55.0) + 51.0;
} else if (pm10 < 254.0) {
aqi_10_0 = (150.0 - 101.0) / (254.0 - 155.0) * (pm10 - 155.0) + 101.0;
} else if (pm10 < 354.0) {
aqi_10_0 = (200.0 - 151.0) / (354.0 - 255.0) * (pm10 - 255.0) + 151.0;
} else if (pm10 < 424.0) {
aqi_10_0 = (300.0 - 201.0) / (424.0 - 355.0) * (pm10 - 355.0) + 201.0;
} else if (pm10 < 504.0) {
aqi_10_0 = (400.0 - 301.0) / (504.0 - 425.0) * (pm10 - 425.0) + 301.0;
} else if (pm10 < 604) {
aqi_10_0 = (500.0 - 401.0) / (604.0 - 505.0) * (pm10 - 505.0) + 401.0;
} else {
aqi_10_0 = 500; // everything higher is just counted as 500
}
} else {
ESP_LOGD("custom", "Skipping pm10 AQI calculation. %d hours remaining.", hours_to_wait - size_10_0);
}
int aqi_calc = std::max(aqi_2_5, aqi_10_0);
if (aqi_calc > 0) {
id(aqi).publish_state(aqi_calc);
// Just in case we're counting down, make sure we set to zero
id(aqi_delay_mins) = 0;
if (id(aqi_delay_mins) > 0) {
id(aqi_delay_mins) = 0;
id(aqi_mins_remaining).publish_state(0);
}
// And now publish the category string
if (aqi_calc <= 50.0) {
id(aqi_category).publish_state("Good");
} else if (aqi_calc <= 100.0) {
id(aqi_category).publish_state("Moderate");
} else if (aqi_calc <= 150.0) {
id(aqi_category).publish_state("Unhealthy for Sensitive Groups");
} else if (aqi_calc <= 200.0) {
id(aqi_category).publish_state("Unhealthy");
} else if (aqi_calc <= 300.0) {
id(aqi_category).publish_state("Very Unhealthy");
} else if (aqi_calc <= 400.0) {
id(aqi_category).publish_state("Hazardous");
} else if (aqi_calc <= 500.0) {
id(aqi_category).publish_state("Hazardous"); // again
} else {
id(aqi_category).publish_state("Hazardous"); // and again
}
} else {
int remaining_hours = hours_to_wait - std::max(size_2_5, size_10_0);
ESP_LOGD("custom", "No AQI calculations available. %d hours remaining.", remaining_hours);
// Intervals don't quite overlap with the sensor notification windows,
// so let's readjust this just in case it's fallen out of sync.
id(aqi_delay_mins) = 60 * remaining_hours;
id(aqi_mins_remaining).publish_state(60 * remaining_hours);
}
- id: calculate_nowcast
mode: restart # restart script if called while it's still running
then:
# https://forum.airnowtech.org/t/the-nowcast-for-pm2-5-and-pm10/172
- lambda: |
// NowCast is always calculated over 12 hours, but this is extracted here
// to make debugging easier.
int hours_to_wait = 12;
int nowcast_2_5 = -1;
int size_2_5 = id(pm_2_5_hourly_avg).size();
if (size_2_5 > 12) {
size_2_5 = 12;
}
if (size_2_5 >= hours_to_wait) {
// if (size_2_5 >= 12) {
// Calculate min and max
float max = 0.0;
float min = 31337.0; // just a random large number
for (int i = 0; i < size_2_5; i++) {
float pm = id(pm_2_5_hourly_avg)[i];
if (pm < min) {
min = pm;
}
if (pm > max) {
max = pm;
}
}
// Calculate the weight factor
float range = max - min;
float rate = range / max;
float weight_factor = 1.0 - range;
if (weight_factor < 0.5) {
weight_factor = 0.5;
} else if (weight_factor > 1.0) {
weight_factor = 1.0;
}
float pm_sum = 0.0;
float weight_sum = 0.0;
for (int i = 0; i < size_2_5; i++) {
float weight_pow = pow(weight_factor, i);
pm_sum += id(pm_2_5_hourly_avg)[i] * weight_pow;
weight_sum += weight_pow;
}
float pm25 = pm_sum / weight_sum;
// https://en.wikipedia.org/wiki/Air_quality_index#Computing_the_AQI
if (pm25 < 12.0) {
nowcast_2_5 = (50.0 - 0.0) / (12.0 - 0.0) * (pm25 - 0.0) + 0.0;
} else if (pm25 < 35.4) {
nowcast_2_5 = (100.0 - 51.0) / (35.4 - 12.1) * (pm25 - 12.1) + 51.0;
} else if (pm25 < 55.4) {
nowcast_2_5 = (150.0 - 101.0) / (55.4 - 35.5) * (pm25 - 35.5) + 101.0;
} else if (pm25 < 150.4) {
nowcast_2_5 = (200.0 - 151.0) / (150.4 - 55.5) * (pm25 - 55.5) + 151.0;
} else if (pm25 < 250.4) {
nowcast_2_5 = (300.0 - 201.0) / (250.4 - 150.5) * (pm25 - 150.5) + 201.0;
} else if (pm25 < 350.4) {
nowcast_2_5 = (400.0 - 301.0) / (350.4 - 250.5) * (pm25 - 250.5) + 301.0;
} else if (pm25 < 500.4) {
nowcast_2_5 = (500.0 - 401.0) / (500.4 - 350.5) * (pm25 - 350.5) + 401.0;
} else {
// everything higher is just counted as 500
nowcast_2_5 = 500;
}
} else {
ESP_LOGD("custom", "Skipping pm2.5 NowCast calculation. %d hours remaining.", hours_to_wait - size_2_5);
}
int nowcast_10_0 = -1;
int size_10_0 = id(pm_10_0_hourly_avg).size();
if (size_10_0 > 12) {
size_10_0 = 12;
}
if (size_10_0 >= hours_to_wait) {
// if (size_10_0 >= 12) {
// Calculate min and max
float max = 0.0;
float min = 31337.0; // just a random large number
for (int i = 0; i < size_10_0; i++) {
float pm = id(pm_10_0_hourly_avg)[i];
if (pm < min) {
min = pm;
}
if (pm > max) {
max = pm;
}
}
// Calculate the weight factor
float range = max - min;
float rate = range / max;
float weight_factor = 1.0 - range;
if (weight_factor < 0.5) {
weight_factor = 0.5;
} else if (weight_factor > 1.0) {
weight_factor = 1.0;
}
float pm_sum = 0.0;
float weight_sum = 0.0;
for (int i = 0; i < size_10_0; i++) {
float weight_pow = pow(weight_factor, i);
pm_sum += id(pm_10_0_hourly_avg)[i] * weight_pow;
weight_sum += weight_pow;
}
float pm10 = pm_sum / weight_sum;
// https://en.wikipedia.org/wiki/Air_quality_index#Computing_the_AQI
if (pm10 < 54.0) {
nowcast_10_0 = (50.0 - 0.0) / (54.0 - 0.0) * (pm10 - 0.0) + 0.0;
} else if (pm10 < 154.0) {
nowcast_10_0 = (100.0 - 51.0) / (154.0 - 55.0) * (pm10 - 55.0) + 51.0;
} else if (pm10 < 254.0) {
nowcast_10_0 = (150.0 - 101.0) / (254.0 - 155.0) * (pm10 - 155.0) + 101.0;
} else if (pm10 < 354.0) {
nowcast_10_0 = (200.0 - 151.0) / (354.0 - 255.0) * (pm10 - 255.0) + 151.0;
} else if (pm10 < 424.0) {
nowcast_10_0 = (300.0 - 201.0) / (424.0 - 355.0) * (pm10 - 355.0) + 201.0;
} else if (pm10 < 504.0) {
nowcast_10_0 = (400.0 - 301.0) / (504.0 - 425.0) * (pm10 - 425.0) + 301.0;
} else if (pm10 < 604) {
nowcast_10_0 = (500.0 - 401.0) / (604.0 - 505.0) * (pm10 - 505.0) + 401.0;
} else {
// everything higher is just counted as 500
nowcast_10_0 = 500.0;
}
} else {
ESP_LOGD("custom", "Skipping pm10 NowCast calculation. %d hours remaining.", hours_to_wait - size_10_0);
}
int nowcast_calc = std::max(nowcast_2_5, nowcast_10_0);
if (nowcast_calc > 0) {
id(nowcast).publish_state(nowcast_calc);
// Just in case we're counting down, make sure we set to zero
if (id(nowcast_delay_mins) > 0) {
id(nowcast_delay_mins) = 0;
id(nowcast_mins_remaining).publish_state(0);
}
// And now publish the category string
if (nowcast_calc <= 50.0) {
id(nowcast_category).publish_state("Good");
} else if (nowcast_calc <= 100.0) {
id(nowcast_category).publish_state("Moderate");
} else if (nowcast_calc <= 150.0) {
id(nowcast_category).publish_state("Unhealthy for Sensitive Groups");
} else if (nowcast_calc <= 200.0) {
id(nowcast_category).publish_state("Unhealthy");
} else if (nowcast_calc <= 300.0) {
id(nowcast_category).publish_state("Very Unhealthy");
} else if (nowcast_calc <= 400.0) {
id(nowcast_category).publish_state("Hazardous");
} else if (nowcast_calc <= 500.0) {
id(nowcast_category).publish_state("Hazardous"); // again
} else {
id(nowcast_category).publish_state("Hazardous"); // and again
}
} else {
int remaining_hours = hours_to_wait - std::max(size_2_5, size_10_0);
ESP_LOGD("custom", "No NowCast calculations available. %d hours remaining.", remaining_hours);
// Intervals don't quite overlap with the sensor notification windows,
// so let's readjust this just in case it's fallen out of sync.
id(nowcast_delay_mins) = 60 * remaining_hours;
id(nowcast_mins_remaining).publish_state(60 * remaining_hours);
}
interval:
# Reset hardware watchdog. This varies between hardware. 3min should be plenty.
- interval: 3min
then:
- output.turn_on: watchdog
- delay: 20ms
- output.turn_off: watchdog
# Send data to AirGradient API server
# for more details have a look at sendToServer() function:
# https://www.airgradient.com/open-airgradient/blog/airgradient-diy-pro-instructions/
- interval: 5min
then:
- http_request.post:
# AirGradient URL with full MAC address in Hex format all lower case
url: !lambda |
return "http://hw.airgradient.com/sensors/airgradient:" + get_mac_address() + "/measures";
headers:
Content-Type: application/json
# "!lambda return to_string(id(pm2).state);" Converts sensor output from double to string
# Note: can't use the built-in json encoder here because it does not support nested objects.
body: !lambda |
String jsonString;
StaticJsonDocument<1024> doc;
doc["wifi"] = id(airgradient_wifi_signal).state;
doc["pm01"] = to_string(id(pm_1_0).state);
doc["pm02"] = to_string(id(pm_2_5).state);
doc["pm10"] = to_string(id(pm_10_0).state);
doc["pm003_count"] = to_string(id(pm_0_3um).state);
doc["atmp"] = to_string(id(temperature).state);
doc["rhum"] = to_string(id(humidity).state);
// We don't have access to the boot loop counter in esphome, so just send a 1
// See: https://github.com/esphome/issues/issues/1539
doc["boot"] = "1";
JsonObject channels = doc.createNestedObject("channels");
JsonObject channels_1 = channels.createNestedObject("1");
channels_1["pm01"] = to_string(id(pm1_1_0).state);
channels_1["pm02"] = to_string(id(pm1_2_5).state);
channels_1["pm10"] = to_string(id(pm1_10_0).state);
channels_1["pm003_count"] = to_string(id(pm1_0_3um).state);
channels_1["atmp"] = to_string(id(pm1_temperature).state);
channels_1["rhum"] = to_string(id(pm1_humidity).state);
JsonObject channels_2 = channels.createNestedObject("2");
channels_2["pm01"] = to_string(id(pm2_1_0).state);
channels_2["pm02"] = to_string(id(pm2_2_5).state);
channels_2["pm10"] = to_string(id(pm2_10_0).state);
channels_2["pm003_count"] = to_string(id(pm2_0_3um).state);
channels_2["atmp"] = to_string(id(pm2_temperature).state);
channels_2["rhum"] = to_string(id(pm2_humidity).state);
// Serialize the JSON document into the string
serializeJson(doc, jsonString);
// Convert String to std::string
std::string stdJsonString(jsonString.c_str());
return stdJsonString;
# Decrement the AQI countdowns once per minute until they reach zero
- interval: 1min
then:
- lambda: |
if (id(aqi_delay_mins) > 0) {
id(aqi_delay_mins) -= 1;
id(aqi_mins_remaining).publish_state(id(aqi_delay_mins));
}
if (id(nowcast_delay_mins) > 0) {
id(nowcast_delay_mins) -= 1;
id(nowcast_mins_remaining).publish_state(id(nowcast_delay_mins));
}
light:
- platform: status_led
name: "Status LED"
pin: GPIO10
binary_sensor:
- platform: gpio
name: "Button"
pin:
number: GPIO9
inverted: true
mode:
input: true
pullup: true
on_click:
min_length: 5s
max_length: 30s
then:
- button.press: button_restart
i2c:
sda: GPIO7 # Pin 21
scl: GPIO6 # Pin 20
scan: false
uart:
- id: uart_pm1
baud_rate: 9600
rx_pin: GPIO20 # Pin 30 / RXD0
tx_pin: GPIO21 # Pin 31 / TXD0
- id: uart_pm2
baud_rate: 9600
rx_pin: GPIO0 # Pin 12
tx_pin: GPIO1 # Pin 13
text_sensor:
- platform: template
id: aqi_category
name: "${upper_devicename} AQI Category"
icon: "mdi:weather-windy-variant"
update_interval: 15min
- platform: template
id: nowcast_category
name: "${upper_devicename} NowCast Category"
icon: "mdi:weather-windy-variant"
update_interval: 15min
sensor:
- platform: pmsx003
type: PMS5003T
uart_id: uart_pm1
update_interval: $pm_update_interval
pm_1_0:
id: pm1_1_0
name: "${upper_devicename} Particulate Matter <1.0µm Concentration (1)"
disabled_by_default: true
pm_2_5:
id: pm1_2_5
name: "${upper_devicename} Particulate Matter <2.5µm Concentration (1)"
disabled_by_default: true
pm_10_0:
id: pm1_10_0
name: "${upper_devicename} Particulate Matter <10.0µm Concentration (1)"
disabled_by_default: true
pm_0_3um:
id: pm1_0_3um
name: "${upper_devicename} Particulate Matter >0.3µm Count (1)"
disabled_by_default: true
pm_0_5um:
id: pm1_0_5um
name: "${upper_devicename} Particulate Matter >0.5µm Count (1)"
disabled_by_default: true
pm_1_0um:
id: pm1_1_0um
name: "${upper_devicename} Particulate Matter >1.0µm Count (1)"
disabled_by_default: true
pm_2_5um:
id: pm1_2_5um
name: "${upper_devicename} Particulate Matter >2.5µm Count (1)"
disabled_by_default: true
temperature:
id: pm1_temperature
name: "${upper_devicename} Temperature (1)"
disabled_by_default: true
humidity:
id: pm1_humidity
accuracy_decimals: 1
name: "${upper_devicename} Relative Humidity (1)"
disabled_by_default: true
- platform: pmsx003
type: PMS5003T
uart_id: uart_pm2
update_interval: $pm_update_interval
pm_1_0:
id: pm2_1_0
name: "${upper_devicename} Particulate Matter <1.0µm Concentration (2)"
disabled_by_default: true
pm_2_5:
id: pm2_2_5
name: "${upper_devicename} Particulate Matter <2.5µm Concentration (2)"
disabled_by_default: true
pm_10_0:
id: pm2_10_0
name: "${upper_devicename} Particulate Matter <10.0µm Concentration (2)"
disabled_by_default: true
pm_0_3um:
id: pm2_0_3um
name: "${upper_devicename} Particulate Matter >0.3µm Count (2)"
disabled_by_default: true
pm_0_5um:
id: pm2_0_5um
name: "${upper_devicename} Particulate Matter >0.5µm Count (2)"
disabled_by_default: true
pm_1_0um:
id: pm2_1_0um
name: "${upper_devicename} Particulate Matter >1.0µm Count (2)"
disabled_by_default: true
pm_2_5um:
id: pm2_2_5um
name: "${upper_devicename} Particulate Matter >2.5µm Count (2)"
disabled_by_default: true
temperature:
id: pm2_temperature
name: "${upper_devicename} Temperature (2)"
disabled_by_default: true
humidity:
id: pm2_humidity
accuracy_decimals: 1
name: "${upper_devicename} Relative Humidity (2)"
disabled_by_default: true
# Calculate the average values across both PMS5003T sensors
- platform: template
id: temperature
name: "${upper_devicename} Temperature"
icon: mdi:thermometer
device_class: temperature
state_class: "measurement"
accuracy_decimals: 1
unit_of_measurement: "°C"
lambda: return (id(pm1_temperature).state + id(pm2_temperature).state) / 2.0;
- platform: template
id: humidity
name: "${upper_devicename} Relative Humidity"
icon: mdi:water-percent
device_class: humidity
state_class: "measurement"
accuracy_decimals: 1
unit_of_measurement: "%"
lambda: return (id(pm1_humidity).state + id(pm2_humidity).state) / 2.0;
- platform: template
id: pm_1_0
name: "${upper_devicename} Particulate Matter <1.0µm Concentration"
icon: mdi:chemical-weapon
device_class: pm1
state_class: "measurement"
accuracy_decimals: 0
unit_of_measurement: µg/m³
update_interval: $pm_update_interval
lambda: return (id(pm1_1_0).state + id(pm2_1_0).state) / 2.0;
- platform: template
id: pm_2_5
name: "${upper_devicename} Particulate Matter <2.5µm Concentration"
icon: mdi:chemical-weapon
device_class: pm25
state_class: "measurement"
accuracy_decimals: 0
unit_of_measurement: µg/m³
update_interval: $pm_update_interval
lambda: return (id(pm1_2_5).state + id(pm2_2_5).state) / 2.0;
- platform: template
id: pm_10_0
name: "${upper_devicename} Particulate Matter <10.0µm Concentration"
icon: mdi:chemical-weapon
device_class: pm10
state_class: "measurement"
accuracy_decimals: 0
unit_of_measurement: µg/m³
update_interval: $pm_update_interval
lambda: return (id(pm1_10_0).state + id(pm2_10_0).state) / 2.0;
- platform: template
id: pm_0_3um
name: "${upper_devicename} Particulate Matter >0.3µm Count"
icon: mdi:blur
accuracy_decimals: 0
state_class: "measurement"
unit_of_measurement: /dL
update_interval: $pm_update_interval
lambda: return (id(pm1_0_3um).state + id(pm2_0_3um).state) / 2.0;
- platform: template
id: pm_0_5um
name: "${upper_devicename} Particulate Matter >0.5µm Count"
icon: mdi:blur
accuracy_decimals: 0
state_class: "measurement"
unit_of_measurement: /dL
update_interval: $pm_update_interval
lambda: return (id(pm1_0_5um).state + id(pm2_0_5um).state) / 2.0;
- platform: template
id: pm_1_0um
name: "${upper_devicename} Particulate Matter >1.0µm Count"
icon: mdi:blur
accuracy_decimals: 0
state_class: "measurement"
unit_of_measurement: /dL
update_interval: $pm_update_interval
lambda: return (id(pm1_1_0um).state + id(pm2_1_0um).state) / 2.0;
- platform: template
id: pm_2_5um
name: "${upper_devicename} Particulate Matter >2.5µm Count"
icon: mdi:blur
accuracy_decimals: 0
state_class: "measurement"
unit_of_measurement: /dL
update_interval: $pm_update_interval
lambda: return (id(pm1_2_5um).state + id(pm2_2_5um).state) / 2.0;
- platform: wifi_signal
id: airgradient_wifi_signal
name: "Wifi Strength"
state_class: "measurement"
update_interval: 1min
- platform: uptime
id: uptime_sensor
name: "Uptime Sensor"
update_interval: 1min
- platform: template
id: aqi
name: "${upper_devicename} AQI"
device_class: aqi
state_class: "measurement"
icon: "mdi:weather-windy-variant"
accuracy_decimals: 0
- platform: template
id: nowcast
name: "${upper_devicename} NowCast"
device_class: aqi
state_class: "measurement"
icon: "mdi:weather-windy-variant"
accuracy_decimals: 0
- platform: template
id: aqi_mins_remaining
name: "AQI: Minutes Remaining"
icon: mdi:timer-sand
accuracy_decimals: 0
unit_of_measurement: minutes
- platform: template
id: nowcast_mins_remaining
name: "NowCast: Minutes Remaining"
icon: mdi:timer-sand
accuracy_decimals: 0
unit_of_measurement: minutes
- platform: copy
source_id: pm_2_5
id: pm_2_5_1h_avg
name: "${upper_devicename} PM <2.5µm 1h Average"
disabled_by_default: true
device_class: pm25
accuracy_decimals: 1
filters:
- sliding_window_moving_average:
window_size: 20 # every 3 minutes = 1 hour
send_every: 20
send_first_at: 20
on_value:
lambda: |
// Insert the current value
float current = id(pm_2_5_1h_avg).state;
if (!isnan(current)) {
id(pm_2_5_hourly_avg).insert(id(pm_2_5_hourly_avg).begin(), current);
// Truncate anything past the first 24
if (id(pm_2_5_hourly_avg).size() > 24) {
id(pm_2_5_hourly_avg).resize(24);
}
}
ESP_LOGD("custom", "pm_2_5_hourly_avg size: %d", id(pm_2_5_hourly_avg).size());
// Trigger the AQI calculations (they'll restart if the pm10 finishes slightly later)
id(calculate_nowcast).execute();
id(calculate_aqi).execute();
- platform: copy
source_id: pm_10_0
id: pm_10_0_1h_avg
name: "${upper_devicename} PM <10.0µm 1h Average"
disabled_by_default: true
device_class: pm10
accuracy_decimals: 1
filters:
- sliding_window_moving_average:
window_size: 20 # every 3 minutes = 1 hour
send_every: 20
send_first_at: 20
on_value:
lambda: |
// Insert the current value
float current = id(pm_10_0_1h_avg).state;
if (!isnan(current)) {
id(pm_10_0_hourly_avg).insert(id(pm_10_0_hourly_avg).begin(), current);
// Truncate anything past the first 24
if (id(pm_10_0_hourly_avg).size() > 24) {
id(pm_10_0_hourly_avg).resize(24);
}
}
ESP_LOGD("custom", "pm_10_0_hourly_avg size: %d", id(pm_10_0_hourly_avg).size());
// Trigger the AQI calculations (they'll restart if the pm2.5 finishes slightly later)
id(calculate_nowcast).execute();
id(calculate_aqi).execute();