Expand() but with duplicates

Hello,

I have written a blueprint which is called like that:

- id: '17...67'
  alias: My Blueprint
  description: ''
  use_blueprint:
    path: myblueprints/blueprint.yaml
input:
  schedules:
  - schedule.sc1
  - schedule.sc1
  - schedule.sc3
  - schedule.sc4

The idea is to re-use schedule.sc1. A schedule.sc2 is not needed, because it’s exactly like sc1 in my special case.

The problem is that inside the blueprint, I 'm trying to iterate over the schedules like:

variables:
    _schedules: !input schedules
....

action:
- repeat:
    for_each: >
      {% for _schedule in expand(_schedules) %}

but expand() is filtering the duplicate schedule, just iterating over:

  - schedule.sc1
  - schedule.sc3
  - schedule.sc4

How can I preserve the double sc1?

I have an idea but I first need to see rest of what is in the repeat.

- repeat:
    for_each: >
      {% for _schedule in expand(_schedules) %}
      ... show the rest of what is in the repeat ...

FWIW, the “idea” is to make for_each iterate through each one of the four schedules individually (do not expand them in advance) and then expand each schedule in the sequence. However, that all depends on what else you’re doing in for_each. It’s unclear to me why you’re using a Jinja2 for-loop in a repeat for_each.

{% for _schedule in _schedules %}
{% set _schedule = expand(_schedule) %}
...

But is expand() even necessary? What is it going to provide you, in this context, that using states() or states_attr() won’t? If you need access to the state object properties like “last changed” you can use bracket notation, i.e. state[_schedule].last_changed

Thanks for your replies! The idea to use it this way was born here Calling service in a loop with different parameters

But I was now able to rewrite the code avoiding expand just using states() and states_attr() which workes fine.