Fix whitespace in periodic summary by inlining albums list

Inline periodic_albums_list computation into periodic_summary_formatted
using {%- -%} whitespace control to prevent unwanted whitespace in the
formatted output. Add documentation about this Jinja2 pattern to CLAUDE.md.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-02 12:06:17 +03:00
parent 678cdafc39
commit 7b1ede89a3
3 changed files with 54 additions and 30 deletions

View File

@@ -59,4 +59,32 @@ Zigbee/
# Author: Alexei Dolgolyov (dolgolyov.alexei@gmail.com) # Author: Alexei Dolgolyov (dolgolyov.alexei@gmail.com)
``` ```
When any blueprint file is moved ask if I need to update the link somewhere else. 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`.

View File

@@ -1077,39 +1077,35 @@ variables:
{{ false }} {{ false }}
{% endif %} {% endif %}
# Format the albums list for periodic summary # Complete periodic summary message
# Reads album name, share URL, and dates from album ID entity's attributes # Note: periodic_albums_list is computed inline to avoid whitespace issues
periodic_albums_list: > periodic_summary_formatted: >
{% set ns = namespace(items = '') %} {%- set ns = namespace(items = '') -%}
{% for i in range(album_id_entities | length) %} {%- for i in range(album_id_entities | length) -%}
{% set entity_id = album_id_entities[i] %} {%- set entity_id = album_id_entities[i] -%}
{% set id = states(entity_id) | default('') %} {%- set id = states(entity_id) | default('') -%}
{% if id | length > 0 and id not in ['unknown', 'unavailable'] %} {%- if id | length > 0 and id not in ['unknown', 'unavailable'] -%}
{% set name_attr = state_attr(entity_id, 'album_name') %} {%- set name_attr = state_attr(entity_id, 'album_name') -%}
{% set name = name_attr if name_attr not in [none, '', 'unknown', 'unavailable'] else id %} {%- 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_attr = state_attr(entity_id, 'share_url') -%}
{% set url = url_attr if url_attr not in [none, 'unknown', 'unavailable'] else '' %} {%- 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_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 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 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_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 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 album_updated = updated_dt.strftime(date_format) if updated_dt else '' -%}
{% set item = periodic_album_template {%- set item = periodic_album_template
| replace('{album_name}', name) | replace('{album_name}', name)
| replace('{album_id}', id) | replace('{album_id}', id)
| replace('{album_url}', url) | replace('{album_url}', url)
| replace('{album_created}', album_created) | replace('{album_created}', album_created)
| replace('{album_updated}', album_updated) %} | replace('{album_updated}', album_updated) -%}
{% set ns.items = ns.items ~ item %} {%- set ns.items = ns.items ~ item -%}
{% endif %} {%- endif -%}
{% endfor %} {%- endfor -%}
{{ ns.items }}
# Complete periodic summary message
periodic_summary_formatted: >
{{ periodic_summary_message_template {{ periodic_summary_message_template
| replace('{albums}', periodic_albums_list) | replace('{albums}', ns.items)
| replace('{album_count}', album_ids | length | string) }} | replace('{album_count}', album_ids | length | string) }}
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------

View File

@@ -1,3 +1,3 @@
{ {
"version": "1.23.2" "version": "1.23.3"
} }