I thought this was pretty cool so I thought I’d share. Here is a timer that you can set to perform an automation from the Lovelace UI. It could be built out to do just about anything, but as it stands it can turn_off, turn_on, or toggle something after a custom delay. I have tested it with switches, lights, and automations.
Here is the configuration information. You’ll need to add lines to configuration.yaml, automation.yaml, scripts.yaml, customize.yaml, add a python script, and update your lovelace config. To get the custom text in the markdown card in lovelace, you’ll need to install lovelace-markdown-mod.
########### configuration.yaml ###########
input_text:
# for ui input
timer_string:
name: "Time (hr:min:sec)"
pattern: '\d\d:[0,6]\d:[0,6]\d'
initial: '00:00:00'
# for storing action
timer_0_task:
name: "Timer_0 Task"
initial: "None"
timer_1_task:
name: "Timer_1 Task"
initial: "None"
timer_2_task:
name: "Timer_2 Task"
initial: "None"
timer_3_task:
name: "Timer_3 Task"
initial: "None"
timer_4_task:
name: "Timer_4 Task"
initial: "None"
timer:
# timers
timer_0:
timer_1:
timer_2:
timer_3:
timer_4:
input_select:
# list of timers for ui
timer_list:
name: Timers
options:
- timer_0
- timer_1
- timer_2
- timer_3
- timer_4
# action options for ui
timer_to_state:
name: "Timer: To State"
options:
- 'turn_on'
- 'turn_off'
- 'toggle'
# list of entities for ui - list populated in automation at boot
timer_entity_id:
name: "Timer: Entity ID"
options:
- "run 'python_script.populate_timer_entity_id' to populate"
########### automation.yaml ###########
# on boot, compile list of items to have as options for ui
- alias: '[HA] Timer - Load entity ids on boot'
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: python_script.populate_timer_entity_id
initial_state: 'on'
# timer finished - perform recorded action from ui
- alias: '[HA] Timer X'
trigger:
- platform: event
event_type: timer.finished
event_data:
entity_id: timer.timer_0
- platform: event
event_type: timer.finished
event_data:
entity_id: timer.timer_1
- platform: event
event_type: timer.finished
event_data:
entity_id: timer.timer_2
- platform: event
event_type: timer.finished
event_data:
entity_id: timer.timer_3
- platform: event
event_type: timer.finished
event_data:
entity_id: timer.timer_4
condition: []
action:
- service: script.timer_x_execute
data_template:
timer: '{{trigger.event.data.entity_id}}'
########### script.yaml ###########
timer_x_init:
sequence:
# record action to be done when timer finished
- service: script.timer_x_update_task_message
# start timer
- service: timer.start
data_template:
entity_id: >-
{{-"timer."-}}{{- states("input_select.timer_list") -}}
duration: >-
{{(states('input_text.timer_string'))}}
timer_x_execute:
sequence:
# execute recorded action
- service_template: >-
{%- if not timer -%}
{%- set timer = 'timer.timer_1' -%}
{%- endif -%}
{%- set domain_name = timer.split('.') -%}
{%- set read_action = 'input_text.'+domain_name[1] + '_task' -%}
{%- set action = states(read_action).split(' ') -%}
{% set domain_name = action[1].split('.') %}
{{- domain_name[0] -}} {{-'.'-}} {{- action[0] -}}
data_template:
entity_id: >-
{%- if not timer -%}
{%- set timer = 'timer.timer_0' -%}
{%- endif -%}
{%- set domain_name = timer.split('.') -%}
{%- set read_action = 'input_text.' + domain_name[1] + '_task' -%}
{%- set ent_id = states(read_action).split(' ') -%}
{{-ent_id[1]-}}
# clear stored action
- service: input_text.set_value
data_template:
entity_id: >-
{%- if not timer -%}
{%- set timer = 'timer.timer_0' -%}
{%- endif -%}
{%- set domain_name = timer.split('.') -%}
{{"input_text."}}{{ domain_name[1] }}{{"_task"}}
value: 'unknown'
timer_x_cancel:
sequence:
# cancel timer
- service: timer.cancel
data_template:
entity_id: >-
{{-"timer."-}}{{- states("input_select.timer_list") -}}
# clear stored action
- service: input_text.set_value
data_template:
entity_id: >-
{%- if not timer -%}
{%- set timer = 'timer.timer_0' -%}
{%- endif -%}
{%- set domain_name = timer.split('.') -%}
{{"input_text."}}{{ domain_name[1] }}{{"_task"}}
value: 'unknown'
timer_x_update_task_message:
sequence:
# read action to perform at end of timer
- service: input_text.set_value
data_template:
entity_id: '{{"input_text."}}{{ states("input_select.timer_list") }}{{"_task"}}'
value: '{{states("input_select.timer_to_state")}}{{" "}}{{ states("input_select.timer_entity_id") }}'
########### python_scripts/populate_timer_entity_id.py ###########
# pull all entities in the group group.lights
group_entities = hass.states.get('group.lights').attributes['entity_id']
timer_entities = []
for e in group_entities:
timer_entities.append(e)
timer_entities.append('climate.upstairs')
timer_entities.append('script.party_time_is_over_go_home')
service_data = {'entity_id': 'input_select.timer_entity_id',
'options': timer_entities}
hass.services.call('input_select', 'set_options', service_data)
########### lovelace ###########
- badges: []
cards:
- cards:
- entities:
- entity: input_select.timer_list
- entity: input_text.timer_string
- entity: input_select.timer_entity_id
- entity: input_select.timer_to_state
- entity: script.timer_x_init
show_header_toggle: false
title: Timer - Change state in x time
type: entities
- card:
entities:
- entity: timer.timer_0
type: entities
conditions:
- entity: timer.timer_0
state_not: idle
type: conditional
- card:
entities:
- entity: timer.timer_1
type: entities
conditions:
- entity: timer.timer_1
state_not: idle
type: conditional
- card:
entities:
- entity: timer.timer_2
type: entities
conditions:
- entity: timer.timer_2
state_not: idle
type: conditional
- card:
entities:
- entity: timer.timer_3
type: entities
conditions:
- entity: timer.timer_3
state_not: idle
type: conditional
- card:
entities:
- entity: timer.timer_4
type: entities
conditions:
- entity: timer.timer_4
state_not: idle
type: conditional
type: vertical-stack
- content: |-
### Timer 0
state: [[ timer.timer_0 ]]
total duration: [[ timer.timer_0.attributes.duration ]]
task: [[ input_text.timer_0_task ]]
### Timer 1
state: [[ timer.timer_1 ]]
total duration: [[ timer.timer_1.attributes.duration ]]
task: [[ input_text.timer_1_task ]]
### Timer 2
state: [[ timer.timer_2 ]]
total duration: [[ timer.timer_2.attributes.duration ]]
task: [[ input_text.timer_2_task ]]
### Timer 3
state: [[ timer.timer_3 ]]
total duration: [[ timer.timer_3.attributes.duration ]]
task: [[ input_text.timer_3_task ]]
### Timer 4
state: [[ timer.timer_4 ]]
total duration: [[ timer.timer_4.attributes.duration ]]
task: [[ input_text.timer_4_task ]]
title: Timer Status
type: markdown
icon: 'mdi:timer'
title: Timer
########### customize.yaml ###########
script.timer_x_init:
friendly_name: Run Timer
icon: mdi:timer
script.timer_x_cancel:
friendly_name: Cancel Timer
icon: mdi:cancel
Let me know if you need any help implementing.