Here’s my briefing template. Maybe you get can some ideas from it, and maybe I can get some suggestions for improvement. It does the following:
- Greeting
- Weather
- Holidays
- Deliveries
- Phone charging reminder
- Events today, and on the weekend
- Chore reminders
- SO location/commute status
It runs in the evening after someone gets home. It also runs on-demand as an Alexa skill. Next I will do a morning briefing as a variation.
{# Difference between dates in hours, roughly #}
{% macro hour_diff(first, second) -%}
{{((as_timestamp(first) - as_timestamp(second))/60.0/60.0)}}
{%- endmacro %}
{# Day of week #}
{% macro day_of_week(timestamp) -%}
{{as_timestamp(timestamp)|timestamp_custom('%A', true)|lower}}
{%- endmacro %}
{# On a weekend #}
{% macro on_weekend(timestamp) -%}
{%- if
day_of_week(timestamp) == 'saturday' or
day_of_week(timestamp) == 'sunday'
-%}
true
{%- else -%}
false
{%- endif %}
{%- endmacro %}
{# Is today #}
{% macro is_today(event) -%}
{%- if event.attributes.start_time[:10]== (now()|string)[:10] -%}
true
{%- else -%}
false
{%- endif %}
{%- endmacro %}
{# Friendly time #}
{% macro friendly_time(timestamp) -%}
{{as_timestamp(timestamp)|timestamp_custom('%-I', true)}}
{%- if as_timestamp(timestamp)|timestamp_custom('%-M', true) != '0' -%}
{{ ' ' }}{{as_timestamp(timestamp)|timestamp_custom('%-M', true)}}
{%- endif %}
{%- endmacro %}
{# Hours until a given event, approximately #}
{% macro hours_until(event) -%}
{{hour_diff(event.attributes.start_time, now())|float|round}}
{%- endmacro %}
{# Upcoming event #}
{% macro upcoming_event(event) -%}
{%- set start = event.attributes.start_time -%}
{%- if start and is_today(event) == 'true' -%}
{{event.attributes.message}} is today at {{friendly_time(start)}}
{%- elif start and on_weekend(start) == 'true' and day_of_week(now()) == 'friday' -%}
{{event.attributes.message}} is on {{day_of_week(start)}} at {{friendly_time(start)}}
{%- endif -%}
{%- endmacro %}
{# Household chore #}
{% macro chore_sentence(event, type, sentence) -%}
{%- if (event.attributes.start_time and
is_today(event) == 'true' and
event.attributes.message|lower == type) -%}
{{sentence}}
{%- endif -%}
{%- endmacro %}
{# Holiday sentence #}
{% macro holiday_sentence() -%}
{%- if (states.calendar.holidays_in_united_states.attributes.start_time and
is_today(states.calendar.holidays_in_united_states) == 'true') -%}
It is {{states.calendar.holidays_in_united_states.attributes.message}}.
{%- endif -%}
{%- endmacro %}
{# Event sentence #}
{% macro event_sentence(event, prefix, suffix) -%}
{%- set event_string = upcoming_event(event) -%}
{% if event_string != '' -%}
{{prefix}}{{event_string}}{{suffix}}{{'.'}}
{%- endif %}
{%- endmacro %}
{# Greeting sentence #}
{% macro greeting_sentence(person) %}
{% set greetings = [
"Welcome home " ~ person,
"Hey there " ~ person,
person ~ "! You're home!",
"Welcome back " ~ person
] %}
{{greetings|random}}.
{% endmacro %}
{# Closing sentence #}
{% macro closing_sentence(person) %}
{% set closings = [
"Have a great evening!",
"Enjoy your night.",
"Take it easy this evening.",
"Have fun tonight!",
] %}
{{closings|random}}
{% endmacro %}
{# Weather sentence #}
{% macro weather_sentence() -%}
It is {{states.sensor.dark_sky_summary.state.lower()}} and {{states.sensor.dark_sky_temperature.state|int|round}} degrees outside.
{{- ' ' -}}
{{- states.sensor.dark_sky_hourly_summary.state-}}
{%- endmacro %}
{# Get person's device #}
{% macro get_device(person) -%}
{% for device in states.device_tracker -%}
{% if device.name == person -%}
{{device.entity_id}}
{%- endif %}
{%- endfor %}
{%- endmacro %}
{# Get person's pronoun #}
{%- macro get_pronoun(device_entity) -%}
{{device_entity.split('.')[1].split('_')[0]}}
{%- endmacro -%}
{# How far away is a person #}
{% macro minutes_away(pronoun, part_of_day, max) -%}
{%- if max == 0 -%}
unknown
{%- else -%}
{{((states("proximity." ~ pronoun ~ "_home")|int / max|int) * states("sensor." ~ pronoun ~ "_" ~ part_of_day)|int)|int}}
{%- endif -%}
{%- endmacro %}
{# Who is the other person #}
{% macro get_other_person(person) -%}
{% for device in states.device_tracker -%}
{% if device.name != person -%}
{{device.name}}
{%- endif %}
{%- endfor %}
{%- endmacro %}
{# On My Way sentence #}
{%- macro omw_sentence(person) -%}
{%- set device_entity = get_device(person) -%}
{%- set pronoun = get_pronoun(device_entity) -%}
{%- set max = states.sensor[pronoun ~ "_distance_mean"].attributes.max_value|int -%}
{%- if states(device_entity) == 'not_home' -%}
{{person}} is on {{pronoun}} way home, about {{minutes_away(pronoun, "evening", max)}} minutes out.
{%- elif states(device_entity) != 'home' -%}
{{person}} is still at {{states(device_entity)}}.
{%- endif -%}
{%- endmacro -%}
{# Battery sentence #}
{% macro battery_sentence(person) -%}
{%- set device_entity = get_device(person) -%}
{%- set pronoun = get_pronoun(device_entity) -%}
{%- set percent = states("sensor." ~ pronoun ~ "_phone_battery") -%}
{% if percent|int < 50 %}
Remember to plug in your phone, it is at {{percent}} percent.
{% endif %}
{%- endmacro %}
{# Deliveries #}
{% macro deliveries_sentence() -%}
{% if 'delivered' in states.sensor.deliveries.attributes -%}
{{states.sensor.deliveries.attributes.delivered}}{{' '}}
{%- if states.sensor.deliveries.attributes.delivered == 1 -%}
package was
{%- else -%}
packages were
{%- endif -%}
{{' '}}delivered today.
{%- endif %}
{%- endmacro %}
{# Validate requested person #}
{% macro validate_and_brief(person) -%}
{% if person -%}
{%- set caps = person|capitalize -%}
{%- set device = get_device(caps) -%}
{%- if device -%}
{{briefing(caps)}}
{%- else -%}
{{caps}} does not live here.
{%- endif %}
{%- else -%}
Specify a person.
{%- endif %}
{%- endmacro %}
{# Output the briefing #}
{% macro briefing(person) -%}
{%- set other_person = get_other_person(person) %}
{{greeting_sentence(person)}}
{{holiday_sentence()}}
{{battery_sentence(person)}}
{{weather_sentence()}}
{{omw_sentence(other_person)}}
{{deliveries_sentence()}}
{{chore_sentence(states.calendar.general, 'trash', 'Remember to take out the trash. It is trash day.')}}
{{chore_sentence(states.calendar.general, 'recycling', 'Remember to take out the recycling. It is recycling day.')}}
{{event_sentence(states.calendar.facebook)}}
{{closing_sentence(person)}}
{% endmacro %}
{# ... And, run the briefing #}
{{validate_and_brief(states.sensor.briefing_target.state)}}