From 2ec072af860e2b8057dd77aa770748bfcf942f3c Mon Sep 17 00:00:00 2001
From: Tony Stork
Date: Wed, 5 Apr 2023 20:08:52 -0400
Subject: [PATCH] Template macros
---
custom_templates/lighting.jinja | 4 ++
custom_templates/speech.jinja | 44 +++++++++++++++++++
custom_templates/time.jinja | 19 ++++++++
packages/lighting_and_scenes.yaml | 20 ++++++---
packages/scheduling.yaml | 3 +-
packages/school.yaml | 11 ++---
packages/tina.yaml | 16 +++----
packages/tony.yaml | 8 ++--
templates/speech/daily_briefing.yaml | 29 +-----------
templates/speech/kallen_morning_briefing.yaml | 5 ++-
templates/speech/morning_briefing.yaml | 31 +------------
templates/speech/nightly_briefing.yaml | 5 ++-
12 files changed, 111 insertions(+), 84 deletions(-)
create mode 100644 custom_templates/lighting.jinja
create mode 100644 custom_templates/speech.jinja
create mode 100644 custom_templates/time.jinja
diff --git a/custom_templates/lighting.jinja b/custom_templates/lighting.jinja
new file mode 100644
index 0000000..a8d569a
--- /dev/null
+++ b/custom_templates/lighting.jinja
@@ -0,0 +1,4 @@
+{% macro get_brightness_pct(entity_id) %}
+{% set current = state_attr(entity_id,'brightness') %}
+{{ ((current / 255) * 100) | float | round(2) }}
+{% endmacro %}
diff --git a/custom_templates/speech.jinja b/custom_templates/speech.jinja
new file mode 100644
index 0000000..18b7ddd
--- /dev/null
+++ b/custom_templates/speech.jinja
@@ -0,0 +1,44 @@
+{% macro greeting() %}
+{% if now().strftime('%H')|int < 12 %}
+ {{ [
+ "Good morning. ",
+ "Top of the morning to you laddy. ",
+ ] | random }}
+{% elif now().strftime('%H')|int >= 12 and now().strftime('%H')|int < 17 %}
+ {{ [
+ "Greetings earthling. ",
+ "Good afternoon. ",
+ "How do you do, fellow kids? ",
+ ] | random }}
+{% else %}
+ {{ [
+ "Good evening. ",
+ "The grandest of evenings to you. "
+ ] | random }}
+{% endif %}
+{% if is_state('binary_sensor.morning','on') %}
+ Today is {{ states('sensor.today_is') }}, {{ as_timestamp(now()) | timestamp_custom('%B %d %Y') }}.
+{% endif %}
+It is {{ now().strftime("%I:%M %p") }}.
+{% endmacro %}
+
+{% macro greeting_nodate() %}
+{% if now().strftime('%H')|int < 12 %}
+ {{ [
+ "Good morning. ",
+ "Top of the morning to you laddy. ",
+ ] | random }}
+{% elif now().strftime('%H')|int >= 12 and now().strftime('%H')|int < 17 %}
+ {{ [
+ "Greetings earthling. ",
+ "Good afternoon. ",
+ "How do you do, fellow kids? ",
+ ] | random }}
+{% else %}
+ {{ [
+ "Good evening. ",
+ "The grandest of evenings to you. "
+ ] | random }}
+{% endif %}
+It is {{ now().strftime("%I:%M %p") }}.
+{% endmacro %}
\ No newline at end of file
diff --git a/custom_templates/time.jinja b/custom_templates/time.jinja
new file mode 100644
index 0000000..f64e853
--- /dev/null
+++ b/custom_templates/time.jinja
@@ -0,0 +1,19 @@
+{% macro timer_duration(input_number) %}
+{{ (states(input_number)) | int * 60 }}
+{% endmacro %}
+
+{% macro current_time_12hr() %}
+{{ now().strftime("%I:%M %p") }}
+{% endmacro %}
+
+{% macro current_date_readout() %}
+{{ states('sensor.today_is') }}, {{ as_timestamp(now()) | timestamp_custom('%B %d %Y') }}
+{% endmacro %}
+
+{% macro calendar_event_today(calendar) %}
+{{ as_timestamp(strptime(state_attr(calendar,'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%Y-%m-%d") == now().strftime("%Y-%m-%d") }}
+{% endmacro %}
+
+{% macro set_time_from_calendar(calendar,start_or_end) %}
+{{ as_timestamp(strptime(state_attr(calendar,start_or_end), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%H:%M") }}
+{% endmacro %}
\ No newline at end of file
diff --git a/packages/lighting_and_scenes.yaml b/packages/lighting_and_scenes.yaml
index f3da63f..e8b08f4 100644
--- a/packages/lighting_and_scenes.yaml
+++ b/packages/lighting_and_scenes.yaml
@@ -271,8 +271,8 @@ sensor:
friendly_name: "Basement Studio Lights - Brightness Actual"
unique_id: dee4dc84-a6a0-4150-903e-5b8bd436d962
value_template: >
- {% set current = state_attr('light.basement_studio_lights','brightness') %}
- {{ ((current / 255) * 100) | float | round(2) }}
+ {% from 'lighting.jinja' import get_brightness_pct %}
+ {{ get_brightness_pct('light.basement_studio_lights') }}
availability_template: "{{ is_state('light.basement_studio_lights','on') }}"
unit_of_measurement: '%'
icon_template: mdi:brightness-percent
@@ -321,8 +321,8 @@ sensor:
friendly_name: "Living Room Lights - Brightness Actual"
unique_id: e557022a-184f-4111-bb6a-6c0869cce42c
value_template: >
- {% set current = state_attr('light.living_room_lights','brightness') %}
- {{ ((current / 255) * 100) | float | round(2) }}
+ {% from 'lighting.jinja' import get_brightness_pct %}
+ {{ get_brightness_pct('light.living_room_lights') }}
availability_template: "{{ is_state('light.living_room_lights','on') }}"
unit_of_measurement: '%'
icon_template: mdi:brightness-percent
@@ -585,7 +585,9 @@ automation:
target:
entity_id: timer.living_room_motion_timer
data:
- duration: "{{ (states('input_number.living_room_lights_off_delay') | int ) * 60 }}"
+ duration: >
+ {% from 'time.jinja' import timer_duration %}
+ {{ timer_duration('input_number.living_room_lights_off_delay') }}
- conditions:
- condition: trigger
id: timer-finished
@@ -773,7 +775,9 @@ automation:
target:
entity_id: timer.basement_studio_door_timer
data:
- duration: "{{ (states('input_number.basement_studio_lights_off_delay') | int ) * 60 }}"
+ duration: >
+ {% from 'time.jinja' import timer_duration %}
+ {{ timer_duration('input_number.basement_studio_lights_off_delay') }}
- conditions:
- condition: trigger
id: basement-occupied
@@ -799,7 +803,9 @@ automation:
target:
entity_id: timer.basement_studio_door_timer
data:
- duration: "{{ (states('input_number.basement_studio_lights_off_delay') | int ) * 60 }}"
+ duration: >
+ {% from 'time.jinja' import timer_duration %}
+ {{ timer_duration('input_number.basement_studio_lights_off_delay') }}
- conditions:
- condition: trigger
id: timer-finished
diff --git a/packages/scheduling.yaml b/packages/scheduling.yaml
index 43bcc87..2b35db4 100644
--- a/packages/scheduling.yaml
+++ b/packages/scheduling.yaml
@@ -200,10 +200,11 @@ script:
entity_id: input_datetime.kallen_school_day_start
data:
time: >
+ {% from 'time.jinja' import set_time_from_calendar %}
{% if is_state('input_boolean.two_hour_delay','on') %}
11:00
{% else %}
- {{ as_timestamp(strptime(state_attr('calendar.kallen_school_days','start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%H:%M") }}
+ {{ set_time_from_calendar('calendar.kallen_school_days','start_time') }}
{% endif %}
- delay:
seconds: 1
diff --git a/packages/school.yaml b/packages/school.yaml
index 3148565..34808da 100644
--- a/packages/school.yaml
+++ b/packages/school.yaml
@@ -281,9 +281,8 @@ automation:
- if:
- condition: template
value_template: >
- {%- if as_timestamp(strptime(state_attr('calendar.kallen_school_days', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%Y-%m-%d") == now().strftime("%Y-%m-%d") %}
- true
- {%- endif -%}
+ {% from 'time.jinja' import calendar_event_today %}
+ {{ calendar_event_today('calendar.kallen_school_days') }}
then:
- service: script.kallen_school_today
- service: input_boolean.turn_on
@@ -418,13 +417,15 @@ script:
entity_id: input_datetime.kallen_school_day_end
data:
time: >
- {{ as_timestamp(strptime(state_attr('calendar.kallen_school_days','end_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%H:%M") }}
+ {% from 'time.jinja' import set_time_from_calendar %}
+ {{ set_time_from_calendar('calendar.kallen_school_days','end_time') }}
- service: input_datetime.set_datetime
target:
entity_id: input_datetime.kallen_school_day_start
data:
time: >
- {{ as_timestamp(strptime(state_attr('calendar.kallen_school_days','start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%H:%M") }}
+ {% from 'time.jinja' import set_time_from_calendar %}
+ {{ set_time_from_calendar('calendar.kallen_school_days','start_time') }}
- service: input_boolean.turn_on
entity_id:
- input_boolean.kallen_school_today
diff --git a/packages/tina.yaml b/packages/tina.yaml
index ccbb2a0..6efe8e9 100644
--- a/packages/tina.yaml
+++ b/packages/tina.yaml
@@ -32,9 +32,8 @@ automation:
- if:
- condition: template
value_template: >
- {%- if as_timestamp(strptime(state_attr('calendar.family_tinawork','start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%Y-%m-%d") == now().strftime("%Y-%m-%d") %}
- true
- {%- endif -%}
+ {% from 'time.jinja' import calendar_event_today %}
+ {{ calendar_event_today('calendar.family_tinawork') }}
then:
- service: script.tina_work_today
- service: input_boolean.turn_on
@@ -59,13 +58,15 @@ script:
entity_id: input_datetime.tina_workday_start
data:
time: >
- {{ as_timestamp(strptime(state_attr('calendar.family_tinawork','start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%H:%M") }}
+ {% from 'time.jinja' import set_time_from_calendar %}
+ {{ set_time_from_calendar('calendar.family_tinawork','start_time') }}
- service: input_datetime.set_datetime
target:
entity_id: input_datetime.tina_workday_end
data:
time: >
- {{ as_timestamp(strptime(state_attr('calendar.family_tinawork','end_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%H:%M") }}
+ {% from 'time.jinja' import set_time_from_calendar %}
+ {{ set_time_from_calendar('calendar.family_tinawork','end_time') }}
- service: input_boolean.turn_on
target:
entity_id: input_boolean.work_today
@@ -84,9 +85,8 @@ script:
- if:
- condition: template
value_template: >
- {%- if as_timestamp(strptime(state_attr('calendar.family_tinawork','start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%Y-%m-%d") == now().strftime("%Y-%m-%d") %}
- true
- {%- endif -%}
+ {% from 'time.jinja' import calendar_event_today %}
+ {{ calendar_event_today('calendar.family_tinawork') }}
then:
- service: script.tina_work_today
else:
diff --git a/packages/tony.yaml b/packages/tony.yaml
index 5bf6668..37d2114 100644
--- a/packages/tony.yaml
+++ b/packages/tony.yaml
@@ -22,9 +22,8 @@ automation:
condition:
- condition: template
value_template: >
- {%- if as_timestamp(strptime(state_attr('calendar.tony_s_twitch_schedule','start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%Y-%m-%d") == now().strftime("%Y-%m-%d") %}
- true
- {%- endif -%}
+ {% from 'time.jinja' import calendar_event_today %}
+ {{ calendar_event_today('calendar.tony_s_twitch_schedule') }}
action:
- service: script.tony_stream_today
@@ -67,7 +66,8 @@ script:
entity_id: input_datetime.tony_streaming_start_time
data:
time: >
- {{ as_timestamp(strptime(state_attr('calendar.tony_s_twitch_schedule','start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%H:%M") }}
+ {% from 'time.jinja' import set_time_from_calendar %}
+ {{ set_time_from_calendar('calendar.tony_s_twitch_schedule','start_time') }}
- service: input_boolean.turn_on
entity_id: input_boolean.tony_streaming_today
diff --git a/templates/speech/daily_briefing.yaml b/templates/speech/daily_briefing.yaml
index b832b51..c5b4bda 100644
--- a/templates/speech/daily_briefing.yaml
+++ b/templates/speech/daily_briefing.yaml
@@ -1,35 +1,10 @@
>
{# Daily Briefing #}
{%- macro getReport() -%}
+ {% from 'speech.jinja' import greeting %}
- {% if now().strftime('%H')|int < 12 %}
- {{ [
- "Good morning. ",
- "Top of the morning to you laddy. ",
- ] | random }}
- {% elif now().strftime('%H')|int >= 12 and now().strftime('%H')|int < 17 %}
- {{ [
- "Greetings earthling. ",
- "Good afternoon. ",
- "How do you do, fellow kids? ",
- ] | random }}
- {% else %}
- {{ [
- "Good evening. ",
- "The grandest of evenings to you. "
- ] | random }}
- {% endif %}
+ {{ greeting() }}
-
- {% if is_state('binary_sensor.morning','on') %}
-
- Today is {{ states.sensor.today_is.state }}, {{ as_timestamp(now()) | timestamp_custom('%B %d %Y') }}.
-
- {% else %}
-
- It is {{ now().strftime("%I:%M %p") }}.
-
- {% endif %}
{% if is_state('input_boolean.tornado_alarm','on') %}
diff --git a/templates/speech/kallen_morning_briefing.yaml b/templates/speech/kallen_morning_briefing.yaml
index 7d941d4..ef24ca9 100644
--- a/templates/speech/kallen_morning_briefing.yaml
+++ b/templates/speech/kallen_morning_briefing.yaml
@@ -1,13 +1,14 @@
>
{# Kallen Morning Briefing #}
{%- macro getReport() -%}
+ {% from 'time.jinja' import current_time_12hr, current_date_readout %}
Good morning, Collin.
{% if is_state('binary_sensor.morning','on') %}
- Today is {{ states.sensor.today_is.state }}, {{ as_timestamp(now()) | timestamp_custom('%B %d %Y') }}.
+ Today is {{ current_date_readout() }}.
{% else %}
- It is {{ now().strftime("%I:%M %p") }}.
+ It is {{ current_time_12hr() }}.
{% endif %}
{% if is_state('sensor.anniversary_kallen_s_birthday', '0') %}
diff --git a/templates/speech/morning_briefing.yaml b/templates/speech/morning_briefing.yaml
index 4cadd94..967fce6 100644
--- a/templates/speech/morning_briefing.yaml
+++ b/templates/speech/morning_briefing.yaml
@@ -1,36 +1,9 @@
>
{# Morning Briefing #}
{%- macro getReport() -%}
+ {% from 'speech.jinja' import greeting_nodate %}
- {% if now().strftime('%H')|int < 12 and now().strftime('%H')|int > 6 %}
- {{ [
- "Good morning. ",
- "Top of the morning to you laddy. ",
- ] | random }}
- {% elif now().strftime('%H')|int >= 12 and now().strftime('%H')|int < 17 %}
- {{ [
- "Greetings earthling. ",
- "Good afternoon. ",
- "How do you do, fellow kids? ",
- ] | random }}
- {% else %}
- {{ [
- "Good evening. ",
- "The grandest of evenings to you. "
- ] | random }}
- {% endif %}
-
-
-
- {% if is_state('binary_sensor.morning','on') %}
-
- Today is {{ states.sensor.today_is.state }}, {{ as_timestamp(now()) | timestamp_custom('%B %d %Y') }}.
-
- {% else %}
-
- It is {{ now().strftime("%I:%M %p") }}.
-
- {% endif %}
+ {{ greeting_nodate() }}
{{ [
diff --git a/templates/speech/nightly_briefing.yaml b/templates/speech/nightly_briefing.yaml
index a13cba6..738dad7 100644
--- a/templates/speech/nightly_briefing.yaml
+++ b/templates/speech/nightly_briefing.yaml
@@ -1,8 +1,11 @@
>
{# Nightly Briefing #}
{%- macro getReport() -%}
+ {% from 'speech.jinja' import greeting %}
+
+ {{ greeting() }}
+
- "Good evening. It is {{ now().strftime("%I:%M %p") }}. "
{{ [
"Before the day comes to a close, here are my thoughts. Just kidding, I am a computer, I do not think. ",
"The day grows short, let us gather around the campfire for an epic tale of things to come. ",