Need help with for_each template in automation

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!

for_each only takes simple objects, not complex objects like datetime objects. You’ll need to convert any datetime objects to datetime strings.

Datetime objects are not json serializable.

You should figure out which data points you actually need from the individual events, process the events in a variables block before the repeat, then just pass the necessary data into for_each.

So the datetimes properties inside the next_events value from the attribute is what’s causing me grief?

I didn’t see anything from the documentation regarding that’s restriction. It seems a bit weird to me.

The way the variables behave as well kind of makes me think this might be a limitation from the template engine more generally.

I suppose I’ll have to find a way to rewrite my loop with jinja templates instead…