Add Count-Up Timer Functionality
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_changedattribute (or a custom start time, if configured). This is activated by using themax_valueoption instead of thedurationoption. max_valueOption: 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.
- Specific Time: A string like
auto_incrementOption: Whenmax_valueis 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) andHH:MM:SS(for times 60 minutes and over) whenresolution: automaticis set. This improves readability. - Mutual Exclusivity of
durationandmax_value: The code now ensures that ifmax_valueis set,durationis 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.mdwith 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.tsandformat-time.ts.
Code Structure and Implementation Details:
- The core logic for count-up is handled within
helpers.ts(intimerTimeRemainingandtimerTimePercent) andtimer-bar-entity-row.ts(managing the_currentMaxValuestate for theautomode). - The existing countdown functionality is fully preserved and remains the default behavior if
durationis 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_incrementvalues. - Combinations of count-up and countdown configurations.
- Edge cases (entity unavailability, invalid
last_changed, etc.). - No unit test where added
- Basic count-up with a fixed
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
Hi,
love the time bar for so many scenarios but for my washing machine the count-up would be also great, so are there already any news on this topic?
If not how can I install the fork directly (already tried a lot, failed a lot)?
best thanks for info/support
best regards Gerald
I haven't had time to review this yet. That's a lot of changes :) And many changes to important logic bits.
In the meantime, to run this directly:
- Either install Node.js and typescript and run
npm run devto generatedist/timer-bar-card.js, or download this attachment: timer-bar-card.txt and rename it to timer-bar-card.js. The latter is what I just compiled from the fork. - Uninstall timer-bar-card from HACS if you used HACS.
- Follow the manual installation instructions.