From 1f99a6aeb894bd872e5fa4465e7915dbc95280fa Mon Sep 17 00:00:00 2001 From: Joshua Post Date: Mon, 15 Jul 2024 16:36:31 -0500 Subject: [PATCH] Fix possible negative numbers for PM2.5 after correction --- packages/airgradient_d1_mini_board.yaml | 2 +- packages/airgradient_esp32-c3_board.yaml | 2 +- packages/sensor_pms5003.yaml | 18 ++++++++++------ packages/sensor_pms5003_extended_life.yaml | 21 +++++++++++++++++++ packages/sensor_pms5003t.yaml | 19 ++++++++++------- packages/sensor_pms5003t_2.yaml | 19 ++++++++++------- packages/sensor_pms5003t_2_extended_life.yaml | 18 ++++++++++------ packages/sensor_pms5003t_extended_life.yaml | 18 ++++++++++------ 8 files changed, 83 insertions(+), 34 deletions(-) diff --git a/packages/airgradient_d1_mini_board.yaml b/packages/airgradient_d1_mini_board.yaml index 707693e..b2eead4 100644 --- a/packages/airgradient_d1_mini_board.yaml +++ b/packages/airgradient_d1_mini_board.yaml @@ -1,5 +1,5 @@ substitutions: - config_version: 4.0.0 + config_version: 4.0.1 esphome: name: "${name}" diff --git a/packages/airgradient_esp32-c3_board.yaml b/packages/airgradient_esp32-c3_board.yaml index a25e213..a8f9b77 100644 --- a/packages/airgradient_esp32-c3_board.yaml +++ b/packages/airgradient_esp32-c3_board.yaml @@ -1,5 +1,5 @@ substitutions: - config_version: 4.0.0 + config_version: 4.0.1 esphome: name: "${name}" diff --git a/packages/sensor_pms5003.yaml b/packages/sensor_pms5003.yaml index f43c211..585d51c 100644 --- a/packages/sensor_pms5003.yaml +++ b/packages/sensor_pms5003.yaml @@ -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" diff --git a/packages/sensor_pms5003_extended_life.yaml b/packages/sensor_pms5003_extended_life.yaml index 61b4059..e0f19a9 100644 --- a/packages/sensor_pms5003_extended_life.yaml +++ b/packages/sensor_pms5003_extended_life.yaml @@ -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 diff --git a/packages/sensor_pms5003t.yaml b/packages/sensor_pms5003t.yaml index d074761..4acffce 100644 --- a/packages/sensor_pms5003t.yaml +++ b/packages/sensor_pms5003t.yaml @@ -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" diff --git a/packages/sensor_pms5003t_2.yaml b/packages/sensor_pms5003t_2.yaml index d6bee2b..93e1c99 100644 --- a/packages/sensor_pms5003t_2.yaml +++ b/packages/sensor_pms5003t_2.yaml @@ -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 diff --git a/packages/sensor_pms5003t_2_extended_life.yaml b/packages/sensor_pms5003t_2_extended_life.yaml index 46047c3..c867e87 100644 --- a/packages/sensor_pms5003t_2_extended_life.yaml +++ b/packages/sensor_pms5003t_2_extended_life.yaml @@ -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 diff --git a/packages/sensor_pms5003t_extended_life.yaml b/packages/sensor_pms5003t_extended_life.yaml index e7f2cb9..d8619d1 100644 --- a/packages/sensor_pms5003t_extended_life.yaml +++ b/packages/sensor_pms5003t_extended_life.yaml @@ -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; }