From 5d31c654b98a537c2c1b844762f264f87ffdac08 Mon Sep 17 00:00:00 2001 From: Stuart Lamont Date: Sun, 4 Jun 2023 17:34:51 +1000 Subject: [PATCH] Added For Loops over arrays where it made sense --- inside temps.md | 27 +++++++++++++++++-- pending updates.md | 66 +++++++++++++++++++++++++++++++--------------- 2 files changed, 70 insertions(+), 23 deletions(-) diff --git a/inside temps.md b/inside temps.md index 7b36aa2..c1c1209 100644 --- a/inside temps.md +++ b/inside temps.md @@ -26,5 +26,28 @@ Inside it's currently {{ states('sensor.temperature_dining') }} °C in the Dinin and {{ states('sensor.master_bedroom_purifier_temperature') }} °C in the Master Bedroom. ``` -## TO-DO -After setting this up, I've realised the better way to handle this would be to create an Array of dictionaries containing the Room Name, and the temperatures, then iterating through the array for the announcement. \ No newline at end of file +### But there's a better way! +We're going to crate an Array of Dictionaries, and iterate through the array. + +```jinja + +{% set inside_temps = { + # A List of Dictionaries + # The Dictionary Key is the `entity_id` of the temperature sensor in Home Assistant + # the Dictionary Value is the Name of the room we want our TTS engine to say. + # For the TTS to sound more 'natural' I've added ", and" to the second last item in the array + + 'sensor.temperature_dining': 'Dining Room', + 'sensor.lounge_ac_inside_temperature': 'Lounge Room', + 'sensor.temperature_office': 'Office, and', + 'sensor.master_bedroom_purifier_temperature': 'Master Bedroom' + +} %} + + + +Inside it's currently +{% for sensor, room in inside_temps.items() %} + {{ states(sensor) }} °C in the {{ room }} +{% endfor %} +``` \ No newline at end of file diff --git a/pending updates.md b/pending updates.md index 86dd42c..f8b2c73 100644 --- a/pending updates.md +++ b/pending updates.md @@ -13,24 +13,52 @@ I'll do that for version 2 maybe.... ### The Template ```jinja -{% if states('update.home_assistant_operating_system_update') == 'off' %} +{% if states('update.home_assistant_operating_system_update') == 'on' %} There's a Home Assistant O S Update pending. The Installed version is {{ state_attr('update.home_assistant_operating_system_update', 'installed_version') }} The Available version is {{ state_attr('update.home_assistant_operating_system_update', 'latest_version') }} {% endif %} -{% if states('update.home_assistant_core_update') == 'off' %} +{% if states('update.home_assistant_core_update') == 'on' %} There's a Home Assistant Core Update pending. The Installed version is {{ state_attr('update.home_assistant_core_update', 'installed_version') }} The Available version is {{ state_attr('update.home_assistant_core_update', 'latest_version') }} {% endif %} -{% if states('update.esphome_update') == 'off' %} +{% if states('update.esphome_update') == 'on' %} There's an E S P Home Update pending. The Installed version is {{ state_attr('update.esphome_update', 'installed_version') }} The Available version is {{ state_attr('update.esphome_update', 'latest_version') }} {% endif %} ``` +### A Better Way +Let's try doing this with an Array instead + +```jinja + + +{% set entities = { + # A List of Dictionaries + # The Dictionary Key is the `entity_id` in Home Assistant + # the Dictionary Value is what we want our Text to Speech engine to call it. + + 'update.home_assistant_operating_system_update': 'Home Assistant OS Update', + 'update.home_assistant_core_update': 'Home Assistant Core', + 'update.esphome_update': 'E S P Home', + +} %} + +{% for entity, name in entities.items() %} + {% if states(entity) == 'on' %} + There's a {{ name }} pending. + The Installed version is {{ state_attr( entity, 'installed_version') }} + The Available version is {{ state_attr(entity, 'latest_version') }} + {% endif %} +{% endfor %} +``` +This way, we can easily add or remove "Update" entity dictionaries to the `entities` array. +The `entity_id` is the "Key" of the dictionary, and we put a "Text to Speech" Compatible name in the Value. + ### The Home Assistant Script Here's the script I put into my `scripts.yaml` or you can just paste it into the Home Assistant UI when editing the script in YAML mode. @@ -40,24 +68,20 @@ sequence: - service: notify.alexa_media data: message: >- - {% if states('update.home_assistant_operating_system_update') == 'on' %} - There's a Home Assistant O S Update pending. The Installed version is - {{ state_attr('update.home_assistant_operating_system_update', - 'installed_version') }}. - The Available version is {{ state_attr('update.home_assistant_operating_system_update', 'latest_version') }}. - {% endif %} - {% if states('update.home_assistant_core_update') == 'on' %} - There's a Home Assistant Core Update pending. The Installed version is - {{ state_attr('update.home_assistant_core_update', 'installed_version') }}. - The Available version is {{ state_attr('update.home_assistant_core_update', 'latest_version') }}. - {% endif %} - - {% if states('update.esphome_update') == 'on' %} - There's an E S P Home Update pending. The Installed version is - {{ state_attr('update.esphome_update', 'installed_version') }}. - The Available version is - {{ state_attr('update.esphome_update', 'latest_version') }}. - {% endif %} + {% set entities = { + 'update.home_assistant_operating_system_update': 'Home Assistant OS Update', + 'update.home_assistant_core_update': 'Home Assistant Core', + 'update.esphome_update': 'E S P Home', + + } %} + + {% for entity, name in entities.items() %} + {% if states(entity) == 'on' %} + There's a {{ name }} pending. + The Installed version is {{ state_attr( entity, 'installed_version') }} + The Available version is {{ state_attr(entity, 'latest_version') }} + {% endif %} + {% endfor %} target: - media_player.dining_room_echo_plus mode: single