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" friendly_name: "Basement Studio Lights - Brightness Actual"
unique_id: dee4dc84-a6a0-4150-903e-5b8bd436d962 unique_id: dee4dc84-a6a0-4150-903e-5b8bd436d962
value_template: > value_template: >
{% set current = state_attr('light.basement_studio_lights','brightness') %} {% from 'lighting.jinja' import get_brightness_pct %}
{{ ((current / 255) * 100) | float | round(2) }} {{ get_brightness_pct('light.basement_studio_lights') }}
availability_template: "{{ is_state('light.basement_studio_lights','on') }}" availability_template: "{{ is_state('light.basement_studio_lights','on') }}"
unit_of_measurement: '%' unit_of_measurement: '%'
icon_template: mdi:brightness-percent icon_template: mdi:brightness-percent
@ -321,8 +321,8 @@ sensor:
friendly_name: "Living Room Lights - Brightness Actual" friendly_name: "Living Room Lights - Brightness Actual"
unique_id: e557022a-184f-4111-bb6a-6c0869cce42c unique_id: e557022a-184f-4111-bb6a-6c0869cce42c
value_template: > value_template: >
{% set current = state_attr('light.living_room_lights','brightness') %} {% from 'lighting.jinja' import get_brightness_pct %}
{{ ((current / 255) * 100) | float | round(2) }} {{ get_brightness_pct('light.living_room_lights') }}
availability_template: "{{ is_state('light.living_room_lights','on') }}" availability_template: "{{ is_state('light.living_room_lights','on') }}"
unit_of_measurement: '%' unit_of_measurement: '%'
icon_template: mdi:brightness-percent icon_template: mdi:brightness-percent
@ -585,7 +585,9 @@ automation:
target: target:
entity_id: timer.living_room_motion_timer entity_id: timer.living_room_motion_timer
data: 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: - conditions:
- condition: trigger - condition: trigger
id: timer-finished id: timer-finished
@ -773,7 +775,9 @@ automation:
target: target:
entity_id: timer.basement_studio_door_timer entity_id: timer.basement_studio_door_timer
data: 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: - conditions:
- condition: trigger - condition: trigger
id: basement-occupied id: basement-occupied
@ -799,7 +803,9 @@ automation:
target: target:
entity_id: timer.basement_studio_door_timer entity_id: timer.basement_studio_door_timer
data: 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: - conditions:
- condition: trigger - condition: trigger
id: timer-finished id: timer-finished

View File

@ -200,10 +200,11 @@ script:
entity_id: input_datetime.kallen_school_day_start entity_id: input_datetime.kallen_school_day_start
data: data:
time: > time: >
{% from 'time.jinja' import set_time_from_calendar %}
{% if is_state('input_boolean.two_hour_delay','on') %} {% if is_state('input_boolean.two_hour_delay','on') %}
11:00 11:00
{% else %} {% 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 %} {% endif %}
- delay: - delay:
seconds: 1 seconds: 1

View File

@ -281,9 +281,8 @@ automation:
- if: - if:
- condition: template - condition: template
value_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") %} {% from 'time.jinja' import calendar_event_today %}
true {{ calendar_event_today('calendar.kallen_school_days') }}
{%- endif -%}
then: then:
- service: script.kallen_school_today - service: script.kallen_school_today
- service: input_boolean.turn_on - service: input_boolean.turn_on
@ -418,13 +417,15 @@ script:
entity_id: input_datetime.kallen_school_day_end entity_id: input_datetime.kallen_school_day_end
data: data:
time: > 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 - service: input_datetime.set_datetime
target: target:
entity_id: input_datetime.kallen_school_day_start entity_id: input_datetime.kallen_school_day_start
data: data:
time: > 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 - service: input_boolean.turn_on
entity_id: entity_id:
- input_boolean.kallen_school_today - input_boolean.kallen_school_today

View File

@ -32,9 +32,8 @@ automation:
- if: - if:
- condition: template - condition: template
value_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") %} {% from 'time.jinja' import calendar_event_today %}
true {{ calendar_event_today('calendar.family_tinawork') }}
{%- endif -%}
then: then:
- service: script.tina_work_today - service: script.tina_work_today
- service: input_boolean.turn_on - service: input_boolean.turn_on
@ -59,13 +58,15 @@ script:
entity_id: input_datetime.tina_workday_start entity_id: input_datetime.tina_workday_start
data: data:
time: > 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 - service: input_datetime.set_datetime
target: target:
entity_id: input_datetime.tina_workday_end entity_id: input_datetime.tina_workday_end
data: data:
time: > 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 - service: input_boolean.turn_on
target: target:
entity_id: input_boolean.work_today entity_id: input_boolean.work_today
@ -84,9 +85,8 @@ script:
- if: - if:
- condition: template - condition: template
value_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") %} {% from 'time.jinja' import calendar_event_today %}
true {{ calendar_event_today('calendar.family_tinawork') }}
{%- endif -%}
then: then:
- service: script.tina_work_today - service: script.tina_work_today
else: else:

View File

@ -22,9 +22,8 @@ automation:
condition: condition:
- condition: template - condition: template
value_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") %} {% from 'time.jinja' import calendar_event_today %}
true {{ calendar_event_today('calendar.tony_s_twitch_schedule') }}
{%- endif -%}
action: action:
- service: script.tony_stream_today - service: script.tony_stream_today
@ -67,7 +66,8 @@ script:
entity_id: input_datetime.tony_streaming_start_time entity_id: input_datetime.tony_streaming_start_time
data: data:
time: > 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 - service: input_boolean.turn_on
entity_id: input_boolean.tony_streaming_today entity_id: input_boolean.tony_streaming_today

View File

@ -1,35 +1,10 @@
> >
{# Daily Briefing #} {# Daily Briefing #}
{%- macro getReport() -%} {%- macro getReport() -%}
{% from 'speech.jinja' import greeting %}
<p> <p>
{% if now().strftime('%H')|int < 12 %} {{ greeting() }}
{{ [
"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 %}
<p> <p>
{% if is_state('input_boolean.tornado_alarm','on') %} {% if is_state('input_boolean.tornado_alarm','on') %}

View File

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

View File

@ -1,36 +1,9 @@
> >
{# Morning Briefing #} {# Morning Briefing #}
{%- macro getReport() -%} {%- macro getReport() -%}
{% from 'speech.jinja' import greeting_nodate %}
<p> <p>
{% if now().strftime('%H')|int < 12 and now().strftime('%H')|int > 6 %} {{ greeting_nodate() }}
{{ [
"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 %}
</p> </p>
<p> <p>
{{ [ {{ [

View File

@ -1,8 +1,11 @@
> >
{# Nightly Briefing #} {# Nightly Briefing #}
{%- macro getReport() -%} {%- macro getReport() -%}
{% from 'speech.jinja' import greeting %}
<p>
{{ greeting() }}
</p>
<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. ", "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. ", "The day grows short, let us gather around the campfire for an epic tale of things to come. ",