Script repeat for each problem

Hi, maybe someone can help me with this script:

repeat:
  for_each:
    - raum: rollo_automation_buero
      rollo: 1493491a1ba0c1dd478f7a5d2b7fba99
  sequence:
    - if:
        - condition: state
          state: 'on'
          entity_id: "input_boolean.rollo_automation_buero"
      then:
        - service: homematicip_local.set_device_value
          data:
            channel: 7
            parameter: COMBINED_PARAMETER
            value: WPTCLS=1,WPTCL=2
            device_id: '{{repeat.item.rollo}}'
      else:
        - service: homematicip_local.set_device_value
          data:
            device_id: '{{repeat.item.rollo}}'
            channel: 7
            parameter: COMBINED_PARAMETER
            value: WPTCLS=1,WPTCL=0

This version works perfectly
, however when I try to use a repeat.item for the entity I’d in the if condition I even can’t save the script.


repeat:
  for_each:
    - raum: rollo_automation_buero
      rollo: 1493491a1ba0c1dd478f7a5d2b7fba99
  sequence:
    - if:
        - condition: state
          state: 'on'
          entity_id: "input_boolean.{{repeat.item.raum}}"
      then:
        - service: homematicip_local.set_device_value
          data:
            channel: 7
            parameter: COMBINED_PARAMETER
            value: WPTCLS=1,WPTCL=2
            device_id: '{{repeat.item.rollo}}'
      else:
        - service: homematicip_local.set_device_value
          data:
            device_id: '{{repeat.item.rollo}}'
            channel: 7
            parameter: COMBINED_PARAMETER
            value: WPTCLS=1,WPTCL=0

Anyone an idea?

A State Condition’s entity_id option doesn’t support a template.

Use a Template Condition.

        - condition: template
          value_template: "{{ is_state('input_boolean.' ~ repeat.item.raum, 'on') }}"
1 Like

Thanks a lot, that did. the trick.
(Although I don’t understand it, looks like I still have to learn a lot, especially about templates)

Which part?

We’ll, „just“ the whole logic behind templating :wink:
I am still quite a newbie and not a code person at all, so I try to do copy/paste as good as it gets, and by that step up my knowledge.

Basically, its purpose is to compute a value.

Some options support templates whereas others do not. In this particular situation, the State Condition’s entity_id option does not support a template. The solution is to use another kind of condition that does support templates.

Tip

If the documentation for a trigger or condition doesn’t include any example showing the use of templates in its options then, as a general rule, those options don’t support templates. For example, the documentation for State Condition contains no examples of entity_id with templates.

Another issue I have with this script is, when I use for the repeat part „Rollo“ the device Name as I see it under devices (Rollo Buero) instead of the number, the whole thing doesn’t work. Otherwise when I use the gui for a similar script where I can use the gui and put the Rollo Buero Name in as device the script accepts it,

but when I see the same part as yamml , it shows me the number (which is how I found out about the number)


condition: device
device_id: 1493491a1ba0c1dd478f7a5d2b7fba99
domain: cover
entity_id: cover.rollo_buero_2
type: is_open

Maybe that an issue with the custom_Homematic integration.

Thanks for explaining, that was helpful. Well, step by step I am getting better

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

May be you can help me with another issue.
I made another script with:`alias: Heizung Urlaubsschaltung
sequence:

  • choose:
    • conditions: []
      sequence:
      • repeat:
        for_each:
        - heizung: 47fe914dfbb3c5ce0899ef4864677a95
        hk: heizkorper_wohnzimmer_2
        - heizung: e52455811c87c638fca3b7cf342b0a23
        hk: heizkorper_windfang_2
        - heizung: 5f8abeb6a0bd7eeb345ee172e3b1c839
        hk: heizkorper_treppenhaus_2
        - heizung: 180102e7bd0e7e7e08d4f3653655fd86
        hk: heizkorper_schlafzimmer
        - heizung: 7a07f1fc97acdbd1754dd6fb5285322b
        hk: heizkorper_kinderbad
        - heizung: a05ce0614fd4c975409af158f833a3ac
        hk: heizkorper_gaestebad
        - heizung: 2a1cb9f5fa463738bc368c2b5f666a7a
        hk: heizkorper_badoben
        sequence:
        - device_id: ‘{{repeat.item.heizung}}’
        domain: climate
        entity_id: climate.{{repeat.item.hk}}
        type: set_preset_mode
        preset_mode: Profile 1
        default: []
        mode: single
        `
        Which gives me an error

Message malformed: Entity ID climate.{{repeat.item.hk}} is an invalid entity ID for dictionary value @ data[‘entity_id’]

Although in the documentation for repeat for each in scripts there is an example with


  sequence:
    - service: light.turn_off
      target:
        entity_id: "light.{{ repeat.item }}"

How can I solve this?

The main problem is that your script uses a Device Action which doesn’t support the use of templates.

ok, so I again have to find a different way to set the preset value ;-(
is there a specific reason that device action doesn’t support templates or ‚just not implemented‘ (so I could suggest a feature request)?
actually as long as I use the {{repeat.item}} for the dice_id only there is no problem only when I try it for the entity_id.

Use a service call.

climate.set_preset_mode

Again, thanks. It’s confusing that there are different ways to get to the same result.
The device action I picked is specifically implemented in the custom_Homematic integration, the device is integrated with this integration
Well, with this slightly different service call, it does work