Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Integrations/ESPHome/AIR-1_Factory.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
substitutions:
# Improv-over-BLE provisioning needs the BLE stack powered even while the
# Bluetooth Proxy switch is off, so the Factory image never powers it down.
ble_always_on: "true"

esphome:
name: "${name}"
friendly_name: Apollo AIR-1
Expand Down
145 changes: 134 additions & 11 deletions Integrations/ESPHome/Core.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
substitutions:
name: apollo-air-1
version: "26.11.15.1"
version: "26.11.15.2"
device_description: ${name} made by Apollo Automation - version ${version}.

# Reduced DB Reporting thresholds
Expand Down Expand Up @@ -38,6 +38,28 @@ substitutions:
# `substitutions: { ota_password: !secret <name>_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/

# Default update channel on first boot (no stored user choice yet, i.e. a
# fresh flash). The beta-channel builds override this to "Beta" (see
Expand All @@ -61,6 +83,11 @@ substitutions:
# 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
Expand All @@ -69,21 +96,22 @@ esphome:
- priority: -100
then:
- script.execute: apply_ota_source
# Re-apply the Bluetooth Proxy switch after all components set up, so BLE
# scanning matches the switch state restored from flash.
# 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:
- esp32_ble_tracker.start_scan:
continuous: true
- script.execute: bluetooth_proxy_on
else:
- esp32_ble_tracker.stop_scan:
- script.execute: bluetooth_proxy_off

packages:
# Physics-based Predicted Room Temperature/Humidity sensors and their
# 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

Expand All @@ -93,6 +121,13 @@ esp32:
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:
Expand Down Expand Up @@ -459,6 +494,21 @@ sensor:
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:
Expand All @@ -467,7 +517,48 @@ sensor:
device_class: "humidity"
accuracy_decimals: ${humidity_decimal_places}
filters:
- lambda: return x - id(sen55_humidity_offset).state;
# 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"
Expand Down Expand Up @@ -823,11 +914,12 @@ switch:
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:
- esp32_ble_tracker.start_scan:
continuous: true
- script.execute: bluetooth_proxy_on
on_turn_off:
- esp32_ble_tracker.stop_scan:
- script.execute: bluetooth_proxy_off

select:
- platform: template
Expand Down Expand Up @@ -859,6 +951,37 @@ select:
- 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
Expand Down
22 changes: 11 additions & 11 deletions Integrations/ESPHome/Predicted_Room.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Physics-based room temperature/humidity prediction.
# Physics-based room temperature/humidity estimate.
# Research and coefficient fitting by Ellude (Apollo Discord).
#
# The Air-1 enclosure self-heats, so the on-board sensors read warmer/drier
Expand Down Expand Up @@ -72,11 +72,11 @@ number:
mode: box

sensor:
# Room temperature prediction. Blends the three on-board temperature sensors
# Estimated room temperature. Blends the three on-board temperature sensors
# and corrects for enclosure self-heating using the Physics Multiplier P /
# Baseline Offset CONFIG numbers above.
- platform: template
name: "Predicted Room Temperature"
name: "Estimated Room Temperature"
id: predicted_room_temperature
disabled_by_default: true
unit_of_measurement: "°C"
Expand All @@ -85,7 +85,7 @@ sensor:
accuracy_decimals: 2
update_interval: 10s
lambda: |-
// Make sure the Air-1 is running at least for 3 minutes (avoid bad initial predictions).
// Make sure the Air-1 is running at least for 3 minutes (avoid bad initial estimates).
// sys_uptime is NAN until its first publish, and NAN < 180.0 is false, so guard for it.
float uptime = id(sys_uptime).state;
if (std::isnan(uptime) || uptime < 180.0) {
Expand All @@ -99,7 +99,7 @@ sensor:
float p_val = id(coefficient_p).state;
float baseline = id(baseline_offset).state;

// Ensure all sensors and coefficients are valid numbers before predicting
// Ensure all sensors and coefficients are valid numbers before estimating
if (std::isnan(t_sen) || std::isnan(t_dps) || std::isnan(t_scd) ||
std::isnan(p_val) || std::isnan(baseline)) {
return NAN;
Expand All @@ -111,13 +111,13 @@ sensor:
// Estimate the required offset
float t_offset = p_val * (t_scd - t_sen);

// Return predicted room temperature
// Return the estimated room temperature
return t_base - t_offset + baseline;

# Room humidity prediction. Re-references each sensor's RH to the predicted
# Estimated room humidity. Re-references each sensor's RH to the estimated
# room temperature (Magnus formula), then blends by manufacturing tolerance.
- platform: template
name: "Predicted Room Humidity"
name: "Estimated Room Humidity"
id: predicted_room_humidity
disabled_by_default: true
unit_of_measurement: "%"
Expand All @@ -126,14 +126,14 @@ sensor:
accuracy_decimals: 1
update_interval: 10s
lambda: |-
// Make sure the Air-1 is running at least for 3 minutes (avoid bad initial predictions).
// Make sure the Air-1 is running at least for 3 minutes (avoid bad initial estimates).
// sys_uptime is NAN until its first publish, and NAN < 180.0 is false, so guard for it.
float uptime = id(sys_uptime).state;
if (std::isnan(uptime) || uptime < 180.0) {
return NAN;
}

// Fetch the calculated Predicted Room Temperature
// Fetch the calculated Estimated Room Temperature
float t_room = id(predicted_room_temperature).state;

// Fetch Sensor data
Expand Down Expand Up @@ -170,7 +170,7 @@ sensor:

return final_rh;

# Expose the raw sensors the prediction reads from, with tuning offsets,
# Expose the raw sensors the estimate reads from, with tuning offsets,
# by extending the hardware sensor blocks defined in Core.yaml.
- id: !extend scd40
temperature:
Expand Down
Loading