Template macros

This commit is contained in:
2023-04-05 20:08:52 -04:00
parent bb37a240d0
commit 2ec072af86
12 changed files with 111 additions and 84 deletions

View File

@ -0,0 +1,4 @@
{% macro get_brightness_pct(entity_id) %}
{% set current = state_attr(entity_id,'brightness') %}
{{ ((current / 255) * 100) | float | round(2) }}
{% endmacro %}

View File

@ -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 %}

View File

@ -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 %}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -1,35 +1,10 @@
>
{# Daily Briefing #}
{%- macro getReport() -%}
{% from 'speech.jinja' import greeting %}
<p>
{% 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() }}
</p>
{% if is_state('binary_sensor.morning','on') %}
<p>
Today is {{ states.sensor.today_is.state }}, {{ as_timestamp(now()) | timestamp_custom('%B %d %Y') }}.
</p>
{% else %}
<p>
It is {{ now().strftime("%I:%M %p") }}.
</p>
{% endif %}
<p>
{% if is_state('input_boolean.tornado_alarm','on') %}

View File

@ -1,13 +1,14 @@
>
{# Kallen Morning Briefing #}
{%- macro getReport() -%}
{% from 'time.jinja' import current_time_12hr, current_date_readout %}
<p>
Good morning, Collin.
{% if is_state('binary_sensor.morning','on') %}
<s>Today is {{ states.sensor.today_is.state }}, {{ as_timestamp(now()) | timestamp_custom('%B %d %Y') }}.</s>
<s>Today is {{ current_date_readout() }}.</s>
{% else %}
<s>It is {{ now().strftime("%I:%M %p") }}.</s>
<s>It is {{ current_time_12hr() }}.</s>
{% endif %}
{% if is_state('sensor.anniversary_kallen_s_birthday', '0') %}

View File

@ -1,36 +1,9 @@
>
{# Morning Briefing #}
{%- macro getReport() -%}
{% from 'speech.jinja' import greeting_nodate %}
<p>
{% 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 %}
</p>
<p>
{% if is_state('binary_sensor.morning','on') %}
<p>
Today is {{ states.sensor.today_is.state }}, {{ as_timestamp(now()) | timestamp_custom('%B %d %Y') }}.
</p>
{% else %}
<p>
It is {{ now().strftime("%I:%M %p") }}.
</p>
{% endif %}
{{ greeting_nodate() }}
</p>
<p>
{{ [

View File

@ -1,8 +1,11 @@
>
{# Nightly Briefing #}
{%- macro getReport() -%}
{% from 'speech.jinja' import greeting %}
<p>
{{ greeting() }}
</p>
<p>
"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. ",