Skip to content

πŸ’‘ Create dynamic timer-based automations in Home Assistant with full control over hours, minutes, and seconds – all from the UI. Self-disabling, customizable, and perfect for notifications or delayed actions. πŸ‘‰ Open-source: github.com/Bacard1/HASS-Flexible-Timer

License

Notifications You must be signed in to change notification settings

Bacard1/HASS-Flexible-Timer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

⏱️ HASS - FLEXIBLE TIMER AUTOMATION

License: MIT GitHub last commit hacs_badge

Home Assistant Donate via PayPal Script

Π‘ΡŠΠ»Π³Π°Ρ€ΡΠΊΠΈ Deutch English

🌟 This project demonstrates a fully dynamic, self-disabling timer automation in Home Assistant, configurable via UI (hours/minutes/seconds).


πŸ“š Table of Contents


πŸ“¦ KEY FEATURES

  • πŸ•’ UI Configuration (hours/minutes/seconds)
  • ⚑ Self-disabling after execution
  • πŸ”„ Reset without restart
  • πŸ“… Stores last execution time
  • πŸ›‘οΈ Prevents false triggers

πŸ”§ CONFIGURATION

1. Interval Setting input_number

input_number:
  interval_hours:
    name: "Interval - Hours"
    min: 0
    max: 24
    step: 1
    unit_of_measurement: "h"
    
  interval_minutes:
    name: "Interval - Minutes"
    min: 0
    max: 59
    step: 1
    unit_of_measurement: "min"
    
  interval_seconds:
    name: "Interval - Seconds"
    min: 5  # Minimum 5 sec for stability
    max: 59
    step: 1
    unit_of_measurement: "sec"

πŸ’‘ Defines the interval. Values are summed automatically (1h + 30min = 5400 sec).


2. input_datetime: Store Last Execution Time

input_datetime:
  last_execution_time:
    name: "Last Execution"
    has_date: true
    has_time: true

⏳ This component records when the timer last started. Critical for calculations!


3. Timer Logic Automation

automation:
  - alias: "⏱️ TIMER"
    id: auto_flexible_interval
    description: "Executes action after set interval"
    trigger:
      - platform: time_pattern
        seconds: "/1"  # Checks every second
    condition: >
      {% set h = states('input_number.interval_hours') | int %}
      {% set m = states('input_number.interval_minutes') | int %}
      {% set s = states('input_number.interval_seconds') | int %}
      {% set interval = h * 3600 + m * 60 + s %}
      {% set last = states('input_datetime.last_execution_time') %}
      {{ (now() - strptime(last, '%Y-%m-%d %H:%M:%S')).total_seconds() >= interval if last not in ['unknown', 'unavailable'] else false %}
    action:
      - service: notify.all_devices  # πŸ‘ˆ Replace with your service!
        data:
          message: "πŸ”„ Action executed at {{ now().strftime('%H:%M:%S') }}"
          
      - service: input_datetime.set_datetime
        data:
          entity_id: input_datetime.last_execution_time
          datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
          
      - delay: 00:00:02  # Brief pause before disabling
      
      - service: automation.turn_off  # πŸ”Œ SELF-DISABLING
        target:
          entity_id: "{{ this.entity_id }}"

πŸ” How It Works?
1️⃣ Checks every second if interval has elapsed
2️⃣ Calculates total time in seconds
3️⃣ Compares with last execution
4️⃣ If condition met β†’ executes action and disables itself


4. Control Scripts

script:
  start_interval_timer:
    alias: "▢️ Start Timer"
    sequence:
      - service: input_datetime.set_datetime  # πŸ“Œ Sets initial time
        data:
          entity_id: input_datetime.last_execution_time
          datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
          
      - service: automation.turn_on  # πŸš€ Starts automation
        target:
          entity_id: automation.auto_flexible_interval

  reset_interval_timer:
    alias: "πŸ”„ Reset Timer"
    sequence:
      - service: input_datetime.set_datetime  # πŸ”„ Updates time without stopping
        data:
          entity_id: input_datetime.last_execution_time
          datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

5. Lovelace UI Example

type: vertical-stack
cards:
  - type: entities
    title: "⏱️ Control Panel"
    entities:
      - input_number.interval_hours
      - input_number.interval_minutes
      - input_number.interval_seconds
      - input_datetime.last_execution_time
      
  - type: horizontal-stack
    cards:
      - type: button
        name: "▢️ START"
        icon: mdi:play
        tap_action:
          action: call-service
          service: script.start_interval_timer
          
      - type: button
        name: "πŸ”„ RESET"
        icon: mdi:restart
        tap_action:
          action: call-service
          service: script.reset_interval_timer
          
      - type: button
        name: "⏹️ STOP"
        icon: mdi:stop
        tap_action:
          action: call-service
          service: automation.turn_off
          target:
            entity_id: automation.auto_flexible_interval

🎨 Customize with card-mod for 3D buttons and animations!


πŸš€ Usage Examples

  • 🌑️ AC: Turn off after 2 hours
  • πŸ’‘ Lights: Night mode (30min)
  • πŸ”” Notifications: Reminder every 15 minutes

πŸ’‘ Pro Tips

  1. ⏱️ Precision: Use seconds: "/1" for second-level accuracy
  2. πŸ”„ Reset: Always use reset_interval_timer script instead of manual setting
  3. πŸ“Š Visualization: Add history_graph to track executions

πŸ“Š Process Diagram

img

🎨 Timer Card Visualization

πŸ“± What Does the Card Look Like?

Your timer card will appear as a modern, semi-transparent control panel with three main sections:

  1. ⏱️ Interval Settings

    • Three columns for hours/minutes/seconds with styled +/- buttons
    • Shows current set time (e.g., "0h 5min 30s")
    • Bottom row shows last execution (e.g., "5/20 8:30 PM")
  2. πŸ”Ό START Button

    • Green accent (typically)
    • Starts timer via script.reset_interval_timer
    • Visible only when automation is off
  3. πŸ”΄ STOP Button

    • Red accent
    • Stops automation via script.turn_on
    • Visible only when automation is active

πŸ“¦ Required Additional Packages

To make the card work, install these add-ons:

# In HACS (Home Assistant Community Store):
- bubble-card (for pop-up effect)
- numberbox-card (for styled numeric inputs)
- card-mod (for CSS customization)
- button-card (for additional effects)

πŸ” Code Details

Bubble Card Configuration

type: custom:bubble-card
name: TIMER
card_type: pop-up  # Creates "pop-out" effect
hide_backdrop: true  # Hides background

Numberbox Styles

type: custom:numberbox-card
entity: input_number.interval_seconds
name: Seconds
unit: s.  # Shows units
card_mod:
  style: |
    ha-card {
      border-radius: 15px;  # Rounded corners
      box-shadow: 0 4px 8px rgba(0,0,0,0.2);  # 3D effect
    }

Visibility Conditions

visibility:
  - condition: state
    entity: automation.taimer
    state_not: "on"  # Shows START only when timer is off

πŸ’‘ Customization

  1. Colors: Change rgba(0, 0, 0, 0.35) to other colors (e.g., rgba(25, 25, 112, 0.4) for midnight blue)
  2. Animations: Add transition: all 0.3s ease; for smooth hover effects
  3. Icons: Replace mdi:restart with other icons from Material Design Icons

Result:
Timer Visualization


Tip

If you like this project, find more interesting repositories HERE.
For questions or issues, don't hesitate to contact me.

About

πŸ’‘ Create dynamic timer-based automations in Home Assistant with full control over hours, minutes, and seconds – all from the UI. Self-disabling, customizable, and perfect for notifications or delayed actions. πŸ‘‰ Open-source: github.com/Bacard1/HASS-Flexible-Timer

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published