Skip to content

feat: add runtime/duration-based task triggers (sensor value monitoring) - #116

Open
Seidlm wants to merge 3 commits into
TJPoorman:mainfrom
Seidlm:feature/runtime-based-tasks
Open

feat: add runtime/duration-based task triggers (sensor value monitoring)#116
Seidlm wants to merge 3 commits into
TJPoorman:mainfrom
Seidlm:feature/runtime-based-tasks

Conversation

@Seidlm

@Seidlm Seidlm commented Feb 28, 2026

Copy link
Copy Markdown

Runtime/Duration-Based Task Triggers

Related to #109 - extends the integration with a third trigger type.

Problem

Many smart home devices report cumulative usage metrics - for example, a Husqvarna robotic lawn mower tracks how many hours the blades have been running. Users need a way to trigger maintenance tasks based on these runtime values (e.g., "replace blades after 100 hours").

This can't be solved with time-based intervals (the mower doesn't run every day) or count-based triggers (it's not about on/off cycles, but accumulated hours).

Solution

A new trigger type: Runtime-based - monitors a numeric sensor entity and triggers a task when the accumulated value exceeds a threshold.

How It Works

  1. User creates a task with Trigger Type: Runtime
  2. Selects a sensor entity (e.g., sensor.husqvarna_blade_hours)
  3. Sets a threshold (e.g., 100 hours)
  4. The integration records the current sensor value as baseline
  5. Task becomes due when: current_value - baseline >= threshold
  6. On complete ? baseline resets to current sensor value
  7. Cycle repeats

Automatic Sensor Reset Detection

Some devices reset their counters (e.g., after blade replacement the mower resets hours to 0). The integration handles this automatically:

  • If current_value < baseline ? external reset detected
  • Baseline is automatically adjusted to 0
  • Counting continues from the new value

Example:

Step Sensor Value Baseline Delta Threshold Due?
Task created 450h 450 0 100 No
Normal use 520h 450 70 100 No
Threshold hit 550h 450 100 100 Yes ?
Task completed 550h 550 0 100 No
Blades replaced, sensor resets 0h 0 (auto) 0 100 No
Normal use resumes 100h 0 100 100 Yes ?

Changes

Backend (Python):

  • store.py - 3 new fields: runtime_entity_id, runtime_threshold, runtime_baseline. Auto-reads sensor value as baseline on task creation and completion.
  • binary_sensor.py - New _update_state_runtime() with delta calculation, reset detection. Exposes attributes: runtime_entity_id, runtime_threshold, runtime_baseline, runtime_current, runtime_delta
  • __init__.py - State change listener for runtime entities, detects external resets
  • websocket.py - Accepts runtime fields on add/update, auto-populates baseline

Frontend (Panel):

  • "Runtime-based" option in trigger type dropdown (create + edit)
  • Entity selector filtered to sensor domain
  • Threshold number input
  • Table shows delta/threshold (e.g., "72.5 / 100") with red highlighting when due

Backward Compatibility

  • Fully backward compatible - existing time-based and count-based tasks work unchanged
  • New fields have safe defaults
  • No breaking changes to existing services or WebSocket API

Use Cases

  • ?? Robotic lawn mower blade replacement (hours)
  • ??? Exercise equipment maintenance (usage hours)
  • ?? Car service intervals (km via OBD sensor)
  • ??? Printer maintenance (page count sensor)
  • Any device that reports cumulative numeric usage data

Testing

Tested on Home Assistant (HAOS, Hyper-V) via HACS custom repository. Verified:

  • Runtime delta calculation works correctly
  • External sensor reset (to 0) is detected and handled
  • Task becomes due at threshold
  • Baseline resets on completion
  • Existing time and count tasks unaffected

Michael Seidl and others added 3 commits February 28, 2026 09:31
Adds support for count-based maintenance tasks (e.g., after 30 washing
machine cycles, trigger a cleaning task).

New features:
- trigger_type field: 'time' (existing) or 'count' (new)
- Track HA entity state changes to increment counter
- Counter resets to 0 when task is completed
- New services: increment_count, reset_count
- Frontend: dynamic form for time vs count tasks
- Full backward compatibility with existing tasks

Closes TJPoorman#109
- New trigger_type 'runtime' monitors numeric sensor values (e.g., blade hours)
- Task becomes due when sensor delta exceeds configured threshold
- Auto-detects sensor resets (value drops below baseline)
- Baseline auto-set from current sensor value on task creation
- Baseline updates to current value on task completion
- Full frontend support: create, edit, display runtime tasks
- Backward compatible with existing time and count tasks
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