Using variables from 'for each' loop in a value template

I have an issue when I try to use a ‘for each’ loop, I can’t use variables as filters in the value_template and in the message, and an error occurs during execution. I followed the instructions on this page and tried to find a solution on the forum but I couldn’t understand how to solve it.

This is the code:

repeat:
  for_each:
    - giorni: 2
    - giorni: 7
    - giorni: 14
    - giorni: 30
  sequence:
    if:
      - condition: template
        value_template: >-
          {{ expand(integration_entities('anniversaries')) | selectattr('state',
          'eq', 'repeat.item.giorni') | list | count > 0 }}
        enabled: false
    then:
      - service: notify.telegram_giorgio
        data:
          message: >-
            {{ expand(integration_entities('anniversaries')) |
            selectattr('state', 'eq', 'repeat.item.giorni') | list  |
            map(attribute='name') | join(', ') }}
          title: Anniversari e compleanni tra {{ repeat.item.giorni }} giorni
        enabled: true

image

As you can see from the screenshot, the value ‘2’ is correctly populated in the title.
For example, if I manually replace ‘repeat.time.days’ in value_template with the value “2”, everything works correctly.

Remove the quotes from around 'repeat.item.giorni' in the selectattr() filters. With the quotes you are not rendering the variable but instead selecting for states with the value of the string “repeat.item.giorni”. You will need to either set the values in the for_each to strings by adding quotes or cast the variable to the string() filter.

FWIW, you can also remove the expand() functions and use select().

repeat:
  for_each:
    - giorni: "2"
    - giorni: "7"
    - giorni: "14"
    - giorni: "30"
  sequence:
    if:
      - condition: template
        value_template: >-
          {{ integration_entities('anniversaries') 
          | select('is_state', repeat.item.giorni) | list | count > 0 }}
    then:
      - service: notify.telegram_giorgio
        data:
          message: >-
            {{ integration_entities('anniversaries') | select('is_state', repeat.item.giorni) 
            | map('state_attr', 'friendly_name') | join(',') }}
          title: Anniversari e compleanni tra {{ repeat.item.giorni }} giorni  

Thank you for the help, I had already tried removing the quotes but I was missing the step of removing the selectattr() filter.

Now with this code, the first iteration is executed, but it gets stuck again on the second one.

The issue is likely in the condition of the If action… post the current script you are using there.

Just to be clear, I did not remove the selectattr(), it was switched to a select() filter with an is_state test. The combination is a relatively new addition to HA’s templating, but it is more efficient than using expand(). Your use of selectattr() was correct for the way you had constructed the original template.

I believe I found the problem. The for_each sequence needs to be added at the end, not at the beginning. I rewrote the sequence from scratch, also making a small modification to the message, and now it works. Here’s the working code in case anyone needs it:

repeat:
  sequence:
    - condition: template
      value_template: >-
        {{ integration_entities('anniversaries')  | select('is_state',
        repeat.item.giorni) | list | count > 0 }}
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ repeat.item.giorni == \"0\" }}"
          sequence:
            - service: telegram_bot.send_message
              metadata: {}
              data:
                title: Ricorrenze di oggi
                message: >-
                  {{ integration_entities('anniversaries') | select('is_state',
                  repeat.item.giorni) | map('state_attr', 'friendly_name') |
                  join(',') }}
      default:
        - service: telegram_bot.send_message
          metadata: {}
          data:
            title: Ricorrenze tra {{ repeat.item.giorni }} giorni
            message: >-
              {{ integration_entities('anniversaries') | select('is_state',
              repeat.item.giorni) | map('state_attr', 'friendly_name') |
              join(',') }}
  for_each:
    - giorni: "0"
    - giorni: "2"
    - giorni: "7"
    - giorni: "14"
    - giorni: "30"

Thank you @Didgeridrew for your help. :wink:

It’s a mapping, so the order of the keys doesn’t matter.

So maybe there was some writing error in the code, but now this latest version seems to be working fine without any issues.