From 779dd19eaa638f43a7e017bc2e3ca26e1bb24f3a Mon Sep 17 00:00:00 2001 From: Tony Stork Date: Sun, 16 Jul 2023 21:54:22 -0400 Subject: [PATCH] Expand time from calendar macro to allow modifying the time --- custom_templates/time.jinja | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/custom_templates/time.jinja b/custom_templates/time.jinja index a634038..6959aa0 100644 --- a/custom_templates/time.jinja +++ b/custom_templates/time.jinja @@ -38,11 +38,26 @@ {% endif %} {% endmacro %} -{% macro time_from_calendar(calendar,start_or_end,action) %} +{% macro time_from_calendar(calendar,start_or_end,action,operator,hours,minutes) %} +{% set base = as_timestamp(strptime(state_attr(calendar,start_or_end), '%Y-%m-%d %H:%M:%S')) | int %} +{% if minutes is defined %} + {% set mod = ((hours * 60) * 60) + (minutes * 60) | int %} +{% elif hours is defined %} + {% set mod = (hours* 60) * 60 | int %} +{% endif %} {% if action == 'set' %} -{{ as_timestamp(strptime(state_attr(calendar,start_or_end), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%H:%M") }} + {% set ts = "%H:%M" %} {% elif action == 'read' %} -{{ as_timestamp(strptime(state_attr(calendar,start_or_end), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%-I:%M %p") }} + {% set ts = "%-I:%M %p" %} +{% endif %} +{% if operator is defined %} + {% if operator == 'add' %} + {{ (base + mod) | timestamp_custom(ts) }} + {% elif operator == 'subtract' %} + {{ (base - mod) | timestamp_custom(ts) }} + {% endif %} +{% else %} + {{ base | timestamp_custom(ts) }} {% endif %} {% endmacro %}