Skip to content

Enable CO2 auto calibration by default with toggle to disable#72

Open
bharvey88 wants to merge 2 commits into
ApolloAutomation:betafrom
bharvey88:co2-asc-toggle
Open

Enable CO2 auto calibration by default with toggle to disable#72
bharvey88 wants to merge 2 commits into
ApolloAutomation:betafrom
bharvey88:co2-asc-toggle

Conversation

@bharvey88

@bharvey88 bharvey88 commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Version: 26.6.10.1

What does this implement/fix?

Enables the SCD40's automatic self-calibration (ASC) by default and adds a "CO2 Auto Calibration" switch so users can turn it off.

  • With ASC on (the new default, matching the ESPHome default), the sensor corrects its own baseline as long as it sees fresh air (about 420 ppm) at least once a week. The manual 420 ppm recalibration every 1-2 years is no longer needed for most homes.
  • The new switch is visible under Configuration, defaults to on, and persists across reboots and updates. Toggling takes effect immediately: the script stops periodic measurement, sends the ASC command over I2C (using the scd4x component's public write_command), and restarts measurement, the same sequence the component uses for forced calibration. The saved choice is re-applied ~20 seconds after every boot, since the SCD40 loses the setting on power loss and ESPHome re-applies the YAML default during setup.
  • The "Calibrate SCD40 To 420ppm" button and the calibrate_co2_value action are unchanged and keep working whether ASC is on or off.
  • Version bumped to 26.6.10.1.

Why a breaking change: existing devices get ASC turned on when they update. Readings may shift over the first days as the baseline self-corrects. Users whose device sits in a space that rarely gets fresh air (a sealed office, a basement, a grow room) should turn the new switch off and keep calibrating manually. Suggest including this in the release notes.

The same change is rolling out to AIR-1 and MTR-1 (identical), and R_PRO-1 (toggle fix/rename; its ASC was already on by default). A wiki update covering all four products is ready to merge alongside the firmware releases.

Verification: esphome compile passes for MSR-2.yaml, MSR-2_BLE.yaml, and MSR-2_Factory.yaml (ESPHome 2026.1.3). Not yet tested on hardware.

Types of changes

  • Bugfix (fixed change that fixes an issue)
  • New feature (thanks!)
  • Breaking change (repair/feature that breaks existing functionality)
  • Dependency Update - Does not publish
  • Other - Does not publish
  • Website of github readme file update - Does not publish
  • Github workflows - Does not publish

Checklist / Checklijst:

  • The code change has been tested and works locally
  • The code change has not yet been tested

If user-visible functionality or configuration variables are added/modified:

  • Added/updated documentation for the web page

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Updated Apollo Automation version to 26.6.10.1
    • Enabled automatic self-calibration for CO2 sensor by default
    • Added CO2 Auto Calibration toggle switch to manually control automatic calibration behavior

@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@bharvey88, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 21 minutes and 18 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: cf4f31c4-aeac-426d-b7f4-426c9974a63f

📥 Commits

Reviewing files that changed from the base of the PR and between 0c8e3a0 and 289987e.

📒 Files selected for processing (1)
  • Integrations/ESPHome/Core.yaml

Walkthrough

This PR updates the ESPHome Core configuration with CO2 auto-calibration functionality. Version is bumped to 26.6.10.1, automatic self-calibration is enabled in the SCD4x sensor, and a new switch entity allows users to toggle the feature dynamically via an automation script.

Changes

CO2 Auto-Calibration Feature

Layer / File(s) Summary
Version and Sensor Base Configuration
Integrations/ESPHome/Core.yaml
Apollo Automation version updated to 26.6.10.1. SCD4x sensor (scd40) automatic_self_calibration enabled as default.
CO2 Auto-Calibration Control and Script
Integrations/ESPHome/Core.yaml
Template switch (co2_auto_calibration) wired to execute setCo2AutoCalibration script with boolean parameter. Script delays at boot, stops/restarts periodic SCD40 measurement, applies calibration setting via command sequence with logging.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested labels

new-feature, breaking-change

Suggested reviewers

  • TrevorSchirmer

Poem

Hopping through configs with carbon and cheer,
Auto-calibration now crystal and clear—
A switch flips the magic, the script does the dance,
SCD40 sensors given a control-loving glance! 🐰✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: enabling CO2 auto-calibration by default while adding a toggle for users to disable it, which matches the core objective of the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
Integrations/ESPHome/Core.yaml (1)

834-846: 💤 Low value

Consider adding error handling for stop/start I2C commands.

The ASC command (0x2416) has proper error logging, but the stop (0x3F86) and start (0x21B1) commands do not check return values. If start_periodic_measurement fails, the sensor would stop reporting CO2 until the next reboot.

Since this mirrors ESPHome's pattern for forced calibration and I2C failures are rare, this is a minor resilience improvement.

♻️ Suggested improvement
     - lambda: |-
-        id(scd40).write_command((uint16_t) 0x3F86); //stop periodic measurement
+        if (!id(scd40).write_command((uint16_t) 0x3F86)) {
+          ESP_LOGW("Apollo", "Failed to 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
+        if (!id(scd40).write_command((uint16_t) 0x21B1)) {
+          ESP_LOGE("Apollo", "Failed to restart periodic measurement");
+        }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Integrations/ESPHome/Core.yaml` around lines 834 - 846, The stop/start I2C
commands around id(scd40) use id(scd40).write_command((uint16_t) 0x3F86) and
id(scd40).write_command((uint16_t) 0x21B1) without checking return values; add
the same error handling pattern used for the ASC command (0x2416) so both the
stop (0x3F86) and start (0x21B1) calls check the boolean result of
id(scd40).write_command, log failures with ESP_LOGE("Apollo", "... failed to
stop/start periodic measurement") and optionally ESP_LOGI on success to ensure
the code detects and reports I2C failures (use the existing
id(scd40).set_automatic_self_calibration and the surrounding lambda blocks to
place these checks).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@Integrations/ESPHome/Core.yaml`:
- Around line 834-846: The stop/start I2C commands around id(scd40) use
id(scd40).write_command((uint16_t) 0x3F86) and
id(scd40).write_command((uint16_t) 0x21B1) without checking return values; add
the same error handling pattern used for the ASC command (0x2416) so both the
stop (0x3F86) and start (0x21B1) calls check the boolean result of
id(scd40).write_command, log failures with ESP_LOGE("Apollo", "... failed to
stop/start periodic measurement") and optionally ESP_LOGI on success to ensure
the code detects and reports I2C failures (use the existing
id(scd40).set_automatic_self_calibration and the surrounding lambda blocks to
place these checks).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 313af0e4-7a5b-428b-a577-0f614a3c3524

📥 Commits

Reviewing files that changed from the base of the PR and between 5353096 and 0c8e3a0.

📒 Files selected for processing (1)
  • Integrations/ESPHome/Core.yaml

@bharvey88 bharvey88 added the breaking-change Breaking change label Jun 10, 2026
@bharvey88 bharvey88 changed the base branch from main to beta June 10, 2026 20:28
Enable the SCD40's automatic self-calibration by default (ESPHome
default) so users no longer need to manually recalibrate every 1-2
years. Adds a "CO2 Auto Calibration" switch so users in spaces that
never see fresh-air CO2 levels can turn ASC off and keep using the
manual 420ppm calibration button instead.

The switch re-applies the saved choice after every boot, since the
sensor loses the setting on power loss and the scd4x component
re-applies the YAML default during setup.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change Breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant