Hello,
I’ve been having issue with looping on an array in my automation…
The error is: Error: Repeat 'for_each' must be a list of items.
I’ve seen this post Need help with repeat for_each template and tried the few steps offered there, but these didn’t really help.
My for_each is:
- repeat:
for_each: >-
{{ state_attr('sensor.defi_hilo', 'next_events') }}
In the DevTools’ template, the same template gives me:
{{ state_attr('sensor.defi_hilo', 'next_events') }}
{{ state_attr('sensor.defi_hilo', 'next_events')|typeof }}
{{ state_attr('sensor.defi_hilo', 'next_events')[0].last_update|typeof }}
=>
[{'event_id': 336, 'participating': True, 'configurable': True, 'period': 'am', 'total_devices': 12, 'opt_out_devices': 0, 'pre_heat_devices': 10, 'mode': 'extreme', 'allowed_kWh': 0.0, 'used_kWh': 0.0, 'used_percentage': 0, 'last_update': datetime.datetime(2025, 12, 4, 14, 7, 22, 72967, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=68400), 'EST')), 'phases': {'settingsDeadlineUTC': datetime.datetime(2025, 12, 5, 0, 0, tzinfo=tzlocal()), 'preheat_start': datetime.datetime(2025, 12, 5, 4, 0, tzinfo=tzlocal()), 'preheat_end': datetime.datetime(2025, 12, 5, 6, 0, tzinfo=tzlocal()), 'reduction_start': datetime.datetime(2025, 12, 5, 6, 0, tzinfo=tzlocal()), 'reduction_end': datetime.datetime(2025, 12, 5, 10, 0, tzinfo=tzlocal()), 'recovery_start': datetime.datetime(2025, 12, 5, 10, 0, tzinfo=tzlocal()), 'recovery_end': datetime.datetime(2025, 12, 5, 11, 0, tzinfo=tzlocal())}, 'state': 'scheduled'}]
list
datetime
But in my automation, using variables gives me:
variables:
next_events: >
{{ state_attr('sensor.defi_hilo', 'next_events') }}
next_events_type: '{{ next_events|typeof }}'
next_events_type2: '{{ state_attr(''sensor.defi_hilo'', ''next_events'')|typeof }}'
last_update: >
{{ state_attr('sensor.defi_hilo', 'next_events')[0].last_update }}
last_update_type: '{{ last_update|typeof }}'
last_update_type2: '{{ state_attr(''sensor.defi_hilo'', ''next_events'')[0].last_update|typeof }}'
=>
next_events: >-
[{'event_id': 336, 'participating': True, 'configurable': True, 'period':
'am', 'total_devices': 12, 'opt_out_devices': 0, 'pre_heat_devices': 10,
'mode': 'extreme', 'allowed_kWh': 0.0, 'used_kWh': 0.0, 'used_percentage': 0,
'last_update': datetime.datetime(2025, 12, 4, 14, 7, 22, 72967,
tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=68400), 'EST')),
'phases': {'settingsDeadlineUTC': datetime.datetime(2025, 12, 5, 0, 0,
tzinfo=tzlocal()), 'preheat_start': datetime.datetime(2025, 12, 5, 4, 0,
tzinfo=tzlocal()), 'preheat_end': datetime.datetime(2025, 12, 5, 6, 0,
tzinfo=tzlocal()), 'reduction_start': datetime.datetime(2025, 12, 5, 6, 0,
tzinfo=tzlocal()), 'reduction_end': datetime.datetime(2025, 12, 5, 10, 0,
tzinfo=tzlocal()), 'recovery_start': datetime.datetime(2025, 12, 5, 10, 0,
tzinfo=tzlocal()), 'recovery_end': datetime.datetime(2025, 12, 5, 11, 0,
tzinfo=tzlocal())}, 'state': 'scheduled'}]
next_events_type: str
next_events_type2: list
last_update: '2025-12-04 14:07:22.072967-05:00'
last_update_type: str
last_update_type2: datetime
I’ve also tried next_events|from_json, but it, unsurprisingly, only gives me Error: ValueError: Template error: from_json got invalid input.
I’m a bit at a loss. I’ve tried to confirm the issue isn’t from the integration, but the code looks good.
The sensor is provided by the hilo integration, the attribute is defined there: hilo/custom_components/hilo/sensor.py at 8fff6cdefc5ec8b3031c06dd493711d617850f7e · dvd-dev/hilo · GitHub (and I don’t have control over the integration).
I must be missing something obvious, but at this point, I feel like I need a fresh pair of eyes… Any help would be greatly appreciated!