Skip to content

Add configurable update interval for LTR390 sensor#56

Merged
bharvey88 merged 3 commits intobetafrom
feature/ltr390-configurable-settings
Mar 2, 2026
Merged

Add configurable update interval for LTR390 sensor#56
bharvey88 merged 3 commits intobetafrom
feature/ltr390-configurable-settings

Conversation

@bharvey88
Copy link
Contributor

@bharvey88 bharvey88 commented Jan 31, 2026

Summary

This PR implements the configurable update interval requested in #54 for the LTR390 light sensor.

  • LTR390 Update Interval: Allows users to adjust the sensor update frequency from the default 60 seconds to any value between 1-300 seconds

Changes

Number Input Added

  • LTR390 Update Interval - Configurable sensor polling rate (1-300 seconds, default: 60s)

Sensor Configuration Updates

  • Set LTR390 update_interval to never (disables automatic polling)
  • Added global variable ltr390_last_update to track last sensor update time
  • Added interval component that runs every 1 second to check if configured interval has elapsed
  • Manually triggers sensor update via component.update() when needed

Technical Details

Why use interval component?
ESPHome sensor platforms don't support templatable update_interval parameters. Attempting to use a lambda for update_interval results in compilation error: "This option is not templatable!"

How it works:

  1. LTR390 sensor automatic updates are disabled (update_interval: never)
  2. A 1-second interval checks elapsed time since last update
  3. When configured interval is reached, manually triggers id(ltr_390).update()
  4. Tracks update time in global variable for next interval calculation

The setting:

  • Uses ESPHome template number platform
  • Is categorized as CONFIG entity for proper grouping in the web UI
  • Persists values across device reboots (restore_value: true)
  • Is immediately accessible through the device's web interface on port 80
  • Updates take effect immediately without reboot

Benefits

Users can now adjust the LTR390 sensor update frequency without:

  • Editing YAML configuration files
  • Recompiling firmware
  • Reflashing the device
  • Rebooting the device

Simply adjust the value through the web interface and the sensor will update at the new interval.

Testing

  • YAML syntax validated
  • Configuration follows existing patterns (similar to DPS Temperature Offset)
  • Backward compatible (uses default value of 60s matching current behavior)
  • Interval component approach is standard ESPHome pattern for dynamic timing

Resolves

Closes #54

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added a configurable update-interval setting for the LTR390 light sensor to control reading frequency.
  • Improvements
    • Sensor now uses interval-based polling for consistent, timed updates.
    • Added internal tracking of the last update time to ensure accurate scheduling and prevent redundant readings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 31, 2026

Walkthrough

Added a public global timestamp and a user-configurable template number for the LTR390 update interval, set the sensor to update_interval: never, and introduced two 1s interval timers that poll the configured interval and call the sensor's update() when elapsed.

Changes

Cohort / File(s) Summary
Core integration + globals
Integrations/ESPHome/Core.yaml
Added global ltr390_last_update (uint32_t) with initial 0 and restore_value: no.
User-configurable number entity
Integrations/ESPHome/Core.yaml
Added public template number ltr390_update_interval (id) — initial 60, min 1, max 300, entity_category: CONFIG, unit s, optimistic: true, update_interval: never.
LTR390 polling logic / timers
Integrations/ESPHome/Core.yaml
Set LTR390 sensor update_interval: never. Added two identical 1s interval blocks that compute current_time = millis()/1000, read ltr390_last_update and ltr390_update_interval. If elapsed >= interval, call ltr_390.update() and store current_time to ltr390_last_update.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant User as User (UI/Config)
    participant Num as Number Entity\n`ltr390_update_interval`
    participant Timer as 1s Interval Timer
    participant Globals as Global Storage\n`ltr390_last_update`
    participant Sensor as LTR390 Sensor

    User->>Num: set interval (seconds)
    Note right of Num: stored as entity state
    Timer->>Globals: read `ltr390_last_update`
    Timer->>Num: read `.state` (interval)
    Timer->>Timer: compute current_time = millis()/1000
    alt current_time - last_update >= interval
        Timer->>Sensor: call `update()`
        Sensor-->>Timer: update complete
        Timer->>Globals: write `ltr390_last_update = current_time`
    else not yet elapsed
        Timer-->>Timer: wait next tick
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I count the seconds, tap the sun,
A number set — the updates run.
I note the time and give a nudge,
Small hops, bright data — snug and judged.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR implements the configurable update interval requirement from issue #54, but does not implement the lux offset setting also requested in the same issue. Implement the lux offset setting feature requested in issue #54 to fully address all requirements, or update the issue to clarify that the offset is out of scope for this PR.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately describes the main change: adding a configurable update interval for the LTR390 sensor, which is the primary feature implemented in this changeset.
Out of Scope Changes check ✅ Passed All changes (global variable, template number entity, interval-based polling logic) are directly related to implementing the configurable update interval feature, with no unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/ltr390-configurable-settings

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.

@bharvey88 bharvey88 force-pushed the feature/ltr390-configurable-settings branch from 83db9fd to 0c067fa Compare January 31, 2026 21:24
@bharvey88 bharvey88 changed the title Add configurable update interval and lux offset for LTR390 sensor Add configurable update interval for LTR390 sensor Jan 31, 2026
Allows users to adjust the LTR390 sensor update frequency from the default
60 seconds to any value between 1-300 seconds through the web interface,
eliminating the need for YAML edits and firmware recompilation.

Uses ESPHome's interval component to manually poll the sensor at the
configured rate, as sensor platform update_interval is not templatable.

The setting is exposed as a number input under the CONFIG entity category
with values persisted across device reboots.

Resolves #54

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@bharvey88 bharvey88 force-pushed the feature/ltr390-configurable-settings branch from 0c067fa to 9dd9982 Compare January 31, 2026 21:35
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@Integrations/ESPHome/Core.yaml`:
- Around line 164-176: Add a new template number sensor named ltr390_lux_offset
(same pattern as DPS310 temperature offset: restore_value true, initial_value 0,
wide min/max, unit "lx", entity_category "CONFIG", optimistic true,
update_interval never, step 1, mode box) right after the existing template
numbers block; then update the LTR390 light filter lambda so the filter uses the
offset by changing the current_value assignment from the hardcoded value to
incorporate the offset (replace the current_value assignment with one that adds
id(ltr390_lux_offset).state), ensuring you reference the id ltr390_lux_offset in
the lambda.

bharvey88 and others added 2 commits February 2, 2026 12:04
Add check for first boot condition (last_update == 0 && current_time > 0)
to trigger an immediate sensor update instead of waiting for the full
configured interval to elapse.

The current_time > 0 guard prevents edge case where both values are 0
in the first second after boot.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link

@coderabbitai coderabbitai bot left a comment

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)

164-177: Consider setting disabled_by_default: false for better UX discovery.

In Home Assistant, entities with disabled_by_default: true are discovered but remain disabled until manually enabled. If the intent is for users to easily configure this interval, requiring the extra step of enabling it first adds friction to the workflow.

Suggested change
   - platform: template
     name: LTR390 Update Interval
     id: ltr390_update_interval
-    disabled_by_default: true
+    disabled_by_default: false
     restore_value: true
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Integrations/ESPHome/Core.yaml` around lines 164 - 177, The template sensor
"LTR390 Update Interval" (id ltr390_update_interval) is marked
disabled_by_default: true which hides it from users; change disabled_by_default
to false in the entity definition so the update-interval entity is enabled by
default and easier for users to discover and configure.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@Integrations/ESPHome/Core.yaml`:
- Around line 164-177: The template sensor "LTR390 Update Interval" (id
ltr390_update_interval) is marked disabled_by_default: true which hides it from
users; change disabled_by_default to false in the entity definition so the
update-interval entity is enabled by default and easier for users to discover
and configure.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9dd9982 and 1f5d0ae.

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

@bharvey88 bharvey88 merged commit 0a5b139 into beta Mar 2, 2026
11 checks passed
@bharvey88 bharvey88 deleted the feature/ltr390-configurable-settings branch March 2, 2026 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants