From 4b216396908c3b1d21820e40559627bf5c2c198a Mon Sep 17 00:00:00 2001 From: Tony Stork Date: Mon, 6 Nov 2023 12:51:54 -0500 Subject: [PATCH] Improve current_time macro --- custom_templates/time.jinja | 27 ++++++++++--------- packages/emmalynn.yaml | 22 +++++++-------- packages/kallen.yaml | 8 +++--- packages/tina.yaml | 8 +++--- templates/speech/kallen_morning_briefing.yaml | 2 +- 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/custom_templates/time.jinja b/custom_templates/time.jinja index c16b8af..0dd2fb6 100644 --- a/custom_templates/time.jinja +++ b/custom_templates/time.jinja @@ -4,19 +4,22 @@ {{ (states(input_number)) | int * 60 }} {% endmacro %} -{% macro current_time(hr,date) %} -{% if hr == 12 %} -{{ now().strftime('%-I:%M %p') }} -{% elif hr == 24 %} - {% if date is defined %} - {% if date == 'withdate' %} - {{ now().strftime('%Y-%m-%d %H:%M:%S') }} - {% elif date == 'nodate' %} - {{ now().strftime('%H:%M') }} - {% endif %} - {% else %} - {{ now().strftime('%H:%M') }} +{% macro current_time(type,hr) %} +{% set hr = hr|default(24) %} +{% if type == 'datetime' %} + {% if hr == 12 %} + {{ now().strftime('%Y-%m-%d %-I:%M %p') }} + {% elif hr == 24 %} + {{ now().strftime('%Y-%m-%d %H:%M:%S') }} {% endif %} +{% elif type == 'time' %} + {% if hr == 12 %} + {{ now().strftime('%-I:%M %p') }} + {% elif hr == 24 %} + {{ now().strftime('%H:%M:%S') }} + {% endif %} +{% elif type == 'date' %} + {{ now().strftime('%Y-%m-%d') }} {% endif %} {% endmacro %} diff --git a/packages/emmalynn.yaml b/packages/emmalynn.yaml index 5520633..4c85349 100644 --- a/packages/emmalynn.yaml +++ b/packages/emmalynn.yaml @@ -140,7 +140,7 @@ automation: data: datetime: > {% from 'time.jinja' import current_time %} - {{ current_time(24,'withdate') }} + {{ current_time('datetime',24) }} else: - service: input_datetime.set_datetime target: @@ -148,7 +148,7 @@ automation: data: time: > {% from 'time.jinja' import current_time %} - {{ current_time(24) }} + {{ current_time('time',24) }} - delay: seconds: 1 - service: script.text_notify @@ -180,7 +180,7 @@ automation: data: datetime: > {% from 'time.jinja' import current_time %} - {{ current_time(24,'withdate') }} + {{ current_time('datetime',24) }} - delay: milliseconds: 500 - if: @@ -371,7 +371,7 @@ automation: data: datetime: > {% from 'time.jinja' import current_time %} - {{ current_time(24,'withdate') }} + {{ current_time('datetime',24) }} - conditions: - condition: or conditions: @@ -388,7 +388,7 @@ automation: data: time: > {% from 'time.jinja' import current_time %} - {{ current_time(24) }} + {{ current_time('time',24) }} - conditions: - condition: trigger id: naptime-timer-finished @@ -412,7 +412,7 @@ automation: data: datetime: > {% from 'time.jinja' import current_time %} - {{ current_time(24,'withdate') }} + {{ current_time('datetime',24) }} - conditions: - condition: trigger id: bedtime-timer-finished @@ -436,7 +436,7 @@ automation: data: datetime: > {% from 'time.jinja' import current_time %} - {{ current_time(24) }} + {{ current_time('time',24) }} - id: e59f638a-519f-4619-bf7d-b13251b3a374 alias: Emma Meds Handler @@ -670,7 +670,7 @@ script: data: datetime: > {% from 'time.jinja' import current_time %} - {{ current_time(24,'withdate') }} + {{ current_time('datetime',24) }} - service: input_datetime.set_datetime target: entity_id: input_datetime.emma_ibuprofen_notify @@ -803,7 +803,7 @@ script: data: datetime: > {% from 'time.jinja' import current_time %} - {{ current_time(24,'withdate') }} + {{ current_time('datetime',24) }} - service: input_datetime.set_datetime target: entity_id: input_datetime.emma_tylenol_notify @@ -928,7 +928,7 @@ script: data: datetime: > {% from 'time.jinja' import current_time %} - {{ current_time(24,'withdate') }} + {{ current_time('datetime',24) }} - service: input_datetime.set_datetime target: entity_id: input_datetime.emma_cough_meds_notify @@ -1124,7 +1124,7 @@ script: data: time: > {% from 'time.jinja' import current_time %} - {{ current_time(24) }} + {{ current_time('time',24) }} - if: - condition: state entity_id: binary_sensor.early_night_mode diff --git a/packages/kallen.yaml b/packages/kallen.yaml index 7f03908..e9786e1 100644 --- a/packages/kallen.yaml +++ b/packages/kallen.yaml @@ -149,8 +149,8 @@ automation: entity_id: input_datetime.kallen_morning_meds_taken data: datetime: > - {% from 'time.jinja' import set_datetime %} - {{ set_datetime(0) }} + {% from 'time.jinja' import current_time %} + {{ current_time('datetime',24) }} - conditions: - condition: trigger id: boolean-night @@ -160,8 +160,8 @@ automation: entity_id: input_datetime.kallen_night_meds_taken data: datetime: > - {% from 'time.jinja' import set_datetime %} - {{ set_datetime(0) }} + {% from 'time.jinja' import current_time %} + {{ current_time('datetime',24) }} - id: 1d552b1f-c0ed-4d80-b8ba-3c085d2c3d3a alias: "Kallen Late Bedtime" diff --git a/packages/tina.yaml b/packages/tina.yaml index d47ceac..10ca00a 100644 --- a/packages/tina.yaml +++ b/packages/tina.yaml @@ -146,8 +146,8 @@ automation: entity_id: input_datetime.tina_morning_meds_taken data: datetime: > - {% from 'time.jinja' import set_datetime %} - {{ set_datetime(0) }} + {% from 'time.jinja' import current_time %} + {{ current_time('datetime',24) }} - conditions: - condition: and conditions: @@ -172,8 +172,8 @@ automation: entity_id: input_datetime.tina_night_meds_taken data: datetime: > - {% from 'time.jinja' import set_datetime %} - {{ set_datetime(0) }} + {% from 'time.jinja' import current_time %} + {{ current_time('datetime',24) }} script: tina_work_today: diff --git a/templates/speech/kallen_morning_briefing.yaml b/templates/speech/kallen_morning_briefing.yaml index 4c315f0..307ee57 100644 --- a/templates/speech/kallen_morning_briefing.yaml +++ b/templates/speech/kallen_morning_briefing.yaml @@ -17,7 +17,7 @@ {% if is_state('binary_sensor.morning','on') %} Today is {{ current_date_readout() }}. {% else %} - It is {{ current_time(12) }}. + It is {{ current_time('time',12) }}. {% endif %} {% if is_state('sensor.anniversary_kallen_s_birthday', '0') %}