Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added pulse width accumulate #4622

Open
wants to merge 7 commits into
base: current
Choose a base branch
from

Conversation

patrick-kiwi
Copy link

@patrick-kiwi patrick-kiwi commented Jan 30, 2025

Description:

added docs for new sensor pulse_width_accumulate

Related issue (if applicable): fixes

Pull request in esphome with YAML changes (if applicable): esphome/esphome#

Checklist:

  • I am merging into next because this is new documentation that has a matching pull-request in esphome as linked above.
    or

  • I am merging into current because this is a fix, change and/or adjustment in the current documentation and is not for a new component or feature.

  • Link added in /index.rst when creating new documents for new components or cookbook.

Copy link
Contributor

coderabbitai bot commented Jan 30, 2025

Walkthrough

The pull request introduces new documentation for the pulse_width_accumulate sensor in ESPHome. This sensor allows users to sum the total on-time of a GPIO input, which is beneficial for measuring the on-time of rapidly switched devices such as MOSFETs or TRIACs. The documentation includes technical specifications, polling behavior, and a YAML configuration example, detailing parameters like pin, update_interval, and id. It also specifies experimental threshold values and references related sensor filters and API documentation.

Changes

File Change Summary
components/sensor/pulse_width_accumulate.rst New documentation added for the pulse_width_accumulate sensor, detailing specifications, polling behavior, and YAML configuration examples.

Suggested labels

has-parent, next

Suggested reviewers

  • jesserockz

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0cc107c and ece4fb5.

📒 Files selected for processing (1)
  • components/sensor/pulse_width_accumulate.rst (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • components/sensor/pulse_width_accumulate.rst

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@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

🧹 Nitpick comments (4)
components/sensor/pulse_width_accumulate.rst (4)

4-6: Enhance SEO description specificity.

The current SEO description is too generic. Consider making it more specific to the pulse_width_accumulate sensor's unique features.

-    :description: Instructions for setting up pulse width sensors in ESPHome
+    :description: Instructions for setting up pulse width accumulation sensors in ESPHome to measure total on-time of GPIO inputs for MOSFETs and TRIACs

16-20: Improve technical specifications clarity.

Consider using consistent unit formatting and add maximum polling window value in seconds for clarity.

-    Threshold stability values were experimentally determined for an esp32 devkit_v4. 
-    Pulse width >= 22 microseconds, pulse width delay >= 70 microseconds 
-    (ie. implied maximum frequency ~10.87 KHz)
-    There are no overflow problems ie. the pulse width can be infinitely long. 
-    However, the polling window (default 1 minute) must not be set > 71.58 minutes.         
+    Threshold stability values were experimentally determined for an ESP32 DevKit-v4:
+    - Minimum pulse width: ≥ 22 µs
+    - Minimum pulse width delay: ≥ 70 µs
+    - Maximum implied frequency: ~10.87 kHz
+    - Maximum polling window: 71.58 minutes (4295 seconds)

35-45: Improve clarity of polling behavior explanation.

The explanation of polling behavior and its limitations could be structured better for clarity.

-    The polling function also resets the microsecond counter preventing overflow 
-    at 71.58 min.  A peculiarity of the current Interrupt synchronization is the 
-    occasional sensor skip of one or more polling periods.  The sensor  
-    remains accurate because the accumulated time is pushed forward to the next 
-    polling period.  The graph below shows the sensor output under stressed 
-    conditions (22 microsecond pulses with a pulse width delay of 70 microseconds
-    and an update period of 1 second).  
-    Although the majority of readings are as expected 22/92=0.239 seconds, a number 
-    of zero, and consecutive zero readings are pushed forward.  Polling 
-    misses are less common with polling intervals >=1 min (~0.4% chance). However,
-    be warned that excessive polling periods >15 min may risk overflow.
+    Important implementation details:
+    
+    1. The polling function resets the microsecond counter to prevent overflow at 71.58 minutes
+    2. Due to interrupt synchronization, the sensor may occasionally skip polling periods
+       - The accumulated time is accurately carried forward to the next polling period
+       - Under stress testing (22 µs pulses, 70 µs delay, 1s update):
+         * Expected readings: 0.239s (22/92)
+         * Some readings show as 0 and are pushed to next period
+    3. Polling reliability:
+       - Intervals ≥1 minute have ~0.4% miss rate
+       - Warning: Intervals >15 minutes risk counter overflow

47-49: Add alt text for accessibility.

The figure lacks alt text, which is important for accessibility.

 .. figure:: images/22_70_kernel_density.png
     :align: center
     :width: 80.0%
+    :alt: Kernel density plot showing sensor output distribution under stress conditions
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3edb515 and 931bf87.

⛔ Files ignored due to path filters (1)
  • images/22_70_kernel_density.png is excluded by !**/*.png
📒 Files selected for processing (1)
  • components/sensor/pulse_width_accumulate.rst (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
components/sensor/pulse_width_accumulate.rst (1)

Pattern **: - Do not generate or add any sequence diagrams

🔇 Additional comments (1)
components/sensor/pulse_width_accumulate.rst (1)

51-69: Configuration and references section looks good!

The configuration variables are well documented and all relevant references are included.

components/sensor/pulse_width_accumulate.rst Outdated Show resolved Hide resolved
Copy link

netlify bot commented Jan 30, 2025

Deploy Preview for esphome ready!

Name Link
🔨 Latest commit ece4fb5
🔍 Latest deploy log https://app.netlify.com/sites/esphome/deploys/67a15375856bd90008bae646
😎 Deploy Preview https://deploy-preview-4622--esphome.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@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

🧹 Nitpick comments (2)
components/sensor/pulse_width_accumulate.rst (2)

15-22: Consider adding brief explanation for maximum polling window.

The note mentions a maximum polling window of 71.58 minutes with "see text below", but it would be more user-friendly to briefly explain why here, even though it's detailed later in the implementation notes.

    - Maximum implied frequency: ~10 kHz
-   - Maximum polling window: 71.58 minutes (see text below)         
+   - Maximum polling window: 71.58 minutes (limited by microsecond counter overflow)

23-32: Consider enhancing the example with output value explanations.

The example configuration is clear but would benefit from comments explaining what values to expect from both sensors.

     # Example configuration entry
     sensor:
       - platform: pulse_width_accumulate
         pin: GPIO32
         name: Cumulative on-time sensor
+        # Reports total seconds the input was HIGH during the update interval
         frequency:
           name: Average frequency sensor
+          # Reports average switching frequency in Hz during the update interval
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 931bf87 and a8faba2.

📒 Files selected for processing (1)
  • components/sensor/pulse_width_accumulate.rst (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
components/sensor/pulse_width_accumulate.rst (1)

Pattern **: - Do not generate or add any sequence diagrams

🪛 GitHub Check: build
components/sensor/pulse_width_accumulate.rst

[failure] 36-36:
Directive 'note' must be indented with 4 spaces, not 2. See components/sensor/pulse_width_accumulate.rst:36

🪛 GitHub Actions: Lint
components/sensor/pulse_width_accumulate.rst

[error] 36-36: Directive 'note' must be indented with 4 spaces, not 2

🔇 Additional comments (3)
components/sensor/pulse_width_accumulate.rst (3)

1-14: LGTM! Clear and informative introduction.

The SEO description and introduction effectively explain the sensor's purpose, functionality, and units of measurement.


48-58: LGTM! Well-documented configuration variables.

The configuration variables section is complete and includes all necessary information, including default values and references to relevant schemas.


59-67: LGTM! Comprehensive references.

The See Also section provides helpful links to related documentation and source files.

Comment on lines 34 to 46
.. note::

Important implementation details:

1. The polling function resets the microsecond counter to prevent overflow at 71.58 minutes
2. Sensor accuracy is a function of the input signal frequency:
- Test Condition #1 (25 µs pulses, 75 µs delay, 1 sec polling, 27380 samples):
* Frequency. Expected 10 kHz. Observed mean 9980.5 Hz. Standard deviation=5.5 Hz
* On-time. Expected based on time stamps 6846.75 s. Observed 6844.33 s
- Test Condition #2 (300 µs pulses, 300 µs delay, 1 min polling, 289 samples):
* Frequency, expected 1666.67 Hz, Observed mean 1666.57 Hz, standard deviation=0.08 Hz
* On-time. Expected based on time stamps 8640 s. Observed 8639.98 s

Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix note directive indentation.

The note directive must be indented with 4 spaces, not 2, to comply with RST formatting requirements.

-.. note::
-
-  Important implementation details:
-
-  1. The polling function resets the microsecond counter to prevent overflow at 71.58 minutes
-  2. Sensor accuracy is a function of the input signal frequency: 
-    - Test Condition #1 (25 µs pulses, 75 µs delay, 1 sec polling, 27380 samples):
-      * Frequency. Expected 10 kHz. Observed mean 9980.5 Hz. Standard deviation=5.5 Hz
-      * On-time. Expected based on time stamps 6846.75 s. Observed 6844.33 s
-    - Test Condition #2 (300 µs pulses, 300 µs delay, 1 min polling, 289 samples):
-      * Frequency, expected 1666.67 Hz, Observed mean 1666.57 Hz, standard deviation=0.08 Hz
-      * On-time. Expected based on time stamps 8640 s. Observed 8639.98 s  
+.. note::
+
+    Important implementation details:
+
+    1. The polling function resets the microsecond counter to prevent overflow at 71.58 minutes
+    2. Sensor accuracy is a function of the input signal frequency: 
+        - Test Condition #1 (25 µs pulses, 75 µs delay, 1 sec polling, 27380 samples):
+            * Frequency. Expected 10 kHz. Observed mean 9980.5 Hz. Standard deviation=5.5 Hz
+            * On-time. Expected based on time stamps 6846.75 s. Observed 6844.33 s
+        - Test Condition #2 (300 µs pulses, 300 µs delay, 1 min polling, 289 samples):
+            * Frequency, expected 1666.67 Hz, Observed mean 1666.57 Hz, standard deviation=0.08 Hz
+            * On-time. Expected based on time stamps 8640 s. Observed 8639.98 s
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
.. note::
Important implementation details:
1. The polling function resets the microsecond counter to prevent overflow at 71.58 minutes
2. Sensor accuracy is a function of the input signal frequency:
- Test Condition #1 (25 µs pulses, 75 µs delay, 1 sec polling, 27380 samples):
* Frequency. Expected 10 kHz. Observed mean 9980.5 Hz. Standard deviation=5.5 Hz
* On-time. Expected based on time stamps 6846.75 s. Observed 6844.33 s
- Test Condition #2 (300 µs pulses, 300 µs delay, 1 min polling, 289 samples):
* Frequency, expected 1666.67 Hz, Observed mean 1666.57 Hz, standard deviation=0.08 Hz
* On-time. Expected based on time stamps 8640 s. Observed 8639.98 s
.. note::
Important implementation details:
1. The polling function resets the microsecond counter to prevent overflow at 71.58 minutes
2. Sensor accuracy is a function of the input signal frequency:
- Test Condition #1 (25 µs pulses, 75 µs delay, 1 sec polling, 27380 samples):
* Frequency. Expected 10 kHz. Observed mean 9980.5 Hz. Standard deviation=5.5 Hz
* On-time. Expected based on time stamps 6846.75 s. Observed 6844.33 s
- Test Condition #2 (300 µs pulses, 300 µs delay, 1 min polling, 289 samples):
* Frequency, expected 1666.67 Hz, Observed mean 1666.57 Hz, standard deviation=0.08 Hz
* On-time. Expected based on time stamps 8640 s. Observed 8639.98 s
🧰 Tools
🪛 GitHub Check: build

[failure] 36-36:
Directive 'note' must be indented with 4 spaces, not 2. See components/sensor/pulse_width_accumulate.rst:36

🪛 GitHub Actions: Lint

[error] 36-36: Directive 'note' must be indented with 4 spaces, not 2

Copy link
Contributor

@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

♻️ Duplicate comments (1)
components/sensor/pulse_width_accumulate.rst (1)

36-42: ⚠️ Potential issue

Fix note directive indentation.

The content under the .. note:: directive is not indented with the required 4 spaces. This improper indentation is causing the build/lint failure.

Apply the following diff to fix the indentation:

-.. note::
-
-  Important implementation details:
-
-  1. Edge interrupts, and the polling function both reset the microsecond 
-  counter preventing overflow at 71.58 minutes. 
-  2. Sensor accuracy is determined by the input signal frequency, for example: 
-    - Test Condition #1 (25 µs pulses, 75 µs delay, 1 sec polling, 20379 samples):
-      * Frequency. Expected 10 kHz. Observed mean 9998.2 Hz. Standard deviation=0.9 Hz
-      * On-time. Expected based on time stamps 5094.25 s. Observed 5094.87 s (see figure below)
-    - Test Condition #2 (300 µs pulses, 300 µs delay, 1 sec polling, 15333 samples):
-      * Frequency, expected 1666.67 Hz, Observed mean 1666.57 Hz, standard deviation=0.50 Hz
-      * On-time. Expected based on time stamps 7666 s. Observed 7665.97 s  
-    - Test Condition #3 (random 1s to 10 min pulses, random 1 s to 1 min delay, 1 min polling, XX samples):
-      * On-time. Expected based on generation algorithm output 18404.0 s. Observed 18405.0 s (gen algo used inaccurate millis() function)
+.. note::
+
+    Important implementation details:
+
+    1. Edge interrupts, and the polling function both reset the microsecond counter preventing overflow at 71.58 minutes.
+    2. Sensor accuracy is determined by the input signal frequency, for example:
+        - Test Condition #1 (25 µs pulses, 75 µs delay, 1 sec polling, 20379 samples):
+            * Frequency. Expected 10 kHz. Observed mean 9998.2 Hz. Standard deviation=0.9 Hz
+            * On-time. Expected based on time stamps 5094.25 s. Observed 5094.87 s (see figure below)
+        - Test Condition #2 (300 µs pulses, 300 µs delay, 1 sec polling, 15333 samples):
+            * Frequency, expected 1666.67 Hz, Observed mean 1666.57 Hz, standard deviation=0.50 Hz
+            * On-time. Expected based on time stamps 7666 s. Observed 7665.97 s
+        - Test Condition #3 (random 1s to 10 min pulses, random 1 s to 1 min delay, 1 min polling, XX samples):
+            * On-time. Expected based on generation algorithm output 18404.0 s. Observed 18405.0 s (gen algo used inaccurate millis() function)
🧰 Tools
🪛 GitHub Check: build

[failure] 39-39:
Directive 'note' must be indented with 4 spaces, not 2. See components/sensor/pulse_width_accumulate.rst:39

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a8faba2 and ad723bc.

⛔ Files ignored due to path filters (1)
  • components/sensor/images/kernel_25.png is excluded by !**/*.png
📒 Files selected for processing (1)
  • components/sensor/pulse_width_accumulate.rst (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
components/sensor/pulse_width_accumulate.rst (1)

Pattern **: - Do not generate or add any sequence diagrams

🪛 GitHub Check: build
components/sensor/pulse_width_accumulate.rst

[failure] 39-39:
Directive 'note' must be indented with 4 spaces, not 2. See components/sensor/pulse_width_accumulate.rst:39


[failure] 55-55:
Directive 'figure' is not followed by an empty line. Please insert an empty line after components/sensor/pulse_width_accumulate.rst:55

Comment on lines 52 to 56

.. figure:: images/kernel_25.png
:align: center
:width: 80.0%

Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

⚠️ Potential issue

Insert an additional empty line after the figure directive options.

The .. figure:: directive is not immediately followed by a blank line, which is required by reStructuredText for proper rendering. Please insert an extra empty line immediately after the options block to satisfy the formatting rules.

Apply the following diff:

 .. figure:: images/kernel_25.png
     :align: center
     :width: 80.0%
-     
+     
+
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
.. figure:: images/kernel_25.png
:align: center
:width: 80.0%
.. figure:: images/kernel_25.png
:align: center
:width: 80.0%
🧰 Tools
🪛 GitHub Check: build

[failure] 55-55:
Directive 'figure' is not followed by an empty line. Please insert an empty line after components/sensor/pulse_width_accumulate.rst:55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant