For_each loop doesn't take "0" item

Hi guys, i have the following script to copy the state of many input_boolean:

alias: CopiaTermo
sequence:
  - repeat:
      for_each:
        - "0"
        - "1"
        - "2"
        - "3"
      sequence:
        - data_template:
            entity_id: input_boolean.temp_00{{states('input_text.termoa') ~ "_0_" ~ repeat.index}}
          service_template: >-
            input_boolean.turn_{{states("input_boolean.temp_00" ~
            states('input_text.termoda') ~ "_0_" ~ repeat.index)}}
mode: single

The proble is that the “0” item result in input_boolean.turn_unknown service (but the relative input_boolean exist and all other item do the work). Maybe a bug? How can I workaround?

Second question: how can I substitute 0, 1, 2, 3 items with a template (example 0 to 3 numbers)?

Thanks a lot!

You are using the wrong variable. If you want the value of the item to be used you need to use repeat.item not repeat.index (Repeat Loop Variables) Repeat’s index starts at 1.

FWIW, data_template and service_template are no longer required for most service calls.

alias: CopiaTermo
sequence:
  - repeat:
      for_each:
        - "{{ 5 - 5 }}"
        - "1"
        - "2"
        - "3"
      sequence:
        - data:
            entity_id: input_boolean.temp_00{{states('input_text.termoa') ~ "_0_" ~ repeat.item}}
          service: >-
            input_boolean.turn_{{states("input_boolean.temp_00" ~
            states('input_text.termoda') ~ "_0_" ~ repeat.item)}}
mode: single

Thank you very much, I don’t understand that: may you explain?

I provided an example of how to substitute an item with a template… You did not ask for a specific template, so I used a very generic math equation that would output the same as your original item. If that isn’t what you meant, please clarify your question.

Yes, i tried to use that jinjia2 code to generate item from 0 to 23 but even if in developer model works, into the automation not… how to adapt? It’s really hard for me jinjia and yaml sintax…

{% for s in range(24)   %}        - "{{s}}"
{% endfor %}

You cannot use Jinja to build yaml… Why don’t you show/explain to us what you are trying to accomplish? This method seems overly complicated.

Asking a Good Question

I’m sorry, probably my english is bad. I thought was clear what I want… I want to substitute:

    - "0"
    - "1"
    - "2"
    - "3"

with a template that says “from 0 to 3”.

I understand that… as I said before, you cannot build yaml with Jinja.

If you know you want the range 0-23, you could use a counted repeat and base your other templates off repeat.index -1 instead:

alias: CopiaTermo
sequence:
  - repeat:
      count: 24
      sequence:
        - data:
            entity_id: input_boolean.temp_00{{states('input_text.termoa') ~ "_0_" ~ (repeat.index - 1) }}
          service: >-
            input_boolean.turn_{{states("input_boolean.temp_00" ~
            states('input_text.termoda') ~ "_0_" ~ (repeat.index - 1))}}
mode: single

My question was more about the overall goal of the automation and script… what is it that you are trying to do that requires 96 helper entities?

Many thanks!

The puprose: I have many heater, I want to turn on or off each separately for every hour of every day, so I created 168 input_boolean for each to manage them. All works smoothly.
That script I want to use for copying hours from one heater to another one.

Sounds like you can should have a look at the schedule helper.
I don’t understand what you are doing but I’m sure it’s not the best method

1 Like

That’s the full project, any suggestion is welcome!

Well I wouldn’t have done it that way.
As said, use the schedule or better yet, calendar to do this.
If you use the calendar then you can have multiple temperatures also very easily by making the description of the event the temperature, or just low, mid, high.

And also, the time pattern automations. They are not efficient.
I have not read every detail of your setup since I’m using my phone but it seemed as every hour could be different.
And you have a time pattern that triggers every minute.

I understand this is a mammoth thing you’ve created and your proud of it.
And I applaud you for doing it but I also believe you can do it much simpler.

I suggest you look at the schedule or calendar.
Schedule will probably be enough if you just want two temperatures, but if you want more then I believe calendar will work fine.
Keep what you have and experiment with schedule/calendar and see what you can do.

1 Like