Refactor speech briefing macros for improved clarity and functionality. Updated documentation, enhanced weather reporting with air quality and UV index checks, and streamlined location reporting using a new helper macro. Removed deprecated air quality macro.

This commit is contained in:
Carlo Costanzo
2025-12-03 11:16:32 -05:00
parent 3721935082
commit 92c4b53685

View File

@@ -1,19 +1,13 @@
#------------------------------------------- ######################################################################
# Speech Briefing Macros # @CCOSTAN - Follow Me on X
# Description: Contains various macros for generating speech briefings # For more info visit https://www.vcloudinfo.com/click-here
#
# Features:
# - Weather reports (inside and outside)
# - Responsibility reminders
# - Dark outside notification
# - Holiday and countdown announcements
# - Air quality reports
# - Inspirational quotes and facts
#
# Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig # Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig
# Blog Post: https://www.vcloudinfo.com/category/home-assistant # -------------------------------------------------------------------
# Follow me on https://www.vcloudinfo.com/click-here # Speech Briefing Macros - TTS prompt helpers for daily briefings
#------------------------------------------- # Generates macro prompts for weather, reminders, and AI-driven speech routines.
# -------------------------------------------------------------------
# Weather, responsibilities, holidays, air quality, and fact prompts parsed by speech_processing/speech_engine.
######################################################################
>- >-
@@ -32,37 +26,53 @@
{%- endmacro -%} {%- endmacro -%}
{%- macro inside_weather() -%} {%- macro inside_weather() -%}
Inside the house, it is {{ states.climate.downstairs.attributes['current_temperature'] }} degrees with {{ states('sensor.downstairs_thermostat_humidity') }} percent humidity. [Only mention humidity if it seems unusually high] {% set temp = state_attr('climate.downstairs', 'current_temperature') | default('unknown') %}
{% set humidity = states('sensor.downstairs_thermostat_humidity') %}
Inside the house, it is {{ temp }} degrees{% if humidity not in ['unknown','unavailable','none'] %} with {{ humidity }} percent humidity{% endif %}. [Only mention humidity if it seems unusually high]
{%- endmacro -%} {%- endmacro -%}
{% macro outside_weather() %} {% macro outside_weather() %}
[Here is the current weather outside] [Here is the current weather outside]
Air Quality: {{state_attr('sensor.bear_stone_common_air_quality_index', 'description')}} {% set aq_description = state_attr('sensor.bear_stone_common_air_quality_index', 'description') | default('') %}
{%- for entity in states.sensor if 'pirateweather' in entity.entity_id %} {% set aq_index = states('sensor.bear_stone_common_air_quality_index') | int(0) %}
{%- set state = entity.state %} {% if aq_description %}
{%- set unit = entity.attributes.unit_of_measurement if 'unit_of_measurement' in entity.attributes else '' %} Air Quality: {{ aq_description }}
{%- set friendly_name = ' '.join(entity.attributes.friendly_name.split(' ')[1:]) %} {% endif %}
{%- if state not in ['0', '0.0', 'none'] and 'UV Index' not in friendly_name %} {% if aq_index >= 150 %}
{%- if 'Temperature' in friendly_name -%} [Air quality is unhealthy; limit outdoor activity]
{{ friendly_name }}: {{ state }} {{ unit }} {% endif %}
{%- elif 'Minutely' in friendly_name -%} {% set pirateweather_metrics = states.sensor
{{ friendly_name }}: {{ state }} {{ unit }} | selectattr('entity_id','search','pirateweather')
{%- elif 'Precip' in friendly_name -%} | rejectattr('state','in',['0','0.0','none','unknown','unavailable'])
{{ friendly_name }}: {{ state }} {{ unit }} | list %}
{%- elif 'Wind Speed' in friendly_name and state | float(0) > 15 -%} {% for entity in pirateweather_metrics %}
{{ friendly_name }}: {{ state }} {{ unit }} {% set value = entity.state %}
{%- elif 'Cloud Coverage' in friendly_name and state | float(0) > 75 -%} {% set unit = entity.attributes.unit_of_measurement | default('') %}
{{ friendly_name }}: {{ state }} {{ unit }} {% set base_name = entity.attributes.friendly_name | default(entity.entity_id) %}
{%- elif 'Humidity' in friendly_name and (state | float(0) < 50 or state | float(0) > 85) -%} {% set friendly_name = ' '.join(base_name.split(' ')[1:]) or base_name %}
{{ friendly_name }}: {{ state }} {{ unit }} {% set numeric = value | float(0) %}
{%- elif 'Nearest Storm Distance' in friendly_name and state | float(0) <= 10 -%} {% set include = (
{{ friendly_name }}: {{ state }} {{ unit }} 'Temperature' in friendly_name
{%- endif -%} or 'Minutely' in friendly_name
{%- endif %} or 'Precip' in friendly_name
{% endfor -%} or ('Wind Speed' in friendly_name and numeric > 15)
or ('Cloud Coverage' in friendly_name and numeric > 75)
or ('Humidity' in friendly_name and (numeric < 50 or numeric > 85))
or ('Nearest Storm Distance' in friendly_name and numeric <= 10)
) %}
{% if include and 'UV Index' not in friendly_name %}
{{ friendly_name }}: {{ value }} {{ unit }}
{% endif %}
{%- endfor %}
{% set uv_index = states('sensor.pirateweather_uv_index') | float(0) %}
{% if uv_index >= 6 %}
UV index is {{ uv_index }}.
[Give a helpful suggestion based on the current UV index or weather conditions]
{% endif %}
{%- if states('sensor.nws_alerts') | int(0) > 0 -%} {%- if states('sensor.nws_alerts') | int(0) > 0 -%}
{%- set alert_description = state_attr('sensor.nws_alerts', 'Alerts') %} {%- set alert_description = state_attr('sensor.nws_alerts', 'Alerts') | default('') %}
[Summarize the included weather alert and give overall details on any storms relevant to the residents of the home. Use the Situation Overview Section to best understand what is going on - Be sure to highlight any impacts to Seminole County or Tallahassee] [Summarize the included weather alert and give overall details on any storms relevant to the residents of the home. Use the Situation Overview Section to best understand what is going on - Be sure to highlight any impacts to Seminole County or Tallahassee]
{{ alert_description }} {{ alert_description }}
[END of Weather Alert] [END of Weather Alert]
@@ -162,26 +172,7 @@
{%- endmacro -%} {%- endmacro -%}
{%- macro uv() -%} {%- macro uv() -%}
{% if states.sensor.pirateweather_uv_index.state|int(9999)>= 6 %} {# UV details are now included in outside_weather with a threshold gate #}
UV index is {{ states.sensor.pirateweather_uv_index.state }}.
{%- for entity in states.sensor if 'pirateweather' in entity.entity_id %}
{%- set state = entity.state %}
{%- set unit = entity.attributes.unit_of_measurement if 'unit_of_measurement' in entity.attributes else '' %}
{%- set friendly_name = ' '.join(entity.attributes.friendly_name.split(' ')[1:]) %}
{%- if state not in ['0', '0.0', 'none'] and 'UV Index' not in friendly_name %}
{%- if 'Temperature' in friendly_name -%}
{{ friendly_name }}: {{ state }} {{ unit }}
{%- elif 'Minutely' in friendly_name -%}
{{ friendly_name }}: {{ state }} {{ unit }}
{%- elif 'Cloud Coverage' in friendly_name and state | float > 75 -%}
{{ friendly_name }}: {{ state }} {{ unit }}
{%- elif 'Humidity' in friendly_name and (state | float < 50 or state | float > 85) -%}
{{ friendly_name }}: {{ state }} {{ unit }}
{%- endif -%}
{%- endif %}
{% endfor -%}
[Give a helpful suggestion based on the current UV index or weather conditions]
{% endif %}
{%- endmacro -%} {%- endmacro -%}
{%- macro holiday() -%} {%- macro holiday() -%}
@@ -215,15 +206,30 @@
{% endif %} {% endif %}
{%- endmacro -%} {%- endmacro -%}
{% macro air_quality() %}
{{states('sensor.bear_stone_common_air_quality_index')}}
{{state_attr('sensor.bear_stone_common_air_quality_index', 'description')}}
{% endmacro %}
{% macro fact_of_the_day() %} {% macro fact_of_the_day() %}
[incorporate into the message a relevant fact about something that happened in the past on this day] [incorporate into the message a relevant fact about something that happened in the past on this day]
{% endmacro %} {% endmacro %}
{# friendly duration/location helper #}
{% macro friendly_location(entity_id, name) %}
{% set entity = states[entity_id] %}
{% if not entity %}
{{ name }}: Away
{% else %}
{% set state = states(entity_id) %}
{% if state == 'unknown' %}
{{ name }}: Away
{% else %}
{% set last_changed = as_timestamp(entity.last_changed) %}
{% set seconds = (as_timestamp(now()) - last_changed) | int(0) if last_changed else 0 %}
{% set hours = (seconds // 3600) | int %}
{% set minutes = ((seconds % 3600) // 60) | int %}
{% set duration = (hours ~ ' hours') if hours >= 1 else (minutes ~ ' minutes') %}
{{ name }}: {{ state }}{% if seconds >= 60 %} for {{ duration }}{% endif %}
{% endif %}
{% endif %}
{% endmacro %}
{# a macro that removes all newline characters, empty spaces, and returns formatted text and replaces underscores with spaces #} {# a macro that removes all newline characters, empty spaces, and returns formatted text and replaces underscores with spaces #}
{%- macro cleanup(data) -%} {%- macro cleanup(data) -%}
{%- for item in data.split("\n") if item | trim != "" -%} {%- for item in data.split("\n") if item | trim != "" -%}
@@ -249,30 +255,10 @@
[Current date time: {{ month }} {{ day }}, {{ year }} {{ time }}] [Current date time: {{ month }} {{ day }}, {{ year }} {{ time }}]
[Resident: Location: [Resident: Location:
- Carlo: - {{ friendly_location('sensor.carlo_place', 'Carlo') }}
{%- if states('sensor.carlo_place') != 'unknown' %} - {{ friendly_location('sensor.stacey_place', 'Stacey') }}
{{ states('sensor.carlo_place') }} - {{ friendly_location('sensor.justin_place', 'Justin') }}
{% else -%} - {{ friendly_location('sensor.paige_place', 'Paige') }}
Away
{% endif -%}
- Stacey:
{%- if states('sensor.stacey_place') != 'unknown' %}
{{ states('sensor.stacey_place') }}
{% else -%}
Away
{% endif -%}
- Justin:
{%- if states('sensor.justin_place') != 'unknown' %}
{{ states('sensor.justin_place') }}
{% else -%}
Away
{% endif -%}
- Paige:
{%- if states('sensor.paige_place') != 'unknown' %}
{{ states('sensor.paige_place') }}
{% else -%}
Away
{% endif -%}
{% if range(1, 100) | random <= 25 %} {% if range(1, 100) | random <= 25 %}
Cat Molly: Always home. Cat Molly: Always home.
{% endif %} {% endif %}
@@ -298,13 +284,7 @@
{{ inside_weather() }} {{ inside_weather() }}
{% endif %} {% endif %}
{% if call_outside_weather == 1 and is_state('sun.sun', 'above_horizon') %}
{{ outside_weather() }} {{ outside_weather() }}
{% endif %}
{% if (states('sensor.bear_stone_common_air_quality_index')|int(0)) > 50 %}
{{ air_quality() }}
{% endif %}
{% if (states('sensor.blitzortung_lightning_counter')|int(0)) > 0 %} {% if (states('sensor.blitzortung_lightning_counter')|int(0)) > 0 %}
{{ lightning() }} {{ lightning() }}
@@ -353,9 +333,9 @@
{% endif %} {% endif %}
{# call a Random fact about the house or inspiration quote #} {# call a Random fact about the house or inspiration quote #}
{{ ([moon, uv, holiday, days_until, outside_weather, outside_weather, fact_of_the_day]|random)() }} {{ ([moon, holiday, days_until, fact_of_the_day]|random)() }}
] ]
[Previous broadcast: "{{ state_attr('sensor.openai_response', 'response') }}" ] [Previous broadcast for context: "{{ state_attr('sensor.openai_response', 'response') }}" ]
{%- endmacro -%} {%- endmacro -%}
{{- cleanup(mother_of_all_macros()) -}} {{- cleanup(mother_of_all_macros()) -}}