diff --git a/CLAUDE.md b/CLAUDE.md index f01dac3..a30f22e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -59,4 +59,32 @@ Zigbee/ # Author: Alexei Dolgolyov (dolgolyov.alexei@gmail.com) ``` -When any blueprint file is moved ask if I need to update the link somewhere else. \ No newline at end of file +When any blueprint file is moved ask if I need to update the link somewhere else. + +## Jinja2 Whitespace Issue + +Home Assistant evaluates all blueprint variables before conditions are checked. When a variable's Jinja2 template contains whitespace (newlines, indentation), that whitespace gets included in the output even if the template logic produces an empty string. + +**Problem:** Using a variable like `{{ my_variable }}` in string replacements will include unwanted whitespace. + +**Solution:** Compute complex values inline where they're used, with `{%- -%}` whitespace control: + +```yaml +# BAD - whitespace from my_list variable will be included +my_list: > + {% for item in items %} + {{ item }} + {% endfor %} + +result: "{{ template | replace('{items}', my_list) }}" + +# GOOD - inline computation with whitespace control +result: > + {%- set ns = namespace(items = '') -%} + {%- for item in items -%} + {%- set ns.items = ns.items ~ item -%} + {%- endfor -%} + {{ template | replace('{items}', ns.items) }} +``` + +This pattern is used throughout the Immich Album Watcher blueprint for `common_date`, `common_location`, `video_warning`, and `periodic_albums_list`. diff --git a/Common/Immich Album Watcher/blueprint.yaml b/Common/Immich Album Watcher/blueprint.yaml index 3909928..84e525a 100644 --- a/Common/Immich Album Watcher/blueprint.yaml +++ b/Common/Immich Album Watcher/blueprint.yaml @@ -1077,39 +1077,35 @@ variables: {{ false }} {% endif %} - # Format the albums list for periodic summary - # Reads album name, share URL, and dates from album ID entity's attributes - periodic_albums_list: > - {% set ns = namespace(items = '') %} - {% for i in range(album_id_entities | length) %} - {% set entity_id = album_id_entities[i] %} - {% set id = states(entity_id) | default('') %} - {% if id | length > 0 and id not in ['unknown', 'unavailable'] %} - {% set name_attr = state_attr(entity_id, 'album_name') %} - {% set name = name_attr if name_attr not in [none, '', 'unknown', 'unavailable'] else id %} - {% set url_attr = state_attr(entity_id, 'share_url') %} - {% set url = url_attr if url_attr not in [none, 'unknown', 'unavailable'] else '' %} - {% set created_attr = state_attr(entity_id, 'created_at') | default('', true) %} - {% set created_dt = created_attr | as_datetime(none) if created_attr is string and created_attr | length > 0 else none %} - {% set album_created = created_dt.strftime(date_format) if created_dt else '' %} - {% set updated_attr = state_attr(entity_id, 'last_updated_at') | default('', true) %} - {% set updated_dt = updated_attr | as_datetime(none) if updated_attr is string and updated_attr | length > 0 else none %} - {% set album_updated = updated_dt.strftime(date_format) if updated_dt else '' %} - {% set item = periodic_album_template + # Complete periodic summary message + # Note: periodic_albums_list is computed inline to avoid whitespace issues + periodic_summary_formatted: > + {%- set ns = namespace(items = '') -%} + {%- for i in range(album_id_entities | length) -%} + {%- set entity_id = album_id_entities[i] -%} + {%- set id = states(entity_id) | default('') -%} + {%- if id | length > 0 and id not in ['unknown', 'unavailable'] -%} + {%- set name_attr = state_attr(entity_id, 'album_name') -%} + {%- set name = name_attr if name_attr not in [none, '', 'unknown', 'unavailable'] else id -%} + {%- set url_attr = state_attr(entity_id, 'share_url') -%} + {%- set url = url_attr if url_attr not in [none, 'unknown', 'unavailable'] else '' -%} + {%- set created_attr = state_attr(entity_id, 'created_at') | default('', true) -%} + {%- set created_dt = created_attr | as_datetime(none) if created_attr is string and created_attr | length > 0 else none -%} + {%- set album_created = created_dt.strftime(date_format) if created_dt else '' -%} + {%- set updated_attr = state_attr(entity_id, 'last_updated_at') | default('', true) -%} + {%- set updated_dt = updated_attr | as_datetime(none) if updated_attr is string and updated_attr | length > 0 else none -%} + {%- set album_updated = updated_dt.strftime(date_format) if updated_dt else '' -%} + {%- set item = periodic_album_template | replace('{album_name}', name) | replace('{album_id}', id) | replace('{album_url}', url) | replace('{album_created}', album_created) - | replace('{album_updated}', album_updated) %} - {% set ns.items = ns.items ~ item %} - {% endif %} - {% endfor %} - {{ ns.items }} - - # Complete periodic summary message - periodic_summary_formatted: > + | replace('{album_updated}', album_updated) -%} + {%- set ns.items = ns.items ~ item -%} + {%- endif -%} + {%- endfor -%} {{ periodic_summary_message_template - | replace('{albums}', periodic_albums_list) + | replace('{albums}', ns.items) | replace('{album_count}', album_ids | length | string) }} # --------------------------------------------------------------------------- diff --git a/manifest.json b/manifest.json index ff62d0a..06d58f6 100644 --- a/manifest.json +++ b/manifest.json @@ -1,3 +1,3 @@ { - "version": "1.23.2" + "version": "1.23.3" }