I fixed my problem. This problem could not have been solved by anyone else because there was missing information from the beginning. I’ll try to show what went wrong.
First I create sensors like the following (and if anyone can help me simplify any of it that would be great):
template:
sensor:
- name: CBS Programs
state: "{{ state_attr('sensor.zap2it','programme') | selectattr('@channel', 'eq', 'I9.1.70140.zap2it.com') | list | count }}"
attributes:
list: >-
{% for i in range(state_attr('sensor.zap2it','programme') | selectattr('@channel', 'eq', 'I9.1.70140.zap2it.com') | list | count) %}
{"title":"{{ (state_attr('sensor.zap2it','programme') | selectattr('@channel', 'eq', 'I9.1.70140.zap2it.com') | map(attribute='title') | list)[i]['#text'] }}",
"start":"{{ (as_timestamp(strptime((state_attr('sensor.zap2it','programme') | selectattr('@channel', 'eq', 'I9.1.70140.zap2it.com') | map(attribute='@start') | list)[i].split(' ')[0],"%Y%m%d%H%M%S", 0)) | int + states('sensor.utc_offset_seconds') | int) | timestamp_custom("%Y-%m-%d %H:%M:%S")}}",
"stop":"{{ (as_timestamp(strptime((state_attr('sensor.zap2it','programme') | selectattr('@channel', 'eq', 'I9.1.70140.zap2it.com') | map(attribute='@stop') | list)[i].split(' ')[0],"%Y%m%d%H%M%S", 0)) | int + states('sensor.utc_offset_seconds') | int) | timestamp_custom("%Y-%m-%d %H:%M:%S") }}"}
{% if not loop.last %},{% endif %}{% endfor %}
Home Assistant’s template editor shows me that this sensor’s list attribute has Result type: list so I feel confident I can use this list in a repeat for_each.
My automation would not work with the error telling me that I was not giving a list of items.
The solution ended up being to add square brackets on the above template sensor like this:
template:
sensor:
- name: CBS Programs
state: "{{ state_attr('sensor.zap2it','programme') | selectattr('@channel', 'eq', 'I9.1.70140.zap2it.com') | list | count }}"
attributes:
list: >-
[{% for i in range(state_attr('sensor.zap2it','programme') | selectattr('@channel', 'eq', 'I9.1.70140.zap2it.com') | list | count) %}
{"title":"{{ (state_attr('sensor.zap2it','programme') | selectattr('@channel', 'eq', 'I9.1.70140.zap2it.com') | map(attribute='title') | list)[i]['#text'] }}",
"start":"{{ (as_timestamp(strptime((state_attr('sensor.zap2it','programme') | selectattr('@channel', 'eq', 'I9.1.70140.zap2it.com') | map(attribute='@start') | list)[i].split(' ')[0],"%Y%m%d%H%M%S", 0)) | int + states('sensor.utc_offset_seconds') | int) | timestamp_custom("%Y-%m-%d %H:%M:%S")}}",
"stop":"{{ (as_timestamp(strptime((state_attr('sensor.zap2it','programme') | selectattr('@channel', 'eq', 'I9.1.70140.zap2it.com') | map(attribute='@stop') | list)[i].split(' ')[0],"%Y%m%d%H%M%S", 0)) | int + states('sensor.utc_offset_seconds') | int) | timestamp_custom("%Y-%m-%d %H:%M:%S") }}"}
{% if not loop.last %},{% endif %}{% endfor %}]
Now my sensor’s list attribute is truly a list of items and the automation runs without error. Sorry to @petro for wasting time not giving complete information, and a big thank you as well for pointing me towards looking elsewhere.