Skip to content

Instantly share code, notes, and snippets.

@Blackshome
Last active January 4, 2024 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Blackshome/495198096386f0db2cdaf91c0bda50ea to your computer and use it in GitHub Desktop.
Save Blackshome/495198096386f0db2cdaf91c0bda50ea to your computer and use it in GitHub Desktop.
trigger-run-on-timer.yaml
blueprint:
name: Trigger - Run ON Timer
description: >
# ⏱️ Trigger - Run ON Timer
**Version: 1.6**
Let us know what you think of this blueprint and for community support including updates: [Click Here](https://community.home-assistant.io/t/trigger-run-on-timer/485176)
**The Automation Process:**
- When a trigger is activated, it turns the selected entities ON and then OFF automatically in a pre-set time period.
- You can set 4 different triggers. Each trigger can have a different time period.
- The entities will stay ON if the trigger has been activated again before the time delay turns the entity OFF. It will then reset the time delay after the last trigger event.
- You have the option to use a timer helper. This is useful if you would like to display the remaining time in a dashboard and the timer will survive a HA restart.
- HA Restart - If you use a timer helper it will resume as nothing happened (recommended). If you don't use a timer helper, then when HA restarts, the automation will try and reset back to the start of trigger 1 time.
Required = *
**If you like my blueprints, and would like to show your support or just say thank you?** [Click Here](https://www.paypal.com/donate/?hosted_button_id=WAZS3QSDTPGA8) 🙂
domain: automation
input:
trigger_state:
name: The Trigger State
description: Select what state the trigger will be in to run the automation.
default: "on"
selector:
select:
options:
- label: Turn "ON" State
value: "on"
- label: Turn "OFF" State
value: "off"
- label: Any State Change - Example button helper
value: "state"
trigger_one:
name: Trigger One *
description: The trigger to start the timer. Example - Press Button, Press Mechanism, Motion Sensor, Contact Sensor, Door Sensor, etc.
selector:
entity:
multiple: true
trigger_one_time:
name: Trigger One Time
description: Set the delay for trigger one.
default: 00:00:00
selector:
duration:
entity_input:
name: Entities To Turn ON *
description: The entities that get turned ON by the trigger and OFF with the time delay.
selector:
target:
include_timer_helper:
name: Use The Timer Helper Options (Optional)
description: Select enable if you would like to use a timer helper.
**Note** - When creating your timer helper, make sure you tick the box "Restore" so it will survive a HA restart.
Don't set any time in the timer helper duration, use the time settings below.
default: disable_timer_helper
selector:
select:
options:
- label: Use a timer helper
value: "enable_timer_helper"
- label: Dont use a timer helper
value: "disable_timer_helper"
timer_helper:
name: Timer Helper
description: Select the timer helper to be used if you have chosen to use a timer helper above.
default: []
selector:
entity:
filter:
domain:
- timer
include_extra_triggers:
name: Use The Extra Triggers (Optional)
description: Select any additional trigger to be used.
default: []
selector:
select:
multiple: true
options:
- label: Use trigger two
value: "use_trigger_two"
- label: Use trigger three
value: "use_trigger_three"
- label: Use trigger four
value: "use_trigger_four"
trigger_two:
name: Trigger Two
description: The trigger to start the timer. Example - Press Button, Press Mechanism, Motion Sensor, Contact Sensor, Door Sensor, etc.
default: []
selector:
entity:
multiple: true
trigger_two_time:
name: Trigger Two Time
description: Set the delay for trigger two.
default: 00:00:00
selector:
duration:
trigger_three:
name: Trigger Three
description: The trigger to start the timer. Example - Press Button, Press Mechanism, Motion Sensor, Contact Sensor, Door Sensor, etc.
default: []
selector:
entity:
multiple: true
trigger_three_time:
name: Trigger Three Time
description: Set the delay for trigger three.
default: 00:00:00
selector:
duration:
trigger_four:
name: Trigger Four
description: The trigger to start the timer. Example - Press Button, Press Mechanism, Motion Sensor, Contact Sensor, Door Sensor, etc.
default: []
selector:
entity:
multiple: true
trigger_four_time:
name: Trigger Four Time
description: Set the delay for trigger four.
default: 00:00:00
selector:
duration:
mode: restart
max_exceeded: silent
variables:
trigger_state: !input trigger_state
trigger_one: !input trigger_one
trigger_one_time: !input trigger_one_time
entity_input: !input entity_input
include_timer_helper: !input include_timer_helper
timer_helper: !input timer_helper
include_extra_triggers: !input include_extra_triggers
trigger_two: !input trigger_two
trigger_two_time: !input trigger_two_time
trigger_three: !input trigger_three
trigger_three_time: !input trigger_three_time
trigger_four: !input trigger_four
trigger_four_time: !input trigger_four_time
trigger:
- platform: state
entity_id: !input trigger_one
to: !input trigger_state
id: 't1_to_state'
- platform: state
entity_id: !input trigger_one
id: 't1_no_state'
- platform: state
entity_id: !input trigger_two
to: !input trigger_state
id: 't2_to_state'
- platform: state
entity_id: !input trigger_two
id: 't2_no_state'
- platform: state
entity_id: !input trigger_three
to: !input trigger_state
id: 't3_to_state'
- platform: state
entity_id: !input trigger_three
id: 't3_no_state'
- platform: state
entity_id: !input trigger_four
to: !input trigger_state
id: 't4_to_state'
- platform: state
entity_id: !input trigger_four
id: 't4_no_state'
- platform: state
entity_id: !input timer_helper
to: "idle"
id: 't5'
- platform: homeassistant
id: "t6"
event: start
# All Conditions
condition:
#Trigger conditions
- condition: or
conditions:
- condition: and # trigger for ON and OFF state
conditions:
- "{{ (trigger_state == 'on') or (trigger_state == 'off') }}"
- condition: trigger
id:
- 't1_to_state'
- condition: and
conditions:
- "{{ (trigger_state == 'on') or (trigger_state == 'off') }}"
- condition: trigger
id: 't2_to_state'
- "{{ 'use_trigger_two' in include_extra_triggers }}"
- condition: and
conditions:
- "{{ (trigger_state == 'on') or (trigger_state == 'off') }}"
- condition: trigger
id: 't3_to_state'
- "{{ 'use_trigger_three' in include_extra_triggers }}"
- condition: and
conditions:
- "{{ (trigger_state == 'on') or (trigger_state == 'off') }}"
- condition: trigger
id: 't4_to_state'
- "{{ 'use_trigger_four' in include_extra_triggers }}"
- condition: and # trigger for any state change
conditions:
- "{{ trigger_state == 'state' }}"
- condition: trigger
id:
- 't1_no_state'
- condition: and
conditions:
- "{{ trigger_state == 'state' }}"
- condition: trigger
id: 't2_no_state'
- "{{ 'use_trigger_two' in include_extra_triggers }}"
- condition: and
conditions:
- "{{ trigger_state == 'state' }}"
- condition: trigger
id: 't3_no_state'
- "{{ 'use_trigger_three' in include_extra_triggers }}"
- condition: and
conditions:
- "{{ trigger_state == 'state' }}"
- condition: trigger
id: 't4_no_state'
- "{{ 'use_trigger_four' in include_extra_triggers }}"
- condition: and # trigger for end timer helper
conditions:
- condition: trigger
id: 't5'
- "{{include_timer_helper == 'enable_timer_helper'}}"
- "{{ expand(entity_input.entity_id) | selectattr('state', '==', 'on') | list | count > 0 }}"
- condition: and # trigger by HA Restart, check if the timer helper is disabled & check if any triggers are on
conditions:
- condition: trigger
id: 't6'
- "{{include_timer_helper == 'disable_timer_helper'}}"
- "{{ expand(entity_input.entity_id) | selectattr('state', '==', 'on') | list | count > 0 }}"
action:
- choose:
- alias: "Check if the time helper is enabled"
conditions:
- "{{include_timer_helper == 'enable_timer_helper'}}"
- condition: trigger
id:
- 't1_to_state'
- 't2_to_state'
- 't3_to_state'
- 't4_to_state'
- 't1_no_state'
- 't2_no_state'
- 't3_no_state'
- 't4_no_state'
sequence:
- alias: "Turn ON the entities"
service: homeassistant.turn_on
target: !input entity_input
- choose:
- alias: "For trigger one with a time helper"
conditions:
- condition: trigger
id:
- 't1_to_state'
- 't1_no_state'
sequence:
- alias: "Turn ON the timer helper"
service: timer.start
entity_id: !input timer_helper
data:
duration: !input trigger_one_time
- alias: "For trigger two with a time helper"
conditions:
- condition: trigger
id:
- 't2_to_state'
- 't2_no_state'
sequence:
- alias: "Turn ON the timer helper"
service: timer.start
entity_id: !input timer_helper
data:
duration: !input trigger_two_time
- alias: "For trigger three with a time helper"
conditions:
- condition: trigger
id:
- 't3_to_state'
- 't3_no_state'
sequence:
- alias: "Turn ON the timer helper"
service: timer.start
entity_id: !input timer_helper
data:
duration: !input trigger_three_time
- alias: "For trigger four with a time helper"
conditions:
- condition: trigger
id:
- 't4_to_state'
- 't4_no_state'
sequence:
- alias: "Turn ON the timer helper"
service: timer.start
entity_id: !input timer_helper
data:
duration: !input trigger_four_time
- alias: "Check if the time helper is disabled"
conditions:
- "{{include_timer_helper == 'disable_timer_helper'}}"
- condition: trigger
id:
- 't1_to_state'
- 't2_to_state'
- 't3_to_state'
- 't4_to_state'
- 't1_no_state'
- 't2_no_state'
- 't3_no_state'
- 't4_no_state'
sequence:
- alias: "Turn ON the entities"
service: homeassistant.turn_on
target: !input entity_input
- choose:
- alias: "For trigger one with a no time helper"
conditions:
- condition: trigger
id:
- 't1_to_state'
- 't1_no_state'
sequence:
- alias: "Wait for the time that has been set"
delay: !input trigger_one_time
- alias: "For trigger two with a no time helper"
conditions:
- condition: trigger
id:
- 't2_to_state'
- 't2_no_state'
sequence:
- alias: "Wait for the time that has been set"
delay: !input trigger_two_time
- alias: "For trigger three with a no time helper"
conditions:
- condition: trigger
id:
- 't3_to_state'
- 't3_no_state'
sequence:
- alias: "Wait for the time that has been set"
delay: !input trigger_three_time
- alias: "For trigger four with a no time helper"
conditions:
- condition: trigger
id:
- 't4_to_state'
- 't4_no_state'
sequence:
- alias: "Wait for the time that has been set"
delay: !input trigger_four_time
- alias: "Turn OFF the entities"
service: homeassistant.turn_off
target: !input entity_input
- alias: "Turn OFF entities after the timer helper is finished"
conditions:
- condition: trigger
id:
- 't5'
sequence:
- alias: "Turn OFF the entities"
service: homeassistant.turn_off
target: !input entity_input
- alias: "HA Restart"
conditions:
- condition: trigger
id: "t6"
sequence:
- alias: "Wait for the time that has been setin trigger one time"
delay: !input trigger_one_time
- alias: "Turn OFF the entities"
service: homeassistant.turn_off
target: !input entity_input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment