From 5fe5fc6916356c7b46f8bcccbd226763e9d1a126 Mon Sep 17 00:00:00 2001 From: Tony Stork Date: Sun, 4 Aug 2024 15:02:58 -0400 Subject: [PATCH] Add new macro for setting time AND date from calendars --- custom_templates/time.jinja | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/custom_templates/time.jinja b/custom_templates/time.jinja index 0dd2fb6..ab3ba40 100644 --- a/custom_templates/time.jinja +++ b/custom_templates/time.jinja @@ -68,6 +68,31 @@ {% endif %} {% endmacro %} +{% macro datetime_from_calendar(calendar,start_or_end,action,operator,days,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 = ((((days * 24) * 60) * 60) + (hours* 60) * 60) + (minutes * 60) | int %} +{% elif hours is defined %} + {% set mod = (((days * 24) * 60) * 60) + (hours* 60) * 60 | int %} +{% elif days is defined %} + {% set mod = ((days * 24) * 60) * 60 | int %} +{% endif %} +{% if action == 'set' %} + {% set ts = "%Y-%m-%d %H:%M:%S" %} +{% elif action == 'read' %} + {% set ts = "%Y-%m-%d %-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 %} + {% macro set_datetime(hours,minutes,seconds) %} {% if seconds is defined %} {{ (as_timestamp(now()) + (((hours * 60) * 60) + (minutes * 60)) + seconds) | int | timestamp_custom('%Y-%m-%d %H:%M:%S') }}