-
Notifications
You must be signed in to change notification settings - Fork 0
Timespan Calculation
The timespan calculation measures how long an entity has been in a specific state.
A timespan sensor tracks the duration since an entity changed to a particular state. It continuously updates, showing how many seconds, minutes, or hours have elapsed.
- Go to Clockwork configuration
- Select "Add Calculation"
- Choose "Timespan Calculation"
- Fill in the following:
- Calculation Name: A friendly name for your sensor (e.g., "Door Open Duration")
- Entity to Monitor: The entity to watch for state changes
-
Track State: Which state change to track
-
on: Track time since entity turned ON -
off: Track time since entity turned OFF -
both: Track time for any state change
-
- Update Interval: How often to update the value (in seconds, minimum 1, default 60)
- Icon: Optional custom icon

{
"type": "timespan",
"name": "Front Door Open Duration",
"entity_id": "binary_sensor.front_door",
"track_state": "on",
"update_interval": 30,
"icon": "mdi:door-open"
}Measures duration since the entity turned ON.
Example: If a door sensor is currently open, the timespan shows how long the door has been open.
Measures duration since the entity turned OFF.
Example: If a door sensor is currently closed, the timespan shows how long the door has been closed.
Measures duration since the last state change (whether to ON or OFF).
Example: Shows time elapsed since the door's state last changed, regardless of direction.
{
"name": "Garage Door Open Timer",
"entity_id": "cover.garage_door",
"track_state": "on",
"update_interval": 10
}Create an automation that closes the garage if it stays open longer than 30 minutes.
{
"name": "No Motion Duration",
"entity_id": "binary_sensor.living_room_motion",
"track_state": "off",
"update_interval": 15
}Turn lights off if there's been no motion for more than 5 minutes.
{
"name": "Device Offline Time",
"entity_id": "binary_sensor.device_available",
"track_state": "off",
"update_interval": 60
}Alert if a device has been offline for more than 1 hour.
The sensor's state is represented as a number of seconds. You can display it in different formats:
{% set seconds = states('sensor.front_door_open_duration') | int(0) %}
{% set minutes = (seconds / 60) | int %}
{% set hours = (seconds / 3600) | int %}
Door has been open for:
- {{ seconds }} seconds
- {{ minutes }} minutes
- {{ hours }} hourscondition:
- condition: numeric_state
entity_id: sensor.door_open_duration
above: 1800 # 30 minutesThe Update Interval determines how frequently the state is recalculated:
- Fast Updates (1-10 seconds): More responsive but uses more resources
- Standard (30-60 seconds): Good balance for most use cases
- Slow (120+ seconds): Battery-friendly for less critical timers
- Check that the entity ID is correct
- Verify the entity is changing states
- Review Home Assistant logs for errors
- Ensure the Update Interval is set appropriately
- Check that you're tracking the correct state (on/off/both)
- Restart Home Assistant if values seem stuck
Next: Learn about Offset Calculation →
Previous: Back to Home →