Trigger automations prior to the value of an Input datetime helper or Sensor with device class timestamp
with a user-defined offset.
The Automation:
- Trigger:
- You can choose either an Input datetime or a Sensor whose device class is
timestamp
to provide the initial time. - When using Input datetime entities the trigger will follow the same general rules as Time triggers based on Input datetimes.
- In the offset, specify the days, hours, and/or minutes before the initial time that you want your automation to trigger at. Due to the way time templates work, triggers will always fire at the top of the minute and offsets less than 2 minutes are not recommended.
- You can choose either an Input datetime or a Sensor whose device class is
- Condition and Action:
- Conditions and Actions are completely configurable by the user.
blueprint:
name: Entity-based "Time" trigger with Offset
description: |
Trigger a set of actions prior to a value from a datetime entity.
Select either an Input Datetime helper or a Sensor with device class timestamp.
The template-based trigger used only approximates a Time trigger and is limited in terms of accuracy. The trigger will always fire at the top of the minute. Time offsets less than 2 minutes are not recommended since they may not reliably trigger prior to the target value.
domain: automation
input:
sensor:
name: Sensor
description: ""
default: ""
selector:
entity:
domain:
- sensor
device_class: timestamp
dt_helper:
name: Input Datetime Helper
description: ""
default: ""
selector:
entity:
domain:
- input_datetime
offset:
name: Time offset
description: The number of hours to use for the timedelta
selector:
duration:
enable_day: true
conditional:
name: Conditions
description: Conditions
selector:
condition:
default: []
action:
name: Action
description: Action to perform
selector:
action:
default: []
mode: single
trigger_variables:
var_dt_helper: !input dt_helper
var_sensor: !input sensor
var_offset: !input offset
trigger:
- platform: template
value_template: |-
{%- if var_dt_helper is defined and var_dt_helper not in ['', none, 'None'] %}
{%- set (hd, ht) =(state_attr(var_dt_helper, 'has_date'), state_attr(var_dt_helper, 'has_time')) %}
{%- if hd and not ht %}
{%- set dt_helper = today_at() %}
{%- elif hd and ht %}
{%- set dt_helper = states(var_dt_helper)|as_datetime|as_local %}
{%- else %}
{%- set dt_helper = today_at(states(var_dt_helper)) %}
{% endif %}
{%- endif %}
{% set dt = states(var_sensor)|as_datetime|as_local
if var_sensor is defined and var_sensor not in ['', none, 'None']
else dt_helper %}
{% set delta = timedelta(days = var_offset.days|default(0), hours = var_offset.hours|default(0),
minutes = var_offset.minutes|default(0), seconds = var_offset.seconds|default(0)) %}
{{ (now() + delta > dt) }}
condition: !input conditional
action: !input action