substitutions: name: apollo-air-1 version: "26.11.17.1" device_description: ${name} made by Apollo Automation - version ${version}. # Reduced DB Reporting thresholds reduced_reporting_heartbeat_ms: "3600000UL" temperature_reporting_delta: "0.2" humidity_reporting_delta: "2.0" pressure_reporting_delta: "0.5" pm_reporting_delta: "0.5" voc_reporting_delta: "5.0" co2_reporting_delta: "10.0" co_reporting_delta: "1.0" nitrogen_dioxide_reporting_delta: "0.05" hydrogen_reporting_delta: "0.1" ethanol_reporting_delta: "0.5" methane_reporting_delta: "100.0" ammonia_reporting_delta: "0.1" # Sensor decimal places temperature_decimal_places: "1" humidity_decimal_places: "1" pressure_decimal_places: "1" pm_decimal_places: "1" voc_decimal_places: "0" aqi_decimal_places: "0" co2_decimal_places: "0" co_decimal_places: "1" nox_decimal_places: "0" nitrogen_dioxide_decimal_places: "2" hydrogen_decimal_places: "1" ethanol_decimal_places: "1" methane_decimal_places: "0" ammonia_decimal_places: "1" # Default OTA password. Override in your device YAML by re-declaring # `substitutions: { ota_password: !secret _ota_password }` so each # device on your network uses a unique secret instead of the shared default. ota_password: "apolloautomation" # To use OTA encryption instead of the password, modify the packaged # ESPHome OTA entry (id: ota_esphome) from your own device YAML instead of # adding a second one. The OTA key is taken from a static # `api: encryption: key:` in your device YAML (a key provisioned at runtime # cannot be reused; set `ota` `encryption: key:` explicitly in that case). # This takes two flashes: # # # Flash 1: keep the password so the running firmware's password prompt # # can still be answered, and allow this one plaintext upload. # ota: # - id: !extend ota_esphome # encryption: # allow_plaintext_upload: true # # # Flash 2: the device now accepts encrypted uploads; drop the password # # and the plaintext allowance. # ota: # - id: !extend ota_esphome # password: !remove # encryption: {} # # See https://esphome.io/components/ota/esphome/ # Manifest URL bases. Stable = GitHub Pages (main branch builds). # Beta = rolling "beta-fw" pre-release assets (beta branch builds). stable_manifest_base: "https://apolloautomation.github.io/AIR-1" beta_manifest_base: "https://github.com/ApolloAutomation/AIR-1/releases/download/beta-fw" # Per-variant OTA manifest URLs. Core defaults to the unified (standard) # image; AIR-1_BLE.yaml overrides these so legacy BLE devices stay on the # firmware-ble manifest. ota_stable_manifest: "${stable_manifest_base}/firmware/manifest.json" ota_beta_manifest: "${beta_manifest_base}/manifest-standard.json" # Bluetooth Proxy switch state on a device with no stored preference yet # (first boot after this switch was added, or after a factory reset). The # unified image ships the proxy off; AIR-1_BLE.yaml overrides this to ON so # legacy BLE devices keep proxying instead of going quiet after an update. bluetooth_proxy_restore_mode: RESTORE_DEFAULT_OFF # Keep the BLE stack powered even while the Bluetooth Proxy switch is off. # Only the Factory image sets this to "true", because its Improv-over-BLE # provisioning needs the radio regardless of the proxy switch. Everywhere # else the radio is powered down completely when the proxy is off. ble_always_on: "false" esphome: # List form so package merging concatenates with each variant's own on_boot # entries (mapping form would be replaced by the variant's block instead). on_boot: - priority: -100 then: - script.execute: apply_ota_source # Re-apply the Bluetooth Proxy switch after all components set up (also # on every wake from deep sleep), so the BLE radio matches the switch # state restored from flash: powered and scanning when on, powered down # when off. - priority: -300 then: - if: condition: switch.is_on: bluetooth_proxy_switch then: - script.execute: bluetooth_proxy_on else: - script.execute: bluetooth_proxy_off packages: # Physics-based Estimated Room Temperature/Humidity sensors and their # tuning CONFIG numbers. Everything in it is disabled by default. predicted_room: !include Predicted_Room.yaml esp32: variant: esp32c3 flash_size: 4MB framework: type: esp-idf # The BLE stack starts powered down. The Bluetooth Proxy switch powers it up # (and starts scanning) only when the proxy is wanted, so a unit with the # proxy off does not carry the radio's power draw and self-heating. esp32_ble: id: ble enable_on_boot: ${ble_always_on} esp32_ble_tracker: id: ble_tracker scan_parameters: continuous: true bluetooth_proxy: http_request: verify_ssl: true # GitHub release-asset downloads answer with a redirect carrying a # ~3.6 KB Content-Security-Policy header; each header line must fit # this buffer or the request fails with "HTTP_CLIENT: Out of buffer". buffer_size_rx: 5120 # The redirect target is a signed URL with a ~850-char query string; the # follow-up request line must fit the TX buffer or esp_http_client_open # fails with "Out of buffer" before sending anything. buffer_size_tx: 2048 api: encryption: on_client_connected: - delay: 90s - if: condition: or: - binary_sensor.is_on: ota_mode - switch.is_on: prevent_sleep then: - lambda: |- ESP_LOGW("Apollo", "Preventing Deep Sleep Due To OTA Or Switch"); id(deep_sleep_1).prevent_deep_sleep(); else: - lambda: |- id(reportAllValues).execute(); - deep_sleep.enter: id: deep_sleep_1 actions: #Co2 Calibration Action - action: calibrate_co2_value variables: co2_ppm: float then: - scd4x.perform_forced_calibration: value: !lambda "return co2_ppm;" id: scd40 - action: sen55_clean then: - sen5x.start_fan_autoclean: sen55 reboot_timeout: 0s captive_portal: web_server: port: 80 version: 3 # No firmware upload from the device page; the setup hotspot still allows it ota: false globals: - id: cycleCounter type: int restore_value: no initial_value: "0" - id: button_press_timestamp restore_value: no type: uint32_t initial_value: "0" - id: runTest restore_value: yes type: bool initial_value: "true" - id: testCycleCount type: int restore_value: no initial_value: "0" - id: dps310Passed restore_value: no type: bool initial_value: "false" # The last four CO2 readings, newest first, for the "Calibrate CO2" button's check, and when they came (millis()) - id: co2_recent type: std::array restore_value: no initial_value: std::array{NAN, NAN, NAN, NAN} - id: co2_recent_ms type: std::array restore_value: no initial_value: std::array{} # How far co2_forced_calibration got: 0 the measurement did not stop, 1 stopped, 2 target sent, 3 target taken, # 4 target refused (the sensor answered 0xFFFF) - id: co2_frc_step type: int restore_value: no initial_value: "0" # Whether the CO2 measurement runs again after co2_forced_calibration - id: co2_measuring type: bool restore_value: no initial_value: "true" i2c: sda: GPIO1 scl: GPIO0 id: bus_a deep_sleep: id: deep_sleep_1 sleep_duration: 5min run_duration: 2min number: - platform: template name: "Air Quality LED Brightness" id: air_quality_led_brightness icon: mdi:brightness-6 optimistic: true restore_value: true entity_category: "config" unit_of_measurement: "%" min_value: 5 max_value: 100 step: 1 initial_value: 100 mode: slider on_value: then: - script.execute: update_air_quality_led - platform: template name: SEN55 Temperature Offset id: sen55_temperature_offset restore_value: true initial_value: 6.0 min_value: -70.0 max_value: 70.0 entity_category: "CONFIG" unit_of_measurement: "°C" optimistic: true update_interval: never step: 0.1 mode: box - platform: template name: SEN55 Humidity Offset id: sen55_humidity_offset restore_value: true initial_value: 0 min_value: -70.0 max_value: 70.0 entity_category: "CONFIG" unit_of_measurement: "%" optimistic: true update_interval: never step: 0.1 mode: box - platform: template name: "DPS310 Pressure Offset" id: dps310_pressure_offset disabled_by_default: true restore_value: true initial_value: 0.0 min_value: -100.0 max_value: 100.0 entity_category: "CONFIG" device_class: "pressure" unit_of_measurement: "hPa" optimistic: true update_interval: never step: 0.1 mode: box - platform: template name: "CO2 Calibration Target" id: co2_calibration_target icon: mdi:molecule-co2 restore_value: true initial_value: 420 min_value: 400 max_value: 2000 entity_category: "CONFIG" unit_of_measurement: "ppm" optimistic: true update_interval: never step: 1 mode: box - platform: template name: "Sleep Duration" id: deep_sleep_sleep_duration min_value: 0 max_value: 800 step: 1 mode: box update_interval: never optimistic: true restore_value: true initial_value: 5 icon: "mdi:arrow-collapse-right" entity_category: CONFIG unit_of_measurement: "min" on_value: then: - lambda: |- id(deep_sleep_1).set_sleep_duration(x * 60 * 1000); binary_sensor: - platform: status name: Online id: ink_ha_connected - platform: gpio pin: number: GPIO9 inverted: true mode: input: true pullup: true ignore_strapping_warning: true id: reset_button on_press: then: - lambda: |- id(button_press_timestamp) = millis(); on_release: then: - if: condition: lambda: "return millis() - id(button_press_timestamp) >= 10000;" then: - lambda: |- id(prevent_sleep).turn_on(); id(factory_reset_switch).turn_on(); else: - if: condition: lambda: "return millis() - id(button_press_timestamp) >= 3000;" then: - lambda: |- //Turn Prevent Sleep On id(prevent_sleep).turn_on(); //Prevent Sleep id(deep_sleep_1).prevent_deep_sleep(); else: # StatusCheck, then the self-test. Wait for statusCheck so the # self-test colour doesn't cut off the status colour, and use # a delay action: a blocking delay() would stall the main loop. - script.execute: statusCheck - script.wait: statusCheck - delay: 3s - lambda: |- id(testCycleCount) = 0; id(runTest) = true; - script.execute: testScript - platform: homeassistant name: "OTA Mode" id: ota_mode entity_id: input_boolean.apollo_ota_mode on_press: then: - lambda: |- id(deep_sleep_1).prevent_deep_sleep(); on_release: then: - if: condition: switch.is_off: prevent_sleep then: - lambda: |- id(deep_sleep_1).allow_deep_sleep(); sensor: - platform: internal_temperature name: "ESP Temperature" id: sys_esp_temperature device_class: "temperature" - platform: uptime name: Uptime id: sys_uptime update_interval: 60s - platform: wifi_signal name: RSSI id: wifi_signal_db device_class: "signal_strength" update_interval: 60s entity_category: "diagnostic" - platform: scd4x id: scd40 automatic_self_calibration: true update_interval: 60s measurement_mode: "periodic" i2c_id: bus_a ambient_pressure_compensation_source: dps310pressure co2: name: "CO2" id: "co2" device_class: "carbon_dioxide" accuracy_decimals: ${co2_decimal_places} filters: - !include { file: reduce_filter.yaml, vars: { delta: "${co2_reporting_delta}" } } on_value: - if: condition: lambda: 'return id(air_quality_led_source).current_option() == "CO2";' then: - script.execute: update_air_quality_led # Raw readings, before the Reduce DB Reporting filter: the "Calibrate CO2" button needs every 60 s reading on_raw_value: - lambda: |- auto &r = id(co2_recent); auto &t = id(co2_recent_ms); uint32_t now = millis(); // A reading every 60 s; a gap of more than 90 s (a missed reading) breaks the steady window if (!std::isnan(r[0]) && now - t[0] > 90000) r.fill(NAN); for (int i = 3; i > 0; i--) { r[i] = r[i - 1]; t[i] = t[i - 1]; } r[0] = x; t[0] = now; - platform: dps310 id: dps_310 i2c_id: bus_a update_interval: 30s pressure: name: "DPS310 Pressure" id: dps310pressure device_class: "atmospheric_pressure" accuracy_decimals: ${pressure_decimal_places} filters: - lambda: |- const float offset = id(dps310_pressure_offset).state; return isnan(offset) ? x : x + offset; - !include { file: reduce_filter.yaml, vars: { delta: "${pressure_reporting_delta}" } } temperature: id: dps310temperature accuracy_decimals: ${temperature_decimal_places} filters: - !include { file: reduce_filter.yaml, vars: { delta: "${temperature_reporting_delta}" } } - platform: sen5x id: sen55 pm_1_0: name: "PM <1µm Weight concentration" id: pm_1_0 device_class: "pm1" accuracy_decimals: ${pm_decimal_places} filters: - !include { file: reduce_filter.yaml, vars: { delta: "${pm_reporting_delta}" } } pm_2_5: name: "PM <2.5µm Weight concentration" id: pm_2_5 device_class: "pm25" accuracy_decimals: ${pm_decimal_places} filters: - !include { file: reduce_filter.yaml, vars: { delta: "${pm_reporting_delta}" } } pm_4_0: name: "PM <4µm Weight concentration" id: pm_4_0 device_class: "pm4" accuracy_decimals: ${pm_decimal_places} filters: - !include { file: reduce_filter.yaml, vars: { delta: "${pm_reporting_delta}" } } pm_10_0: name: "PM <10µm Weight concentration" id: pm_10_0 device_class: "pm10" accuracy_decimals: ${pm_decimal_places} filters: - !include { file: reduce_filter.yaml, vars: { delta: "${pm_reporting_delta}" } } temperature: name: "SEN55 Temperature" id: sen55_temperature device_class: "temperature" accuracy_decimals: ${temperature_decimal_places} filters: # Warm-up: the first readings after power-up are off while the SEN55 # and enclosure settle, so drop them for the first 3 minutes (the same # window the Estimated Room sensors wait out). Only while deep sleep is # prevented: a sleeping unit is awake ~2 minutes per cycle and would # otherwise never report. - lambda: |- static bool warmed_up = false; if (!warmed_up) { if (millis() >= 180000) { warmed_up = true; } else if (id(prevent_sleep).state || id(ota_mode).state) { return {}; } } return x; - lambda: return x - id(sen55_temperature_offset).state; - !include { file: reduce_filter.yaml, vars: { delta: "${temperature_reporting_delta}" } } humidity: name: "SEN55 Humidity" id: sen55_humidity device_class: "humidity" accuracy_decimals: ${humidity_decimal_places} filters: # Warm-up: the first readings after power-up are off while the SEN55 # and enclosure settle, so drop them for the first 3 minutes (the same # window the Estimated Room sensors wait out). Only while deep sleep is # prevented: a sleeping unit is awake ~2 minutes per cycle and would # otherwise never report. - lambda: |- static bool warmed_up = false; if (!warmed_up) { if (millis() >= 180000) { warmed_up = true; } else if (id(prevent_sleep).state || id(ota_mode).state) { return {}; } } return x; # The temperature offset models enclosure self-heating, so the air # the SEN55 measures holds the same water vapour as the room but at a # higher temperature. Re-reference RH to the corrected temperature at # constant vapour pressure (Magnus formula, same coefficients as # Predicted_Room.yaml), then apply the flat humidity offset. # Uses the unfiltered SEN55 temperature: sen5x publishes temperature # before humidity in each read, so it is from the same sample. - lambda: |- float rh = x; const float t_raw = id(sen55_temperature).get_raw_state(); const float t_off = id(sen55_temperature_offset).state; if (!std::isnan(rh) && !std::isnan(t_raw) && !std::isnan(t_off)) { const float t_room = t_raw - t_off; const float mag_raw = expf((17.625f * t_raw) / (243.04f + t_raw)); const float mag_room = expf((17.625f * t_room) / (243.04f + t_room)); rh = rh * (mag_raw / mag_room); } const float rh_off = id(sen55_humidity_offset).state; if (!std::isnan(rh_off)) { rh -= rh_off; } if (rh > 100.0f) { rh = 100.0f; } else if (rh < 0.0f) { rh = 0.0f; } return rh; - !include { file: reduce_filter.yaml, vars: { delta: "${humidity_reporting_delta}" } } voc_index: name: "SEN55 VOC" id: sen55_voc accuracy_decimals: ${voc_decimal_places} filters: - !include { file: reduce_filter.yaml, vars: { delta: "${voc_reporting_delta}" } } on_value: - if: condition: lambda: 'return id(air_quality_led_source).current_option() == "VOC Index";' then: - script.execute: update_air_quality_led algorithm_tuning: #https://sensirion.com/media/documents/25AB572C/62B463AA/Sensirion_Engineering_Guidelines_SEN5x.pdf index_offset: 100 learning_time_offset_hours: 72 learning_time_gain_hours: 72 gating_max_duration_minutes: 180 std_initial: 50 gain_factor: 230 nox_index: name: "SEN55 NOX" id: sen55_nox accuracy_decimals: ${nox_decimal_places} acceleration_mode: low store_baseline: true # Some units misreport SEN54 on the product name register even though the # NOx element is present; forcing the model keeps NOx enabled. Needs the # model option from esphome 2026.7.2. model: sen55 address: 0x69 update_interval: 10s - platform: template name: "PM 0.3 To 1 µm" id: pm0_3_to_1 disabled_by_default: true lambda: return id(pm_1_0).state; unit_of_measurement: "µg/m³" state_class: measurement icon: mdi:air-filter accuracy_decimals: ${pm_decimal_places} update_interval: 10s filters: - !include { file: reduce_filter.yaml, vars: { delta: "${pm_reporting_delta}" } } - platform: template name: "PM 1 To 2.5 µm" id: pm1_to_2_5 disabled_by_default: true lambda: return std::max(0.0f, id(pm_2_5).state - id(pm_1_0).state); unit_of_measurement: "µg/m³" state_class: measurement icon: mdi:air-filter accuracy_decimals: ${pm_decimal_places} update_interval: 10s filters: - !include { file: reduce_filter.yaml, vars: { delta: "${pm_reporting_delta}" } } - platform: template name: "PM 2.5 To 4 µm" id: pm2_5_to_4 disabled_by_default: true lambda: return std::max(0.0f, id(pm_4_0).state - id(pm_2_5).state); unit_of_measurement: "µg/m³" state_class: measurement icon: mdi:air-filter accuracy_decimals: ${pm_decimal_places} update_interval: 10s filters: - !include { file: reduce_filter.yaml, vars: { delta: "${pm_reporting_delta}" } } - platform: template name: "PM 4 To 10 µm" id: pm4_to_10 disabled_by_default: true lambda: return std::max(0.0f, id(pm_10_0).state - id(pm_4_0).state); unit_of_measurement: "µg/m³" state_class: measurement icon: mdi:air-filter accuracy_decimals: ${pm_decimal_places} update_interval: 10s filters: - !include { file: reduce_filter.yaml, vars: { delta: "${pm_reporting_delta}" } } - platform: aqi name: "NowCast AQI" id: nowcast_aqi pm_2_5: pm_2_5 pm_10_0: pm_10_0 calculation_type: AQI device_class: "aqi" accuracy_decimals: ${aqi_decimal_places} on_value: - if: condition: lambda: 'return id(air_quality_led_source).current_option() == "NowCast AQI";' then: - script.execute: update_air_quality_led - platform: mics_4514 id: mics4514 update_interval: 60s nitrogen_dioxide: name: Nitrogen Dioxide id: nitrogen_dioxide device_class: "nitrogen_dioxide" accuracy_decimals: ${nitrogen_dioxide_decimal_places} filters: - !include { file: reduce_filter.yaml, vars: { delta: "${nitrogen_dioxide_reporting_delta}" } } carbon_monoxide: name: Carbon Monoxide id: carbon_monoxide device_class: "carbon_monoxide" accuracy_decimals: ${co_decimal_places} filters: - !include { file: reduce_filter.yaml, vars: { delta: "${co_reporting_delta}" } } hydrogen: name: Hydrogen id: hydrogen accuracy_decimals: ${hydrogen_decimal_places} filters: - !include { file: reduce_filter.yaml, vars: { delta: "${hydrogen_reporting_delta}" } } ethanol: name: Ethanol id: ethanol accuracy_decimals: ${ethanol_decimal_places} filters: - !include { file: reduce_filter.yaml, vars: { delta: "${ethanol_reporting_delta}" } } methane: name: Methane id: methane accuracy_decimals: ${methane_decimal_places} filters: - !include { file: reduce_filter.yaml, vars: { delta: "${methane_reporting_delta}" } } ammonia: name: Ammonia id: ammonia accuracy_decimals: ${ammonia_decimal_places} filters: - !include { file: reduce_filter.yaml, vars: { delta: "${ammonia_reporting_delta}" } } light: - platform: esp32_rmt_led_strip id: rgb_light name: "RGB Light" pin: GPIO3 default_transition_length: 0s chipset: WS2812 num_leds: 3 channel_colors: GRB effects: - pulse: name: "Slow Pulse" transition_length: 250ms update_interval: 250ms min_brightness: 50% max_brightness: 100% - pulse: name: "Fast Pulse" transition_length: 100ms update_interval: 100ms min_brightness: 50% max_brightness: 100% button: - platform: restart icon: mdi:power-cycle name: "ESP Reboot" - platform: factory_reset disabled_by_default: True name: "Factory Reset ESP" id: factory_reset_all # Forced recalibration to "CO2 Calibration Target", the CO2 the sensor sees right now (outdoor air at an open # window, or a reference meter beside it). The sensor wants >3 minutes of steady air first, so the button only # calibrates when the last four readings (3 minutes without a gap, the newest at most 90 s old) lie within 20 ppm # of each other and within 100 ppm of the target; a press indoors by mistake changes nothing. "CO2 Calibration # Result" says what happened, "Calibrated" only once the sensor answered that it took the target. - platform: template name: "Calibrate CO2" id: set_SCD40_calibrate on_press: - lambda: |- auto &r = id(co2_recent); // A newest reading older than 90 s (the sensor stopped reporting) leaves no current history if (millis() - id(co2_recent_ms)[0] > 90000) r.fill(NAN); float target = id(co2_calibration_target).state; float lo = 1e9f, hi = -1e9f, sum = 0.0f; bool full = true; for (float v : r) { if (std::isnan(v)) { full = false; break; } lo = std::min(lo, v); hi = std::max(hi, v); sum += v; } float mean = sum / 4.0f; char buf[96]; if (id(co2_forced_calibration).is_running() || id(setCo2AutoCalibration).is_running()) { snprintf(buf, sizeof(buf), "Not calibrated: the sensor is busy"); } else if (std::isnan(target)) { snprintf(buf, sizeof(buf), "Not calibrated: no target"); } else if (!full || id(co2_recent_ms)[0] - id(co2_recent_ms)[3] < 170000) { // four readings 60 s apart span 180 s; 170 s leaves room for the scheduler's jitter snprintf(buf, sizeof(buf), "Not calibrated: less than 3 minutes of readings"); } else if (hi - lo > 20.0f) { snprintf(buf, sizeof(buf), "Not calibrated: readings not steady (%.0f-%.0f ppm)", lo, hi); } else if (fabsf(mean - target) > 100.0f) { snprintf(buf, sizeof(buf), "Not calibrated: %.0f ppm is too far from %.0f ppm", mean, target); } else { // The outcome comes from the sensor's answer, about a second later (co2_forced_calibration); the next // calibration needs three fresh minutes again id(co2_forced_calibration).execute((int) lroundf(target), r[0]); r.fill(NAN); return; } ESP_LOGI("co2_calibration", "%s", buf); id(co2_calibration_result).publish_state(buf); - platform: template name: "Clean SEN55" id: clean_sen55 on_press: - sen5x.start_fan_autoclean: sen55 - platform: template name: "Firmware Update" id: update_firmware icon: mdi:cloud-download entity_category: "config" on_press: - logger.log: "Applying firmware update for the selected channel" # OTA download needs the device awake for its whole duration. - lambda: |- id(deep_sleep_1).prevent_deep_sleep(); - delay: 3s - script.execute: install_selected_channel - script.wait: install_selected_channel # Reached after perform(true) has queued the flash, or when the script # refused to install (no fresh manifest from the selected channel). # Re-arm deep sleep unless something else is holding the device awake. - if: condition: and: - switch.is_off: prevent_sleep - binary_sensor.is_off: ota_mode then: - lambda: |- id(deep_sleep_1).allow_deep_sleep(); text_sensor: - platform: template name: "CO2 Calibration Result" id: co2_calibration_result icon: mdi:molecule-co2 entity_category: "diagnostic" update_interval: never # Convert VOC Index To Text: # https://sensirion.com/media/documents/02232963/6294E043/Info_Note_VOC_Index.pdf # https://sensirion.com/media/documents/ACD82D45/6294DFC0/Info_Note_Integration_VOC_NOx_Sensor.pdf - platform: template name: "VOC Quality" id: voc_quality icon: mdi:air-filter lambda: |- if (id(sen55_voc).state < 80) { return std::string("Improved");} else if (id(sen55_voc).state < 150) { return std::string("Normal");} else if (id(sen55_voc).state < 250) { return std::string("Abnormal");} else if (id(sen55_voc).state < 400) { return std::string("Very abnormal");} else if (id(sen55_voc).state >= 400) { return std::string("Extremely abnormal");} else { return std::string("Unknown");} - platform: wifi_info ip_address: name: "IP Address" id: wifi_ip entity_category: "diagnostic" - platform: version name: "ESPHome Version" hide_timestamp: true entity_category: "diagnostic" - platform: template name: "Apollo Firmware Version" id: apollo_firmware_version update_interval: never entity_category: "diagnostic" switch: - platform: factory_reset id: factory_reset_switch internal: true - platform: template name: "Reduce DB Reporting" id: reduce_db_reporting icon: mdi:database restore_mode: RESTORE_DEFAULT_OFF optimistic: true entity_category: "config" - platform: template name: "Prevent Sleep" id: prevent_sleep icon: mdi:sleep restore_mode: RESTORE_DEFAULT_ON optimistic: true entity_category: "config" on_turn_on: then: - lambda: |- id(deep_sleep_1).prevent_deep_sleep(); on_turn_off: then: - if: condition: binary_sensor.is_off: ota_mode then: - lambda: |- id(deep_sleep_1).allow_deep_sleep(); - platform: template name: "CO2 Auto Calibration" id: co2_auto_calibration icon: mdi:molecule-co2 restore_mode: RESTORE_DEFAULT_ON optimistic: true entity_category: "config" on_turn_on: then: - script.execute: id: setCo2AutoCalibration enable: true on_turn_off: then: - script.execute: id: setCo2AutoCalibration enable: false - platform: template name: "Bluetooth Proxy" id: bluetooth_proxy_switch icon: mdi:bluetooth entity_category: "config" restore_mode: ${bluetooth_proxy_restore_mode} optimistic: true # On: power up the BLE stack, then scan and proxy. Off: stop scanning and # power the BLE stack down entirely (not just pause the scanner). on_turn_on: - script.execute: bluetooth_proxy_on on_turn_off: - script.execute: bluetooth_proxy_off select: - platform: template name: "Air Quality LED Source" id: air_quality_led_source icon: mdi:led-on optimistic: true restore_value: true entity_category: "config" options: ["Off", "NowCast AQI", "CO2", "VOC Index"] initial_option: "Off" on_value: then: - script.execute: update_air_quality_led - platform: template name: "Firmware Channel" id: firmware_channel icon: mdi:source-branch entity_category: "config" optimistic: true restore_value: true options: - "Stable" - "Beta" initial_option: "Stable" on_value: then: - script.execute: apply_ota_source script: - id: bluetooth_proxy_on mode: restart then: - ble.enable: # ble.enable only requests the stack; wait until it is up so the scan # start is not rejected. The tracker also resumes continuous scanning on # its own once the stack is active, so a timeout here is harmless. - wait_until: condition: ble.enabled: timeout: 10s - esp32_ble_tracker.start_scan: continuous: true - id: bluetooth_proxy_off mode: restart then: # Cancel a turn-on still waiting for the stack so it cannot start a scan # after the radio has been switched off. - script.stop: bluetooth_proxy_on # Always stop, even before the stack is up: this is what clears the # tracker's continuous-scan flag. Otherwise, with the stack kept powered # (ble_always_on), the tracker would start scanning on its own as soon # as the stack becomes active. With nothing running it is a no-op. - esp32_ble_tracker.stop_scan: - if: condition: lambda: "return !${ble_always_on};" then: - ble.disable: - id: apply_ota_source # Sets the OTA manifest URL from the Firmware Channel select (Stable/Beta). # The manifest base is per-variant (ota_*_manifest substitutions) so legacy # AIR-1_BLE builds keep their firmware-ble manifest. then: - lambda: |- const bool beta = id(firmware_channel).current_option() == "Beta"; const char *url = beta ? "${ota_beta_manifest}" : "${ota_stable_manifest}"; ESP_LOGI("firmware", "OTA manifest set to: %s", url); id(update_http_request).set_source_url(url); - component.update: update_http_request # The update component drops a check requested while another one is running, # so keep asking until install_selected_channel has what it needs. - id: request_channel_manifest mode: restart then: - repeat: count: 4 then: - script.execute: apply_ota_source - script.wait: apply_ota_source - delay: 5s # Forced install, but only of a manifest fetched after this press from the # selected channel. mode single: a second press while this runs is ignored. - id: install_selected_channel mode: single then: # Mark the cached firmware URL as old. Only a successful check replaces it. - lambda: |- std::string &fw = const_cast(id(update_http_request).update_info.firmware_url); if (fw.rfind('!', 0) != 0) fw.insert(0, "!"); - script.execute: request_channel_manifest # Wait for a check without error whose firmware sits in the directory of # the selected channel's manifest (relative Pages paths are merged with # it, beta paths are release-asset URLs in the same directory). - wait_until: timeout: 20s condition: lambda: |- const char *src = id(update_http_request).get_source_url(); const size_t dir = strrchr(src, '/') - src + 1; return !id(update_http_request).status_has_error() && strncmp(id(update_http_request).update_info.firmware_url.c_str(), src, dir) == 0; - script.stop: request_channel_manifest - lambda: |- std::string &fw = const_cast(id(update_http_request).update_info.firmware_url); const char *src = id(update_http_request).get_source_url(); const size_t dir = strrchr(src, '/') - src + 1; const bool err = id(update_http_request).status_has_error(); const char *why = nullptr; if (fw.rfind('!', 0) == 0) { why = err ? "the manifest check failed" : "no manifest arrived within 20s"; fw.erase(0, 1); // put the old URL back for the update entity } else if (err || strncmp(fw.c_str(), src, dir) != 0) { why = "the manifest that arrived is not from the selected channel"; } if (why != nullptr) { ESP_LOGW("firmware", "Firmware Update: %s (%s); not installing", why, src); return; } ESP_LOGI("firmware", "Installing %s", fw.c_str()); id(update_http_request).perform(true); - id: setCo2AutoCalibration mode: restart parameters: enable: bool then: #The SCD40 forgets this setting on power loss and the scd4x component #re-applies the YAML default during setup (~1.5s after boot), so when this #runs at boot (via the restored switch state) wait for setup to finish first. - if: condition: lambda: "return millis() < 20000;" then: - delay: 20s #Not while a forced calibration stops and restarts the measurement - script.wait: co2_forced_calibration #The sensor only accepts the ASC command while idle, so stop periodic #measurement first, then restart it. Same sequence the component uses #for forced calibration. - lambda: |- id(scd40).write_command((uint16_t) 0x3F86); //stop periodic measurement - delay: 500ms - lambda: |- id(scd40).set_automatic_self_calibration(enable); if (!id(scd40).write_command((uint16_t) 0x2416, (uint16_t) (enable ? 1 : 0))) { ESP_LOGE("Apollo", "Failed to set CO2 auto calibration"); } else { ESP_LOGI("Apollo", "CO2 auto calibration %s", enable ? "enabled" : "disabled"); } - delay: 10ms - lambda: |- id(scd40).write_command((uint16_t) 0x21B1); //start periodic measurement # The forced recalibration the "Calibrate CO2" button starts. It runs here rather than through the scd4x # component's perform_forced_calibration(), which does not read the sensor's answer: stop the periodic measurement, # send the target (0x362F) only if that worked, read the correction 400 ms later (0xFFFF: refused), start the # measurement again, as in the SCD4x datasheet, and report only once the sensor measures again. - id: co2_forced_calibration mode: single parameters: target: int before: float then: # no reading of the component in between, whose answer would take the correction's place - component.suspend: scd40 - lambda: |- id(co2_measuring) = false; id(co2_frc_step) = id(scd40).write_command((uint16_t) 0x3F86) ? 1 : 0; //stop periodic measurement if (id(co2_frc_step) == 0) ESP_LOGE("co2_calibration", "Could not stop the CO2 measurement"); # The sensor takes the target only while idle - if: condition: lambda: "return id(co2_frc_step) == 1;" then: - delay: 500ms - lambda: |- if (id(scd40).write_command((uint16_t) 0x362F, (uint16_t) target)) id(co2_frc_step) = 2; - delay: 400ms - lambda: |- uint16_t answer = 0xFFFF; if (id(co2_frc_step) == 2 && id(scd40).read_data(answer)) { id(co2_frc_step) = answer != 0xFFFF ? 3 : 4; if (answer != 0xFFFF) ESP_LOGI("co2_calibration", "Correction %d ppm", (int) answer - 0x8000); } # Whatever the outcome, the measurement must run again, or the CO2 would stand still: start it and check that # the sensor flags new data (every 5 s), up to five times - repeat: count: 5 then: - if: condition: lambda: "return !id(co2_measuring);" then: - lambda: |- id(scd40).write_command((uint16_t) 0x21B1); //start periodic measurement (refused while running) - delay: 6s - lambda: |- uint16_t ready = 0; bool asked = id(scd40).write_command((uint16_t) 0xE4B8); //get data ready status delay(1); id(co2_measuring) = asked && id(scd40).read_data(ready) && (ready & 0x07FF) != 0; - lambda: |- char buf[160]; int n; if (id(co2_frc_step) == 3) { n = snprintf(buf, sizeof(buf), "Calibrated to %d ppm (was %.0f ppm)", target, before); } else if (id(co2_frc_step) == 0) { n = snprintf(buf, sizeof(buf), "Not calibrated: the sensor did not stop measuring"); } else if (id(co2_frc_step) == 1) { n = snprintf(buf, sizeof(buf), "Not calibrated: the target did not reach the sensor"); } else if (id(co2_frc_step) == 2) { // The target reached the sensor, so it may have taken it n = snprintf(buf, sizeof(buf), "Calibration outcome unknown: the sensor's answer could not be read"); } else { n = snprintf(buf, sizeof(buf), "Not calibrated: the sensor refused it"); } if (!id(co2_measuring)) { snprintf(buf + n, sizeof(buf) - n, "; the measurement did not restart, restarting the device"); ESP_LOGE("co2_calibration", "%s", buf); } else if (id(co2_frc_step) == 3) { ESP_LOGI("co2_calibration", "%s", buf); } else { ESP_LOGW("co2_calibration", "%s", buf); } id(co2_calibration_result).publish_state(buf); - component.resume: scd40 # The last way to get the measurement going: the scd4x component starts it at boot - if: condition: lambda: "return !id(co2_measuring);" then: - delay: 5s - lambda: "App.safe_reboot();" - id: testScript then: if: condition: - lambda: "return id(runTest) == true;" then: - lambda: "id(runTest) = false;" - lambda: "id(testCycleCount) = 0;" - while: condition: - lambda: "return id(testCycleCount) < 5;" then: - if: condition: - lambda: "return id(dps310Passed) == false;" then: - if: condition: - lambda: "return id(dps310pressure).state > 800 && id(dps310pressure).state < 1200;" then: - lambda: "id(dps310Passed) = true;" - if: condition: - lambda: "return id(dps310Passed);" then: - lambda: "id(testCycleCount) = 10;" - lambda: "id(runTest) = false;" - delay: 1s - lambda: "id(testCycleCount) += 1;" #Check If Test Passed To Trigger Lights - if: condition: - lambda: "return id(dps310Passed);" then: - lambda: "id(runTest) = false;" - light.turn_on: id: rgb_light red: 0% green: 100% blue: 0% - delay: 5s - light.turn_on: id: rgb_light red: 0% green: 0% blue: 0% - light.turn_off: id: rgb_light else: - lambda: "id(runTest) = false;" - light.turn_on: id: rgb_light red: 100% green: 0% blue: 0% - delay: 5s - light.turn_on: id: rgb_light red: 0% green: 0% blue: 0% - light.turn_off: id: rgb_light - script.execute: restore_air_quality_led - id: statusCheck then: - if: condition: - lambda: "return id(ink_ha_connected).state;" then: - logger.log: "Apollo Automation: Connected To HA" - light.turn_on: id: rgb_light brightness: 100% red: 0% green: 0% blue: 100% else: - if: condition: - wifi.connected then: - logger.log: "Apollo Automation: Connected To Wifi" - light.turn_on: id: rgb_light brightness: 100% red: 0% green: 100% blue: 0% else: - logger.log: "Apollo Automation: Not Connected To Wifi" - light.turn_on: id: rgb_light brightness: 100% red: 100% green: 100% blue: 0% - delay: 5s - light.turn_off: rgb_light - script.execute: restore_air_quality_led - id: reportAllValues then: - component.update: sen55 - component.update: mics4514 - component.update: dps_310 - component.update: scd40 - component.update: wifi_signal_db - component.update: sys_esp_temperature - component.update: sys_uptime - component.update: pm0_3_to_1 - component.update: pm1_to_2_5 - component.update: pm2_5_to_4 - component.update: pm4_to_10 - component.update: voc_quality - id: update_air_quality_led then: - lambda: |- // The select's restore_value publishes during component setup and // fires on_value before rgb_light (esp32_rmt_led_strip) is // initialized; touching the light then faults in schedule_show(). // Don't write the LED in the first few seconds after boot. The // sensor/select/brightness triggers all run normally after that. if (millis() < 5000) { return; } // While statusCheck or testScript owns the LED (button-press status // flash, boot self-test), leave rgb_light alone so this updater // doesn't override their colors mid-display. if (id(statusCheck).is_running() || id(testScript).is_running()) { return; } const std::string src = id(air_quality_led_source).current_option(); if (src == "Off") { id(rgb_light).turn_off().perform(); return; } // Map the selected metric to a severity band: 0=Good … 5=Hazardous. // Each metric has its own scale; bands share one color table below. int band = -1; if (src == "NowCast AQI") { // US EPA AQI const float x = id(nowcast_aqi).state; if (isnan(x)) { id(rgb_light).turn_off().perform(); return; } // no sample yet (e.g. missing module); clear stale color if (x <= 50) band = 0; else if (x <= 100) band = 1; else if (x <= 150) band = 2; else if (x <= 200) band = 3; else if (x <= 300) band = 4; else band = 5; } else if (src == "CO2") { // ppm, SCD40 const float x = id(co2).state; if (isnan(x)) { id(rgb_light).turn_off().perform(); return; } // no sample yet (e.g. missing module); clear stale color if (x <= 800) band = 0; else if (x <= 1000) band = 1; else if (x <= 1500) band = 2; else if (x <= 2000) band = 3; else if (x <= 2500) band = 4; else band = 5; } else if (src == "VOC Index") { // Sensirion VOC index (matches "VOC Quality" text sensor) const float x = id(sen55_voc).state; if (isnan(x)) { id(rgb_light).turn_off().perform(); return; } // no sample yet (e.g. missing module); clear stale color if (x < 80) band = 0; else if (x < 150) band = 1; else if (x < 250) band = 2; else if (x < 400) band = 3; else band = 4; // VOC scale tops out at "extremely abnormal" } if (band < 0) return; auto call = id(rgb_light).turn_on(); call.set_brightness(id(air_quality_led_brightness).state / 100.0f); switch (band) { case 0: call.set_rgb(0.0f, 1.0f, 0.0f); break; // Green - Good case 1: call.set_rgb(1.0f, 1.0f, 0.0f); break; // Yellow - Moderate case 2: call.set_rgb(1.0f, 0.5f, 0.0f); break; // Orange - Unhealthy (Sensitive) case 3: call.set_rgb(1.0f, 0.0f, 0.0f); break; // Red - Unhealthy case 4: call.set_rgb(0.6f, 0.0f, 1.0f); break; // Purple - Very Unhealthy case 5: call.set_rgb(0.5f, 0.0f, 0.0f); break; // Maroon - Hazardous } call.perform(); # Runs after statusCheck/testScript finish so the air-quality color comes # back right away instead of waiting for the next sensor on_value (up to 60s # when CO2 is the source). statusCheck/testScript call this as their last # action; the short delay lets the calling script finish first so # update_air_quality_led's is_running() guard passes. mode: restart so a # rapid second status flash doesn't stack pending restores. - id: restore_air_quality_led mode: restart then: - delay: 200ms - script.execute: update_air_quality_led