Skip to content

Commit fb5a18d

Browse files
committed
fix: prefer named places in speech briefings
1 parent c94b61d commit fb5a18d

1 file changed

Lines changed: 37 additions & 23 deletions

File tree

config/templates/speech/briefing.yaml

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
# Greetings use only that dynamic in-home audience; off-site residents are
1717
# reported separately by their current known location.
1818
# Paige Home and Justin Dorm are spoken as home only for their respective child.
19-
# Missing current locations fall back to neighborhood, city, then last named place.
19+
# Named personal zones such as Paige Work outrank reverse-geocoded venues.
20+
# Prefer clean current place names; missing locations fall back to retained
21+
# place name, neighborhood, city, then last named place.
2022
# Notes: Previous broadcast text is stale context only; current sensor data
2123
# stays authoritative for entry point and action wording.
2224
# Notes: Weather context is included during the useful daytime window, when
@@ -109,8 +111,8 @@
109111
{%- endmacro -%}
110112

111113
{%- macro lightning() -%}
112-
There have been {{ states('sensor.blitzortung_lightning_counter') }} lightning strikes detected within {{(states('sensor.blitzortung_lightning_distance') | int(9999)/ 1.69) | round (1, 'floor')}} Miles of our House.
113-
Nearest Storm Distance : {{states('sensor.pirateweather_nearest_storm_distance')}} Miles.
114+
{% set distance_km = states('sensor.blitzortung_lightning_distance') | float(none) %}
115+
There have been {{ states('sensor.blitzortung_lightning_counter') }} lightning strikes detected{% if distance_km is not none %} within {{ (distance_km / 1.609344) | round(1) }} miles of our house{% else %} nearby{% endif %}.
114116
{%- endmacro -%}
115117

116118
{%- macro fridge() -%}
@@ -141,7 +143,7 @@
141143
{%- endmacro -%}
142144

143145
{%- macro lock_check() -%}
144-
{% if states.group.locks.state !='locked' -%}
146+
{% if not is_state('group.locks', 'locked') -%}
145147
The
146148
{%- for state in states.lock -%}
147149
{%- endfor %}
@@ -156,7 +158,7 @@
156158
{%- endmacro -%}
157159

158160
{%- macro garage_check() -%}
159-
{% if states.group.garage_doors.state !='closed' -%}
161+
{% if not is_state('group.garage_doors', 'closed') -%}
160162
The
161163
{%- for state in states.cover -%}
162164
{%- endfor %}
@@ -282,31 +284,31 @@
282284
{%- endmacro -%}
283285

284286
{%- macro holiday() -%}
285-
{% if states.sensor.holiday.state != '' %}
286-
Today is {{ states.sensor.holiday.state }}.
287+
{% if states('sensor.holiday') != '' %}
288+
Today is {{ states('sensor.holiday') }}.
287289
{% endif %}
288290
{%- endmacro -%}
289291

290292
{# YOUTUBE VIDEO ********* https://www.vcloudinfo.com/2019/11/adding-days-until-sensor-to-my-home-assistant-speech-routines.html #}
291293
{%- macro days_until() -%}
292294
{%- if states('sensor.mothers_countdown') | int(9999)< 20 -%}
293-
and don't forget, there is only {{ states.sensor.mothers_countdown.state }} days until Mothers day!
295+
and don't forget, there is only {{ states('sensor.mothers_countdown') }} days until Mothers day!
294296
{%- elif states('sensor.fathers_countdown') | int(9999)< 20 -%}
295-
and don't forget, there are {{ states.sensor.fathers_countdown.state }} days until Fathers day!
297+
and don't forget, there are {{ states('sensor.fathers_countdown') }} days until Fathers day!
296298
{%- elif states('sensor.easter_countdown') | int(9999)< 15 -%}
297-
and don't forget, there are {{ states.sensor.easter_countdown.state }} colorful days until Easter Sunday!
299+
and don't forget, there are {{ states('sensor.easter_countdown') }} colorful days until Easter Sunday!
298300
{%- elif states('sensor.thanksgiving_day_countdown') | int(9999)< 10 and states('sensor.thanksgiving_day_countdown') | int(9999)> 0 -%}
299-
and don't forget, there are {{ states.sensor.thanksgiving_day_countdown.state }} thankful days until Thanksgiving
301+
and don't forget, there are {{ states('sensor.thanksgiving_day_countdown') }} thankful days until Thanksgiving
300302
{%- elif states('sensor.thanksgiving_day_countdown') | int(9999)< 1 -%}
301303
and don't forget, Thanksgiving is tomorrow!
302304
{%- elif states('sensor.halloween_countdown') | int(9999)< 30 and states('sensor.halloween_countdown') | int(9999)> 0 -%}
303-
and don't forget, there are {{ states.sensor.halloween_countdown.state }} Spooky days until Halloween!
305+
and don't forget, there are {{ states('sensor.halloween_countdown') }} Spooky days until Halloween!
304306
{%- elif states('sensor.halloween_countdown') | int(9999)< 1 -%}
305307
and don't forget, Tomorrow is Halloween!
306308
{%- elif states('sensor.chanukkah_countdown') | int(9999)< 15 -%}
307-
and don't forget, there are {{ states.sensor.chanukkah_countdown.state }} days until Chanukkah!
309+
and don't forget, there are {{ states('sensor.chanukkah_countdown') }} days until Chanukkah!
308310
{%- elif states('sensor.christmas_countdown') | int(9999)< 30 and states('sensor.christmas_countdown') | int(9999)> 0 -%}
309-
and don't forget, there are {{ states.sensor.christmas_countdown.state }} Merry days until Christmas!
311+
and don't forget, there are {{ states('sensor.christmas_countdown') }} Merry days until Christmas!
310312
{%- elif states('sensor.christmas_countdown') | int(9999)< 1 -%}
311313
and don't forget, Santa Claus is coming tomorrow!
312314
{% endif %}
@@ -320,29 +322,40 @@
320322
{% else %}
321323
{% set person_state = states(person_id) %}
322324
{% set unknown_locations = ['unknown', 'unavailable', '', 'none', 'not_home', 'not home', 'away'] %}
323-
{% set place_state = states(place_sensor_id) if place_sensor_id in states else 'unknown' %}
325+
{% set place_state = states(place_sensor_id) %}
324326
{% set place_known = place_state | lower not in unknown_locations %}
325327
{% set person_known = person_state | lower not in unknown_locations %}
328+
{% set person_key = person_state | lower %}
329+
{% set named_zone_known = person_known and person_key not in ['home', 'driving'] %}
330+
{% set zone_friendly_name = state_attr('zone.' ~ person_key, 'friendly_name') | default(person_key, true) | string | trim %}
331+
{% set zone_words = zone_friendly_name | replace('_', ' ') | trim %}
332+
{% set zone_words_key = zone_words | lower %}
333+
{% set resident_prefix = (name | lower) ~ ' ' %}
334+
{% set prefix_length = resident_prefix | length %}
335+
{% set named_zone_label = zone_words[prefix_length:] if zone_words_key[:prefix_length] == resident_prefix else zone_words %}
336+
{% set place_name = state_attr(place_sensor_id, 'place_name') | default('', true) | string | trim %}
326337
{% set neighborhood = state_attr(place_sensor_id, 'place_neighbourhood') | default('', true) | string | trim %}
327338
{% set city = state_attr(place_sensor_id, 'city') | default('', true) | string | trim %}
328339
{% set last_place = state_attr(place_sensor_id, 'last_place_name') | default('', true) | string | trim %}
340+
{% set place_name_known = place_name | lower not in unknown_locations %}
329341
{% set neighborhood_known = neighborhood | lower not in unknown_locations %}
330342
{% set city_known = city | lower not in unknown_locations %}
331343
{% set last_place_known = last_place | lower not in unknown_locations %}
332-
{% set fallback_label = neighborhood if neighborhood_known else city if city_known else last_place if last_place_known else '' %}
333-
{% set fallback_kind = 'neighborhood' if neighborhood_known else 'city' if city_known else 'place' if last_place_known else '' %}
344+
{% set current_place_label = place_name if place_name_known else place_state %}
345+
{% set fallback_label = place_name if place_name_known else neighborhood if neighborhood_known else city if city_known else last_place if last_place_known else '' %}
346+
{% set fallback_kind = 'place' if place_name_known else 'neighborhood' if neighborhood_known else 'city' if city_known else 'place' if last_place_known else '' %}
334347
{% set using_fallback = not place_known and not person_known and fallback_label %}
335-
{% set location_label = place_state if place_known else person_state if person_known else fallback_label %}
336348
{% set at_bear_stone = person_state == 'home' %}
337-
{% set location_key = location_label | lower %}
338-
{% set at_personal_home = (name == 'Paige' and location_key == 'paige home') or (name == 'Justin' and location_key == 'justin dorm') %}
349+
{% set at_personal_home = (name == 'Paige' and person_key == 'paige_home') or (name == 'Justin' and person_key == 'justin_dorm') %}
350+
{% set personal_home_label = 'Paige Home' if name == 'Paige' and person_key == 'paige_home' else 'Justin Dorm' if name == 'Justin' and person_key == 'justin_dorm' else '' %}
351+
{% set location_label = personal_home_label if at_personal_home else named_zone_label if named_zone_known else current_place_label if place_known else fallback_label %}
339352
{% set last_changed = as_timestamp(person.last_changed) %}
340353
{% set seconds = (as_timestamp(now()) - last_changed) | int(0) if last_changed else 0 %}
341354
{% set hours = (seconds // 3600) | int %}
342355
{% set minutes = ((seconds % 3600) // 60) | int %}
343356
{% set duration = (hours ~ ' hours') if hours >= 1 else (minutes ~ ' minutes') %}
344357
{% if person_state == 'driving' %}
345-
{% set driving_label = place_state if place_known else fallback_label %}
358+
{% set driving_label = current_place_label if place_known else fallback_label %}
346359
{{ name }}: driving{% if driving_label %} near {{ driving_label }}{% endif %}{% if seconds >= 60 %} for {{ duration }}{% endif %}
347360
{% elif at_bear_stone %}
348361
{{ name }}: at Bear Stone home{% if seconds >= 60 %} for {{ duration }}{% endif %}
@@ -390,7 +403,7 @@
390403
{% set no_fridge_flag = no_fridge | default(0) | int(0) %}
391404
{% set sunset_ts = as_timestamp(state_attr('sun.sun', 'next_setting'), 0) %}
392405
{% set daytime_weather_context = is_state('sun.sun', 'above_horizon') and sunset_ts > 0 and as_timestamp(now()) < (sunset_ts - 7200) %}
393-
{% set lightning_close = states('sensor.blitzortung_lightning_counter') | int(0) > 0 and (states('sensor.blitzortung_lightning_distance') | float(9999) / 1.69) <= 10 %}
406+
{% set lightning_close = states('sensor.blitzortung_lightning_counter') | int(0) > 0 and (states('sensor.blitzortung_lightning_distance') | float(9999) / 1.609344) <= 10 %}
394407
{% set weather_safety_context = (
395408
states('sensor.nws_alerts') | int(0) > 0
396409
or states('sensor.pirateweather_wind_speed') | float(0) >= 30
@@ -418,8 +431,9 @@
418431
- The announcement audience is the dynamic `People at Bear Stone` list below. Greet only those people, and never greet, welcome, or imply that any other resident is at Bear Stone.
419432
- When a presence update is useful, report each off-site resident's known location to the people at Bear Stone. Those residents are status updates, not greeting recipients.
420433
- Paige at Paige Home and Justin at Justin Dorm are each home at their own home. Do not describe either as away, and do not welcome them as being at Bear Stone.
434+
- Prefer a resident's named personal zone over a nearby reverse-geocoded venue. For example, report Paige_Work as "Paige is at work."
421435
- Other named places, including driving, are off-site locations.
422-
- When a current location is unavailable, use the last-known neighborhood, city, or named place supplied below. Keep the words "last known" so stale data is not presented as current.
436+
- When a current location is unavailable, prefer the last-known named place, then neighborhood or city supplied below. Keep the words "last known" so stale data is not presented as current.
423437
- Do not say "location unavailable." If no useful fallback exists, say the location has not updated.]
424438

425439
[LLM speech contract:

0 commit comments

Comments
 (0)