Skip to content

Instantly share code, notes, and snippets.

@XtremeOwnageDotCom
Created January 8, 2025 01:27
Show Gist options
  • Save XtremeOwnageDotCom/854075396ed5ad532b7ab04f0d3e2dce to your computer and use it in GitHub Desktop.
Save XtremeOwnageDotCom/854075396ed5ad532b7ab04f0d3e2dce to your computer and use it in GitHub Desktop.
Scheduled Switch
# # # Main Entities
# # entity_list - list[] switch
# #
# # # Flags, Helpers, etc.
# # input_bool_enabled - Input_Boolean which determines if this automation is enabled.
# # input_bool_vacation - Input_Boolean which determines if we are on vacation.
# # input_timer - Timer helper, used to execute stopping logic after a period of time has passed.
# #
# # # Checkboxes
# # bool_start_at - Checkbox to determine if starting based on specific time is enabled.
# # bool_stop_at - Checkbox to determine if stopping based on specific time is enabled.
# # bool_stop_after - Checkbox to determine if stopping based on duration is enabled.
# # bool_vacation_start_at - Checkbox to determine if starting based on specific time is enabled during vacation.
# # bool_vacation_stop_at - Checkbox to determine if stopping based on specific time is enabled during vacation.
# # bool_vacation_stop_after - Checkbox to determine if stopping based on duration is enabled during vacation.
# #
# # # Times
# # time_start_at - The time which we should start at, when not on vacation.
# # time_vacation_start_at - The time to start at, when on vacation.
# # time_stop_at - The time to stop at, when not on vacation.
# # time_vacation_stop_at - The time to stop at, when on vacation.
# #
# #
# # # Durations
# # duration_stop_after - The duration to run when not on vacation.
# # duration_vacation_stop_after - The duration to run when on vacation.
## FOR Example Automation, See https://hass.kube.xtremeownage.com/config/automation/edit/1731802279540
blueprint:
name: Scheduled Switch
description: >
This blueprint automatically turns a switch on or off, based on time of day, and accounting for vacattion.
domain: automation
homeassistant:
min_version: "2024.6.0"
author: "XtremeOwnage (https://static.xtremeownage.com)"
input:
# Main Section
entity_list:
name: Entities
description: "Configure the list of entities to be turned on or off.."
input:
entity_list:
name: Entities
description: >
This is the list of entities which will be turned on or off by this automation.
selector:
entity:
filter:
domain: switch
multiple: true
main:
name: Features & Flags
description: "This section contains flags which can be specified."
input:
input_bool_enabled:
name: Enabled Entity
description: >
Specify an helper to control if this automation is active.
When populated, this automation will only run when this helper is in the "on" state.
Use this to set a master enable/disable toggle.
Note- When this is turned off- the entities will be turned off as well.
selector:
entity:
filter:
domain: input_boolean
default:
input_bool_vacation:
name: Vacation Mode
description: >
Select a entity which determines if "Vacation Mode" is enabled.
If this is not populated, "Vacation" settings will have no effect.
Vacation mode has its own seperate start/stop times.
selector:
entity:
filter:
domain: input_boolean
default:
input_timer:
name: Timer Helper
description: >
In order to enable stopping after N hours, you must create, and specify a "Timer" helper here.
Make sure to check "restore" when creating- this will allow the timer to persist after restarts.
Note- This is **REQUIRED** to stop after N hours. (Note, because of bugs... its required period)
selector:
entity:
filter:
domain: timer
default:
start:
name: Start
description: >
Settings for controlling the start actions. If "Start Time (Vacation)" is enabled
and the vacation helper is on, then it will take precedence over these settings.
input:
bool_start_at:
name: Enable Start Time
description: "Enable automatic start at a specific time."
default: false
selector:
boolean:
time_start_at:
name: Start Time
description: "Specify the start time if time-based start is enabled."
default: "06:00:00"
selector:
time:
# Stop based on specific Time.
stop:
name: Stop - Specific Time
description: >
Settings for controlling the stop actions. If "Stop Time (Vacation)" is enabled
and the vacation helper is on, then it will take precedence over these settings.
input:
bool_stop_at:
name: Enable Stop Time
description: "Enable automatic stop at a specific time."
default: false
selector:
boolean:
time_stop_at:
name: Stop Time
description: "Specify the stop time if time-based stop is enabled."
default: "22:00:00"
selector:
time:
# Stop based on duration
stop_duration:
name: Stop - After Duration
description: >
Settings for turning off entities after the specified duration has passed.
input:
bool_stop_after:
name: Enable Stop After
description: "Automatically stop the automation after a set number of hours."
default: false
selector:
boolean:
bool_allow_timer_start:
name: Allow Timer To Trigger Automation
description: >
If specified, this will enable triggering this automation when the timer is started manually.
Note- this automation will not set the duration when the timer is started manually!
default: false
selector:
boolean:
duration_stop_after:
name: Stop After
description: >
This determines how long the entities should remain active, if the above checkbox is checked.
selector:
duration:
enable_day: false
enable_millisecond: false
default:
hours: 12
minutes: 30
seconds: 15
# Vacation Start/Stop Section
vacation_start:
name: Start (Vacation)
description: "When should the entities be enabled when on vacation?"
input:
bool_vacation_start_at:
name: Start Time
description: "Enable automatic start at a specific time during vacation."
default: false
selector:
boolean:
time_vacation_start_at:
name: Start Time
description: "Specify the start time when vacation is enabled, and time-based start is enabled."
default: "06:00:00"
selector:
time:
# Stop based on specific Time.
vacation_stop:
name: Stop - Specific Time (Vacation)
description: >
Settings for controlling the stop actions during vacation. If "Stop Time (Vacation)" is enabled
and the vacation helper is on, then it will take precedence over these settings.
input:
bool_vacation_stop_at:
name: Enable Stop Time
description: "Enable automatic stop at a specific time."
default: false
selector:
boolean:
time_vacation_stop_at:
name: Stop Time
description: "Specify the stop time if time-based stop is enabled."
default: "22:00:00"
selector:
time:
vacation_stop_duration:
name: Stop - After Duration (Vacation)
description: >
Settings for turning off entities after the specified duration has passed.
input:
bool_vacation_stop_after:
name: Enable Stop After
description: "Automatically stop the automation after a set number of hours."
default: false
selector:
boolean:
duration_vacation_stop_after:
name: Stop After
description: >
This determines how long the entities should remain active, if the above checkbox is checked.
selector:
duration:
enable_day: false
enable_millisecond: false
default:
hours: 12
minutes: 30
seconds: 15
variables:
# Main Entities
entity_list: !input entity_list
# Flags, Helpers, etc.
input_bool_enabled: !input input_bool_enabled
input_bool_vacation: !input input_bool_vacation
input_timer: !input input_timer
# Checkboxes
bool_start_at: !input bool_start_at
bool_stop_at: !input bool_stop_at
bool_stop_after: !input bool_stop_after
bool_vacation_start_at: !input bool_vacation_start_at
bool_vacation_stop_at: !input bool_vacation_stop_at
bool_vacation_stop_after: !input bool_vacation_stop_after
bool_allow_timer_start: !input bool_allow_timer_start
# Timers
time_start_at: !input time_start_at
time_vacation_start_at: !input time_vacation_start_at
time_stop_at: !input time_stop_at
time_vacation_stop_at: !input time_vacation_stop_at
# Durations
duration_stop_after: !input duration_stop_after
duration_vacation_stop_after: !input duration_vacation_stop_after
# Check if input_bool_enabled is defined and return its state, or true if not defined
is_enabled: >
{% if input_bool_enabled is not none %}
{{ is_state(input_bool_enabled, 'on') }}
{% else %}
true
{% endif %}
# Check if vacation mode is on, ensuring input_bool_vacation is defined
is_vacation: >
{% if input_bool_vacation is not none %}
{{ is_state(input_bool_vacation, 'on') }}
{% else %}
false
{% endif %}
# Check if any entity in entity_list is on
is_any_entity_on: >
{% if entity_list | length > 0 %}
{{ entity_list | select('is_state', 'on') | list | length > 0 }}
{% else %}
false
{% endif %}
# Determines if starting the timer manually will trigger this workflow.
timer_start_trigger_enabled: >
{{ bool_allow_timer_start and input_timer is defined }}
# Determines if timer-based triggers are enabled.
timer_start_enabled: >
{{ (not is_vacation and bool_stop_after) or (is_vacation and bool_vacation_stop_after) and input_timer is not none }}
# Set timer duration
timer_duration: >
{% if timer_start_enabled %}
{% if is_vacation %}
{{ duration_vacation_stop_after }}
{% else %}
{{ duration_stop_after }}
{% endif %}
{% else %}
null
{% endif %}
trigger_variables:
# Flags, Helpers, etc.
input_bool_enabled: !input input_bool_enabled
input_bool_vacation: !input input_bool_vacation
input_timer: !input input_timer
# Checkboxes
bool_start_at: !input bool_start_at
bool_stop_at: !input bool_stop_at
bool_stop_after: !input bool_stop_after
bool_vacation_start_at: !input bool_vacation_start_at
bool_vacation_stop_at: !input bool_vacation_stop_at
bool_vacation_stop_after: !input bool_vacation_stop_after
bool_allow_timer_start: !input bool_allow_timer_start
# Times
time_start_at: !input time_start_at
time_vacation_start_at: !input time_vacation_start_at
time_stop_at: !input time_stop_at
time_vacation_stop_at: !input time_vacation_stop_at
# Determines if timer-based triggers are enabled.
timer_start_trigger_enabled: "{{ bool_allow_timer_start and input_timer }}"
stop_timer_enabled: "{{ ( bool_stop_after or bool_vacation_stop_after or bool_allow_timer_start ) and input_timer }}"
triggers:
- trigger: time
at: !input time_start_at
id: start_time
enabled: "{{ bool_start_at }}"
alias: Start Time (Non-Vacation)
- trigger: time
at: !input time_vacation_start_at
id: vacation_start_time
enabled: "{{ bool_vacation_start_at }}"
alias: Start Time (Vacation)
- trigger: time
at: !input time_stop_at
id: end_time
enabled: "{{ bool_stop_at }}"
alias: End Time (Non-Vacation)
- trigger: time
at: !input time_vacation_stop_at
id: vacation_end_time
enabled: "{{ bool_vacation_stop_at }}"
alias: End Time (Vacation)
- trigger: state
entity_id: !input input_timer
to: active
from: idle
id: timer_started
enabled: "{{ timer_start_trigger_enabled }}"
alias: Timer Started
- trigger: state
entity_id: !input input_timer
from: active
to: idle
id: timer_finished
enabled: "{{ stop_timer_enabled }}"
alias: Timer Finished
- trigger: template
value_template: >
{% if not input_bool_enabled %}
false
{% elif is_state(input_bool_enabled, 'off') %}
true
{% endif %}
id: disabled
alias: "Enabled Entity: Turned Off"
conditions:
- alias: Check Triggers
condition: or
conditions:
- alias: Non-Vacation Triggers
condition: and
conditions:
- condition: trigger
alias: "Trigger: start_time || end_time"
id:
- start_time
- end_time
- condition: template
alias: "NOT Input_Boolean.Vacation == On"
value_template: "{{ not is_vacation }}"
- condition: template
alias: "Input_Boolean.Enabled == On"
value_template: "{{ is_enabled }}"
- alias: Vacation Triggers
condition: and
conditions:
- condition: trigger
alias: "Trigger: vacation_start_time || vacation_end_time"
id:
- vacation_start_time
- vacation_end_time
- condition: template
alias: "Input_Boolean.Vacation == On"
value_template: "{{ is_vacation }}"
- condition: template
alias: "Input_Boolean.Enabled == On"
value_template: "{{ is_enabled }}"
- alias: Enabled Entity Turned Off
condition: and
conditions:
- condition: trigger
alias: "Trigger: disabled"
id: disabled
- condition: template
alias: "Entities: Any are on"
value_template: "{{ is_any_entity_on }}"
- alias: Timer Start Conditions
condition: and
conditions:
- condition: trigger
alias: "Trigger: timer_started"
id: timer_started
- condition: template
alias: "Input_Boolean.Enabled == On"
value_template: "{{ is_enabled }}"
- condition: template
alias: "timer_start_trigger_enabled == true"
value_template: "{{ timer_start_trigger_enabled }}"
- alias: Timer Finished
condition: and
conditions:
- condition: trigger
alias: "Trigger: timer_finished"
id: timer_finished
- condition: template
alias: "Entities: Any are on"
value_template: "{{ is_any_entity_on }}"
actions:
- choose:
- alias: "Start at Specific Time"
conditions:
- condition: trigger
id:
- start_time
- vacation_start_time
- timer_started
alias: "Triggered By: Turn On"
sequence:
- action: switch.turn_on
target:
entity_id: "{{ entity_list }}"
- conditions:
- alias: Turn switch off
condition: trigger
id:
- end_time
- vacation_end_time
- disabled
- timer_finished
sequence:
- action: switch.turn_off
target:
entity_id: "{{ entity_list }}"
- alias: "Start Timer, if Stop After is Enabled"
if:
- condition: trigger
id:
- start_time
- vacation_start_time
alias: "Triggered By: Turn On"
- condition: template
value_template: "{{ timer_start_enabled }}"
alias: "Timer Enabled Check"
then:
- action: timer.start
data:
duration: "{{ timer_duration }}"
target:
entity_id: !input input_timer
- alias: "Stop Timer when Disabled"
if:
- condition: trigger
id:
- disabled
alias: "Trigger: disabled"
- condition: template
value_template: "{{ input_timer is defined }}"
alias: "Timer Enabled Check"
then:
- action: timer.finish
target:
entity_id: !input input_timer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment