-
Notifications
You must be signed in to change notification settings - Fork 0
Custom Automation Conditions
Clockwork provides custom automation conditions that allow you to use time-based logic directly in your Home Assistant automations without creating intermediate sensors.
Compare how long it's been since an entity last changed state.
- Open Settings > Automations & Scenes > Create Automation
- Click Create new automation
- Add a Trigger (e.g., Time pattern, State change)
- Click Add condition
- Search for and select "Clockwork Last Changed"
- Select the entity to monitor in the Entity ID field
- Configure one of the comparison operators:
- Above (seconds): Returns True if time since last change is greater than this value
- Below (seconds): Returns True if time since last change is less than this value
- Equal to (seconds): Returns True if time since last change equals this value
- Save the automation
The visual editor generates YAML with an options section. This is the correct format and will look like:
condition: clockwork.timespan
options:
entity_id: binary_sensor.driveway_motion
above: 300Condition Parameters:
- entity_id (required): The entity to monitor
-
above: Return
Trueif seconds since last change is greater than this value -
below: Return
Trueif seconds since last change is less than this value -
equal_to: Return
Trueif seconds since last change equals this value
At least one comparison operator (above, below, or equal_to) must be specified.
Standard Automation Fields:
- alias (optional): A friendly name for this condition (useful for organizing complex automations)
-
enabled (optional): Whether this condition is enabled (
trueorfalse)
condition: clockwork.timespan
options:
entity_id: binary_sensor.driveway_motion
below: 120
alias: Mailbox checked in last 2 minutesIf the condition appears but isn't evaluating correctly:
1. Verify Entity ID
- The entity must exist and have a valid
last_changedtimestamp - Check that the entity ID is correct (use the dropdown in the UI)
- Binary sensors usually work best:
binary_sensor.xxx
2. Check Entity State History
- Go to the entity's page
- Verify it shows "Last changed" with a recent timestamp
- If "Last changed" is missing, the condition won't work
3. Test with Simple Threshold
condition: clockwork.timespan
options:
entity_id: binary_sensor.driveway_motion
above: 0 # Should always be True if entity has ever changed4. Review Logs During Automation Execution
- Enable debug logging for Clockwork
- Check Settings > System > Logs while running the automation
- Look for
Clockwork timespan condition:debug messages - This shows the calculated seconds and comparison result
Add to your configuration.yaml to see detailed debugging:
logger:
logs:
custom_components.clockwork: debug
custom_components.clockwork.conditions: debugThen restart Home Assistant and check Settings > System > Logs for detailed output.
Compare how long it's been since an automation was last triggered.
- Open Settings > Automations & Scenes > Create Automation
- Click Create new automation
- Add a Trigger (e.g., Time pattern, State change)
- Click Add condition
- Search for and select "Clockwork Last Triggered"
- Select the automation entity to monitor in the Automation Entity field (only automation.* entities are shown)
- Configure one of the comparison operators:
- Above (seconds): Returns True if time since last trigger is greater than this value
- Below (seconds): Returns True if time since last trigger is less than this value
- Equal to (seconds): Returns True if time since last trigger equals this value
- Save the automation
The visual editor generates YAML with an options section. This is the correct format and will look like:
condition: clockwork.last_triggered
options:
entity_id: automation.lights_evening
above: 3600Condition Parameters:
-
entity_id (required): The automation entity to monitor (must be
automation.*) -
above: Return
Trueif seconds since last trigger is greater than this value -
below: Return
Trueif seconds since last trigger is less than this value -
equal_to: Return
Trueif seconds since last trigger equals this value
At least one comparison operator (above, below, or equal_to) must be specified.
Standard Automation Fields:
- alias (optional): A friendly name for this condition (useful for organizing complex automations)
-
enabled (optional): Whether this condition is enabled (
trueorfalse)
condition: clockwork.last_triggered
options:
entity_id: automation.security_system
below: 300
alias: Security system triggered in last 5 minutes- alias: "Long idle timeout"
trigger:
platform: time_pattern
minutes: "/1" # Check every minute
condition:
- condition: clockwork.timespan
options:
entity_id: binary_sensor.driveway_motion
above: 300 # 5 minutes = 300 seconds
action:
- service: light.turn_off
entity_id: light.driveway- alias: "Send notification if motion detected recently"
trigger:
platform: time
at: "08:00:00"
condition:
- condition: clockwork.timespan
options:
entity_id: binary_sensor.driveway_motion
below: 600 # Motion detected in the last 10 minutes
action:
- service: notify.mobile_app
data:
message: "Motion was detected near the driveway recently"- alias: "Specific duration check"
trigger:
platform: state
entity_id: sensor.event
condition:
- condition: clockwork.timespan
options:
entity_id: sensor.event
equal_to: 120 # Exactly 2 minutes
action:
- service: script.my_action- alias: "Re-run automation if it hasn't triggered recently"
trigger:
platform: time_pattern
hours: "/1" # Check every hour
condition:
- condition: clockwork.last_triggered
options:
entity_id: automation.daily_backup
above: 86400 # 24 hours = 86400 seconds
action:
- service: automation.trigger
entity_id: automation.daily_backup- alias: "Send notification if security system triggered recently"
trigger:
platform: time
at: "08:00:00"
condition:
- condition: clockwork.last_triggered
options:
entity_id: automation.security_alert
below: 3600 # Triggered in the last hour
action:
- service: notify.mobile_app
data:
message: "Security system was triggered recently"- alias: "Specific trigger timing check"
trigger:
platform: state
entity_id: sensor.system_status
condition:
- condition: clockwork.last_triggered
options:
entity_id: automation.system_monitor
equal_to: 300 # Exactly 5 minutes ago
action:
- service: script.precise_timing_actioncondition: template
value_template: "{{ (now() - states.binary_sensor.driveway_motion.last_changed).total_seconds() > 300 }}"condition: clockwork.timespan
options:
entity_id: binary_sensor.driveway_motion
above: 300Benefits:
- ✅ Cleaner, more readable YAML
- ✅ No template syntax needed
- ✅ Easier to maintain and modify
- ✅ Better error handling and logging
- ✅ Calculates on-the-fly, no continuous sensor updates needed
- Long Idle Detection: Trigger actions when devices haven't changed state for extended periods
- Recent Activity Detection: Check if something happened recently without needing a separate sensor
- Time-based Cleanup: Turn off devices if no activity detected for X seconds
- Event Debouncing: Skip actions if an event happened too recently
- Automation Optimization: Reduce the number of sensors needed for time-based logic
Unlike timespan sensors which update continuously, this automation condition:
- Calculates the timespan only when the automation is evaluated
- Doesn't require persistent updating in the background
- Has minimal performance impact
- Perfect for low-frequency automation checks
Choose this condition when you don't need continuous monitoring of timespan values. Choose timespan sensors when you want to display or track the value continuously in the UI.
- Timespan Calculations - Create persistent sensors that track timespan
- Offset Calculations - Time-delayed triggers with multiple modes
condition: template
value_template: "{{ (now() - states.binary_sensor.driveway_motion.last_changed).total_seconds() > 300 }}"condition: clockwork.timespan
options:
entity_id: binary_sensor.driveway_motion
above: 300Benefits:
- ✅ Cleaner, more readable YAML
- ✅ No template syntax needed
- ✅ Easier to maintain and modify
- ✅ Better error handling and logging
- ✅ Calculates on-the-fly, no continuous sensor updates needed
- Long Idle Detection: Trigger actions when devices haven't changed state for extended periods
- Recent Activity Detection: Check if something happened recently without needing a separate sensor
- Time-based Cleanup: Turn off devices if no activity detected for X seconds
- Event Debouncing: Skip actions if an event happened too recently
- Automation Optimization: Reduce the number of sensors needed for time-based logic
- If the entity doesn't exist, the condition returns
Falseand logs a warning - If the entity has no
last_changedtime, the condition returnsFalse - If no comparison operator is specified (
above,below,equal_to), the condition returnsTrueif the entity exists
Unlike timespan sensors which update continuously, this automation condition:
- Calculates the timespan only when the automation is evaluated
- Doesn't require persistent updating in the background
- Has minimal performance impact
- Perfect for low-frequency automation checks
Choose this condition when you don't need continuous monitoring of timespan values. Choose timespan sensors when you want to display or track the value continuously in the UI.
- Timespan Calculations - Create persistent sensors that track timespan
- Offset Calculations - Time-delayed triggers with multiple modes