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

Add Count-Up Timer Functionality #200

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

gazambuja
Copy link

This pull request introduces a significant new feature to the timer-bar-card: count-up timer functionality. This allows the card to display the elapsed time since a specified event, in addition to the existing countdown timer capabilities.

Key Features and Changes:

  • Count-Up Mode: The card can now operate as a count-up timer, displaying the time elapsed since an entity's last_changed attribute (or a custom start time, if configured). This is activated by using the max_value option instead of the duration option.
  • max_value Option: This new option controls the count-up behavior:
    • Specific Time: A string like "1h", "30m", "90s" sets a fixed maximum value for the progress bar. The bar fills completely when the elapsed time reaches this value.
    • "auto": Enables automatic expansion of the maximum value. The bar's maximum value increases as the elapsed time approaches the current maximum, creating a continuously expanding time window.
  • auto_increment Option: When max_value is set to "auto", this option controls:
    • The initial maximum value of the bar.
    • The amount by which the maximum value increases when the elapsed time gets close to the current maximum (within 10% of the increment).
    • Defaults to "1h" if not specified. Accepts values like "15m", "2h", "30s".
  • Automatic Time Format Switching: The time display now automatically switches between MM:SS (for times under 60 minutes) and HH:MM:SS (for times 60 minutes and over) when resolution: automatic is set. This improves readability.
  • Mutual Exclusivity of duration and max_value: The code now ensures that if max_value is set, duration is effectively ignored (treated as null), and vice versa. This provides a clear and unambiguous way to switch between countdown and count-up modes.
  • Improved Type Safety: Addressed TypeScript errors related to optional parameters and type mismatches.
  • Comprehensive Documentation: Updated the README.md with detailed explanations of the new features, configuration options, and examples. Added a new section highlighting the count-up functionality, similar to the existing Mushroom styling section.
  • Updated Files: types.ts, timer-bar-card.ts, timer-bar-entity-row.ts, timer-bar-mushroom-row.ts,helpers.ts and format-time.ts.

Code Structure and Implementation Details:

  • The core logic for count-up is handled within helpers.ts (in timerTimeRemaining and timerTimePercent) and timer-bar-entity-row.ts (managing the _currentMaxValue state for the auto mode).
  • The existing countdown functionality is fully preserved and remains the default behavior if duration is used.
  • The code is designed to be backward-compatible with existing configurations.

Testing:

  • The new functionality has been thoroughly tested with various configurations, including:
    • Basic count-up with a fixed max_value.
    • Count-up with max_value: auto.
    • Different auto_increment values.
    • Combinations of count-up and countdown configurations.
    • Edge cases (entity unavailability, invalid last_changed, etc.).
    • No unit test where added

Acknowledgments:

This feature was developed by Gustavo with significant assistance from Google's AI Studio. The collaborative process involved iterative refinement of the code, ensuring correct logic, type safety, and comprehensive documentation.

Example Configuration (Count-Up):

type: custom:timer-bar-card
entity: binary_sensor.oven_on  # Replace with your entity
max_value: 2h  # Bar fills completely after 2 hours

Gustavo added 6 commits February 13, 2025 20:41
…ed max_value

This commit introduces a major new feature: a count-up timer mode for the `timer-bar-card`.  This allows you to display a progress bar that fills up over time, starting from a specified start time, either until a fixed maximum value is reached, or with a dynamically adjusting maximum.

**Key Features:**

*   **Count-Up Mode:** The card now supports both countdown (existing functionality) and count-up modes.  The mode is automatically selected based on the presence of `duration` (countdown) or `max_value` (count-up) in the configuration.
*   **`max_value`:**  Specifies the maximum value for the count-up timer.  This can be a fixed duration string (e.g., "1y", "30m", "2h", "1d") or the special value `"auto"`.
*   **`auto_increment`:** When `max_value` is set to `"auto"`, the `auto_increment` option (optional) controls how much the maximum value increases when the elapsed time gets close to the current maximum.  Defaults to 1 hour.  Uses the same duration string format as `max_value`.
*   **`start_time`:**  Allows you to specify a custom start time for the count-up timer.  This can be:
    *   **`fixed`:**  A fixed date/time string (e.g., "2023-10-26T12:00:00").
    *   **`entity`:** The state or attribute of another Home Assistant entity.  This is particularly useful for using an `input_datetime` helper as the start time.
        *   `entity`: (Required) The entity ID.
        *   `attribute`: (Optional) The attribute of the entity to use (e.g., `timestamp` for `input_datetime`).
        *   `units`: (Required when using `attribute`) Specifies the units of the attribute value ("seconds", "minutes", "hours", "duration").  Use "seconds" when using the `timestamp` attribute.
*   **`active_state`:**  Consistent with the countdown timer, the count-up timer is only active when the main `entity` is in one of the specified `active_state` states.
* **Placeholder entity:** If you want use an static start time, but you don't have a sensor that always be "on", you need to add a placeholder `entity` (typically a `binary_sensor` or a template sensor) in the main `entity` field, and this must to be always `on`.
*   **Dynamic Time Formatting:**  The time display automatically switches between `MM:SS` and `HH:MM:SS` based on the elapsed time.  A new `long` format option displays years, months, days, hours, minutes, and seconds.  Custom format strings are also supported.

**Example Configuration (Count-up from input_datetime):**

```yaml
type: custom:timer-bar-card
name: Time Since Event
entity: binary_sensor.placeholder_always_on  # Placeholder entity, always "on"
start_time:
  entity: input_datetime.my_event_start_time
  attribute: timestamp
  units: seconds
max_value: auto
auto_increment: 1h
active_state: "on"
format: long
```
**Example Configuration (Count-up from sensor):**
```yaml
type: custom:timer-bar-card
name: Time Since Event
entity: binary_sensor.my_event_trigger
max_value: 2d
active_state: "on"
format: HH:MM:SS
```

**Bug Fixes:**

This commit includes several *critical* bug fixes related to the count-up timer:

*   Fixed incorrect percentage calculation when using a fixed `max_value`.  The progress bar now correctly reflects the elapsed time relative to the configured `max_value`.
*   Fixed an issue where `timerTimePercent` wasn't correctly using the dynamic `_currentMaxValue` in "auto" mode.
*   Fixed handling of `input_datetime` entities and their `timestamp` attribute for the `start_time`. The card now correctly interprets the timestamp value.
* Fixed an issue that could show `Invalid duration format` warning.
* Corrected the logic for time calculation
* Fixed issues related to types and arguments.

**Breaking Changes:**

* Although this release try to keep the compatibility, some configuration maybe need to be changed, mainly related to `duration` and `max_value` options.

**Notes:**

*   The `entity` at the top level of the card configuration determines when the timer is active (based on `active_state`).
*   The `start_time.entity` provides the *value* for the starting time.
*   When using a `input_datetime`, is recommended to use `start_time` with `attribute: timestamp` and `units: seconds`.
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.

1 participant