Skip to content

Commit

Permalink
Fix possible negative numbers for PM2.5 after correction
Browse files Browse the repository at this point in the history
  • Loading branch information
MallocArray committed Jul 15, 2024
1 parent cb4addd commit 1f99a6a
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/airgradient_d1_mini_board.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
substitutions:
config_version: 4.0.0
config_version: 4.0.1

esphome:
name: "${name}"
Expand Down
2 changes: 1 addition & 1 deletion packages/airgradient_esp32-c3_board.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
substitutions:
config_version: 4.0.0
config_version: 4.0.1

esphome:
name: "${name}"
Expand Down
18 changes: 12 additions & 6 deletions packages/sensor_pms5003.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@ sensor:
device_class: pm25 # Added to report properly to HomeKit
filters:
- lambda: |-
float result = 0.0;
if (x == 0.0) {
return 0.0;
result = 0.0;
} else if (x < 30.0) {
return((0.524 * x) - (0.0862 * id(humidity).state) + 5.75);
result = (0.524 * x) - (0.0862 * id(humidity).state) + 5.75;
} else if (x < 50.0) {
return((0.786 * (x / 20 - 3/2) + 0.524 * (1 - (x / 20 - 3/2))) * x - (0.0862 * id(humidity).state) + 5.75);
result = (0.786 * (x / 20 - 3/2) + 0.524 * (1 - (x / 20 - 3/2))) * x - (0.0862 * id(humidity).state) + 5.75;
} else if (x < 210.0) {
return((0.786 * x) - (0.0862 * id(humidity).state) + 5.75);
result = (0.786 * x) - (0.0862 * id(humidity).state) + 5.75;
} else if (x < 260.0) {
return((0.69 * (x / 50 - 21/5) + 0.786 * (1 - (x / 50 - 21/5))) * x - (0.0862 * id(humidity).state * (1 - (x / 50 - 21/5))) + (2.966 * (x / 50 - 21/5)) + (5.75 * (1 - (x / 50 - 21/5))) + (8.84 * pow(10,-4) * pow(x,2) * (x / 50 - 21/5)));
result = (0.69 * (x / 50 - 21/5) + 0.786 * (1 - (x / 50 - 21/5))) * x - (0.0862 * id(humidity).state * (1 - (x / 50 - 21/5))) + (2.966 * (x / 50 - 21/5)) + (5.75 * (1 - (x / 50 - 21/5))) + (8.84 * pow(10,-4) * pow(x,2) * (x / 50 - 21/5));
} else {
return(2.966 + (0.69 * x) + (8.84 * pow(10,-4) * pow(x,2)));
result = 2.966 + (0.69 * x) + (8.84 * pow(10,-4) * pow(x,2));
}
if (result <= 0.0) {
return 0.0;
} else {
return result;
}
pm_1_0:
name: "PM 1.0"
Expand Down
21 changes: 21 additions & 0 deletions packages/sensor_pms5003_extended_life.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ sensor:
name: "PM 2.5"
id: pm_2_5
device_class: pm25 # Added to report properly to HomeKit
filters:
- lambda: |-
float result = 0.0;
if (x == 0.0) {
result = 0.0;
} else if (x < 30.0) {
result = (0.524 * x) - (0.0862 * id(humidity).state) + 5.75;
} else if (x < 50.0) {
result = (0.786 * (x / 20 - 3/2) + 0.524 * (1 - (x / 20 - 3/2))) * x - (0.0862 * id(humidity).state) + 5.75;
} else if (x < 210.0) {
result = (0.786 * x) - (0.0862 * id(humidity).state) + 5.75;
} else if (x < 260.0) {
result = (0.69 * (x / 50 - 21/5) + 0.786 * (1 - (x / 50 - 21/5))) * x - (0.0862 * id(humidity).state * (1 - (x / 50 - 21/5))) + (2.966 * (x / 50 - 21/5)) + (5.75 * (1 - (x / 50 - 21/5))) + (8.84 * pow(10,-4) * pow(x,2) * (x / 50 - 21/5));
} else {
result = 2.966 + (0.69 * x) + (8.84 * pow(10,-4) * pow(x,2));
}
if (result <= 0.0) {
return 0.0;
} else {
return result;
}
pm_1_0:
name: "PM 1.0"
id: pm_1_0
Expand Down
19 changes: 12 additions & 7 deletions packages/sensor_pms5003t.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,25 @@ sensor:
accuracy_decimals: 0
state_class: measurement
lambda: |-
float result = 0.0;
if (id(pm_2_5_raw).state == 0.0) {
return 0.0;
result = 0.0;
} else if (id(pm_2_5_raw).state < 30.0) {
return((0.524 * id(pm_2_5_raw).state) - (0.0862 * id(humidity).state) + 5.75);
result = (0.524 * id(pm_2_5_raw).state) - (0.0862 * id(humidity).state) + 5.75;
} else if (id(pm_2_5_raw).state < 50.0) {
return((0.786 * (id(pm_2_5_raw).state / 20 - 3/2) + 0.524 * (1 - (id(pm_2_5_raw).state / 20 - 3/2))) * id(pm_2_5_raw).state - (0.0862 * id(humidity).state) + 5.75);
result = (0.786 * (id(pm_2_5_raw).state / 20 - 3/2) + 0.524 * (1 - (id(pm_2_5_raw).state / 20 - 3/2))) * id(pm_2_5_raw).state - (0.0862 * id(humidity).state) + 5.75;
} else if (id(pm_2_5_raw).state < 210.0) {
return((0.786 * id(pm_2_5_raw).state) - (0.0862 * id(humidity).state) + 5.75);
result = (0.786 * id(pm_2_5_raw).state) - (0.0862 * id(humidity).state) + 5.75;
} else if (id(pm_2_5_raw).state < 260.0) {
return((0.69 * (id(pm_2_5_raw).state / 50 - 21/5) + 0.786 * (1 - (id(pm_2_5_raw).state / 50 - 21/5))) * id(pm_2_5_raw).state - (0.0862 * id(humidity).state * (1 - (id(pm_2_5_raw).state / 50 - 21/5))) + (2.966 * (id(pm_2_5_raw).state / 50 - 21/5)) + (5.75 * (1 - (id(pm_2_5_raw).state / 50 - 21/5))) + (8.84 * pow(10,-4) * pow(id(pm_2_5_raw).state,2) * (id(pm_2_5_raw).state / 50 - 21/5)));
result = (0.69 * (id(pm_2_5_raw).state / 50 - 21/5) + 0.786 * (1 - (id(pm_2_5_raw).state / 50 - 21/5))) * id(pm_2_5_raw).state - (0.0862 * id(humidity).state * (1 - (id(pm_2_5_raw).state / 50 - 21/5))) + (2.966 * (id(pm_2_5_raw).state / 50 - 21/5)) + (5.75 * (1 - (id(pm_2_5_raw).state / 50 - 21/5))) + (8.84 * pow(10,-4) * pow(id(pm_2_5_raw).state,2) * (id(pm_2_5_raw).state / 50 - 21/5));
} else {
return(2.966 + (0.69 * id(pm_2_5_raw).state) + (8.84 * pow(10,-4) * pow(id(pm_2_5_raw).state,2)));
result = 2.966 + (0.69 * id(pm_2_5_raw).state) + (8.84 * pow(10,-4) * pow(id(pm_2_5_raw).state,2));
}
if (result <= 0.0) {
return 0.0;
} else {
return result;
}
- platform: template
# Depends on another sensor providing an ID of pm_2_5 such as a pms5003
name: "PM 2.5 AQI"
Expand Down
19 changes: 12 additions & 7 deletions packages/sensor_pms5003t_2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,25 @@ sensor:
accuracy_decimals: 0
state_class: measurement
lambda: |-
float result = 0.0;
if (id(pm_2_5_2_raw).state == 0.0) {
return 0.0;
result = 0.0;
} else if (id(pm_2_5_2_raw).state < 30.0) {
return((0.524 * id(pm_2_5_2_raw).state) - (0.0862 * id(humidity).state) + 5.75);
result = (0.524 * id(pm_2_5_2_raw).state) - (0.0862 * id(humidity).state) + 5.75;
} else if (id(pm_2_5_2_raw).state < 50.0) {
return((0.786 * (id(pm_2_5_2_raw).state / 20 - 3/2) + 0.524 * (1 - (id(pm_2_5_2_raw).state / 20 - 3/2))) * id(pm_2_5_2_raw).state - (0.0862 * id(humidity).state) + 5.75);
result = (0.786 * (id(pm_2_5_2_raw).state / 20 - 3/2) + 0.524 * (1 - (id(pm_2_5_2_raw).state / 20 - 3/2))) * id(pm_2_5_2_raw).state - (0.0862 * id(humidity).state) + 5.75;
} else if (id(pm_2_5_2_raw).state < 210.0) {
return((0.786 * id(pm_2_5_2_raw).state) - (0.0862 * id(humidity).state) + 5.75);
result = (0.786 * id(pm_2_5_2_raw).state) - (0.0862 * id(humidity).state) + 5.75;
} else if (id(pm_2_5_2_raw).state < 260.0) {
return((0.69 * (id(pm_2_5_2_raw).state / 50 - 21/5) + 0.786 * (1 - (id(pm_2_5_2_raw).state / 50 - 21/5))) * id(pm_2_5_2_raw).state - (0.0862 * id(humidity).state * (1 - (id(pm_2_5_2_raw).state / 50 - 21/5))) + (2.966 * (id(pm_2_5_2_raw).state / 50 - 21/5)) + (5.75 * (1 - (id(pm_2_5_2_raw).state / 50 - 21/5))) + (8.84 * pow(10,-4) * pow(id(pm_2_5_2_raw).state,2) * (id(pm_2_5_2_raw).state / 50 - 21/5)));
result = (0.69 * (id(pm_2_5_2_raw).state / 50 - 21/5) + 0.786 * (1 - (id(pm_2_5_2_raw).state / 50 - 21/5))) * id(pm_2_5_2_raw).state - (0.0862 * id(humidity).state * (1 - (id(pm_2_5_2_raw).state / 50 - 21/5))) + (2.966 * (id(pm_2_5_2_raw).state / 50 - 21/5)) + (5.75 * (1 - (id(pm_2_5_2_raw).state / 50 - 21/5))) + (8.84 * pow(10,-4) * pow(id(pm_2_5_2_raw).state,2) * (id(pm_2_5_2_raw).state / 50 - 21/5));
} else {
return(2.966 + (0.69 * id(pm_2_5_2_raw).state) + (8.84 * pow(10,-4) * pow(id(pm_2_5_2_raw).state,2)));
result = 2.966 + (0.69 * id(pm_2_5_2_raw).state) + (8.84 * pow(10,-4) * pow(id(pm_2_5_2_raw).state,2));
}
if (result <= 0.0) {
return 0.0;
} else {
return result;
}
# Average the readings from both sensors to return a value
- platform: template
Expand Down
18 changes: 12 additions & 6 deletions packages/sensor_pms5003t_2_extended_life.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,24 @@ sensor:
accuracy_decimals: 0
state_class: measurement
lambda: |-
float result = 0.0;
if (id(pm_2_5_2_raw).state == 0.0) {
return 0.0;
result = 0.0;
} else if (id(pm_2_5_2_raw).state < 30.0) {
return((0.524 * id(pm_2_5_2_raw).state) - (0.0862 * id(humidity).state) + 5.75);
result = (0.524 * id(pm_2_5_2_raw).state) - (0.0862 * id(humidity).state) + 5.75;
} else if (id(pm_2_5_2_raw).state < 50.0) {
return((0.786 * (id(pm_2_5_2_raw).state / 20 - 3/2) + 0.524 * (1 - (id(pm_2_5_2_raw).state / 20 - 3/2))) * id(pm_2_5_2_raw).state - (0.0862 * id(humidity).state) + 5.75);
result = (0.786 * (id(pm_2_5_2_raw).state / 20 - 3/2) + 0.524 * (1 - (id(pm_2_5_2_raw).state / 20 - 3/2))) * id(pm_2_5_2_raw).state - (0.0862 * id(humidity).state) + 5.75;
} else if (id(pm_2_5_2_raw).state < 210.0) {
return((0.786 * id(pm_2_5_2_raw).state) - (0.0862 * id(humidity).state) + 5.75);
result = (0.786 * id(pm_2_5_2_raw).state) - (0.0862 * id(humidity).state) + 5.75;
} else if (id(pm_2_5_2_raw).state < 260.0) {
return((0.69 * (id(pm_2_5_2_raw).state / 50 - 21/5) + 0.786 * (1 - (id(pm_2_5_2_raw).state / 50 - 21/5))) * id(pm_2_5_2_raw).state - (0.0862 * id(humidity).state * (1 - (id(pm_2_5_2_raw).state / 50 - 21/5))) + (2.966 * (id(pm_2_5_2_raw).state / 50 - 21/5)) + (5.75 * (1 - (id(pm_2_5_2_raw).state / 50 - 21/5))) + (8.84 * pow(10,-4) * pow(id(pm_2_5_2_raw).state,2) * (id(pm_2_5_2_raw).state / 50 - 21/5)));
result = (0.69 * (id(pm_2_5_2_raw).state / 50 - 21/5) + 0.786 * (1 - (id(pm_2_5_2_raw).state / 50 - 21/5))) * id(pm_2_5_2_raw).state - (0.0862 * id(humidity).state * (1 - (id(pm_2_5_2_raw).state / 50 - 21/5))) + (2.966 * (id(pm_2_5_2_raw).state / 50 - 21/5)) + (5.75 * (1 - (id(pm_2_5_2_raw).state / 50 - 21/5))) + (8.84 * pow(10,-4) * pow(id(pm_2_5_2_raw).state,2) * (id(pm_2_5_2_raw).state / 50 - 21/5));
} else {
return(2.966 + (0.69 * id(pm_2_5_2_raw).state) + (8.84 * pow(10,-4) * pow(id(pm_2_5_2_raw).state,2)));
result = 2.966 + (0.69 * id(pm_2_5_2_raw).state) + (8.84 * pow(10,-4) * pow(id(pm_2_5_2_raw).state,2));
}
if (result <= 0.0) {
return 0.0;
} else {
return result;
}
# Average the readings from both sensors to return a value
Expand Down
18 changes: 12 additions & 6 deletions packages/sensor_pms5003t_extended_life.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,24 @@ sensor:
accuracy_decimals: 0
state_class: measurement
lambda: |-
float result = 0.0;
if (id(pm_2_5_raw).state == 0.0) {
return 0.0;
result = 0.0;
} else if (id(pm_2_5_raw).state < 30.0) {
return((0.524 * id(pm_2_5_raw).state) - (0.0862 * id(humidity).state) + 5.75);
result = (0.524 * id(pm_2_5_raw).state) - (0.0862 * id(humidity).state) + 5.75;
} else if (id(pm_2_5_raw).state < 50.0) {
return((0.786 * (id(pm_2_5_raw).state / 20 - 3/2) + 0.524 * (1 - (id(pm_2_5_raw).state / 20 - 3/2))) * id(pm_2_5_raw).state - (0.0862 * id(humidity).state) + 5.75);
result = (0.786 * (id(pm_2_5_raw).state / 20 - 3/2) + 0.524 * (1 - (id(pm_2_5_raw).state / 20 - 3/2))) * id(pm_2_5_raw).state - (0.0862 * id(humidity).state) + 5.75;
} else if (id(pm_2_5_raw).state < 210.0) {
return((0.786 * id(pm_2_5_raw).state) - (0.0862 * id(humidity).state) + 5.75);
result = (0.786 * id(pm_2_5_raw).state) - (0.0862 * id(humidity).state) + 5.75;
} else if (id(pm_2_5_raw).state < 260.0) {
return((0.69 * (id(pm_2_5_raw).state / 50 - 21/5) + 0.786 * (1 - (id(pm_2_5_raw).state / 50 - 21/5))) * id(pm_2_5_raw).state - (0.0862 * id(humidity).state * (1 - (id(pm_2_5_raw).state / 50 - 21/5))) + (2.966 * (id(pm_2_5_raw).state / 50 - 21/5)) + (5.75 * (1 - (id(pm_2_5_raw).state / 50 - 21/5))) + (8.84 * pow(10,-4) * pow(id(pm_2_5_raw).state,2) * (id(pm_2_5_raw).state / 50 - 21/5)));
result = (0.69 * (id(pm_2_5_raw).state / 50 - 21/5) + 0.786 * (1 - (id(pm_2_5_raw).state / 50 - 21/5))) * id(pm_2_5_raw).state - (0.0862 * id(humidity).state * (1 - (id(pm_2_5_raw).state / 50 - 21/5))) + (2.966 * (id(pm_2_5_raw).state / 50 - 21/5)) + (5.75 * (1 - (id(pm_2_5_raw).state / 50 - 21/5))) + (8.84 * pow(10,-4) * pow(id(pm_2_5_raw).state,2) * (id(pm_2_5_raw).state / 50 - 21/5));
} else {
return(2.966 + (0.69 * id(pm_2_5_raw).state) + (8.84 * pow(10,-4) * pow(id(pm_2_5_raw).state,2)));
result = 2.966 + (0.69 * id(pm_2_5_raw).state) + (8.84 * pow(10,-4) * pow(id(pm_2_5_raw).state,2));
}
if (result <= 0.0) {
return 0.0;
} else {
return result;
}
Expand Down

0 comments on commit 1f99a6a

Please sign in to comment.