Skip to content

Instantly share code, notes, and snippets.

@Didgeridrew
Last active June 27, 2023 14:27
Show Gist options
  • Save Didgeridrew/ca06d99d0be800c8cdd49d51edfe5923 to your computer and use it in GitHub Desktop.
Save Didgeridrew/ca06d99d0be800c8cdd49d51edfe5923 to your computer and use it in GitHub Desktop.
HA: Alexa TTS - list of rooms with open windows from binary sensor group, legacy group, or state object template
## *Using binary sensor groups and Area names*
### scripts.yaml
tts_opened_windows:
alias: Notify - Alexa - List Open Windows
sequence:
- service: notify.alexa_media_last_called
data:
data:
type: tts
message: '{{active_windows}}'
variables:
active_windows: |-
{% set ow = expand('binary_sensor.all_window_sensors')
| selectattr('state','eq','on') |
map(attribute='entity_id') | map('area_name')
| unique | reject('eq',None) | sort | map('lower') | list %}
{% set qty = ow | count %}
{% if qty != 0 %}
There {{'is an' if qty==1 else 'are'}} open window{{' ' if qty==1
else 's '}}in the {{' and '.join((ow|join(', ')).rsplit(', ', 1)) }}.
{% else %}
All windows are closed.
{% endif %}
# If you do not have, or do not want to set up a group for your sensors you can use the following
# but you may need to insert additional filters if you have other binary sensors
# with 'window' in the entity_id...
variables:
active_windows: |-
{% set windows = states.binary_sensor
| selectattr('entity_id', 'search', 'window' )
| rejectattr('entity_id', 'search', 'all' )
| list %}
{{ windows | selectattr('state','eq','on') |
map(attribute='entity_id') | map('area_name')
| unique | reject('eq',None)
| sort | map('lower') | list}}
############################ LEGACY METHODS #############################
# The following pre-date the implementation of type-specific groups and
# the ability to use Areas in templates. As of Home Assistant 2022.7.2
# they still work, but they are less efficient than the script at the top of
# the page.
## *Using legacy Groups and Look Up Table*
### scripts.yaml
tts_opened_windows:
alias: 'TTS List of Open Windows'
sequence:
- service: notify.alexa_media_basement_dot
data:
message: '{{active_windows}}'
data:
type: tts
variables:
active_windows: |-
{% if states('group.all_window_sensors') == 'off' -%}
There are no open windows
{%- else -%}
{{-'There are windows open in the'}}
{%- for window in expand('group.all_window_sensors')
| rejectattr('state', 'eq', 'off') | map(attribute='entity_id')%}
{%- set active_room = dict([
('binary_sensor.cloud_bedroom_window_1', 'bedroom'),
('binary_sensor.kitchen_window', 'kitchen'),
('binary_sensor.living_room_window_1', 'living room'),
('binary_sensor.master_bedroom_window_1', 'master bedroom')
]) %}
{{- (' ')}}
{{- ('and ' if loop.last and not loop.first)}}
{{- active_room[window] -}}
{{- (',' if not loop.last and loop.length >= 3)}}
{%- endfor %}
{%- endif %}
# If the friendly_name of your entities is already TTS-friendly,
# you can leave out the look up table section and modify the variable's template as follows:
variables:
active_windows: |-
{% if states('group.all_window_sensors') == 'off' -%}
There are no open windows
{%- else %}
{{- 'There are windows open in the'}}
{%- for window in expand('group.all_window_sensors')|rejectattr('state', 'eq', 'off') | map(attribute='name')%}
{{- (' ')}}{{- ('and ' if loop.last and not loop.first)}}
{{- window -}}
{{- (',' if not loop.last and loop.length >= 3)}}
{%- endfor -%}
{%- endif %}
# If you have your TTS set to pull from an input_text, just substitute the services in the sequence...
tts_opened_windows:
alias: 'TTS List of Open Windows'
sequence:
- service: input_text.set_value
target:
entity_id: input_text.your_input_text
data:
value: '{{active_windows}}'
variables:
active_windows:
### SEE ABOVE ###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment