For each and data templating

Hi all,

I am already busy for days to get the following working. Googled and looked around everywhere, but I wasn’t able to fix the following. What I try to accomplish in short:

  • I try to create a sort of Zigbee error correction script (when a light doesnt respond. I tried everything, trust me).

  • Therefor I have created a light group (light.), a light template (light.tl) and finally my real Zigbee lights (light.zb). In my front end and scenes, I only use the “light” group, where my light.tl and light.zb bulbs are member of

  • This way, I have a state to compare (light.zb) and to correct with a script

  • I have this working, but now I need to apply to 100+ lights

  • I have tried to fix this with “ForEach” but this doesnt seem to work with data templates

Short example what I try to fix:

repeat:
  for_each:
    - huis_badkamer_spot_1
    - huis_badkamer_spot_2
  sequence:
    - service: light.turn_on
      data_template:
        brightness: "{{state_attr('light.tl_' + repeat.item, 'brightness')|int}}"
      target:
        entity_id: light.zb_{{ repeat.item }}

The error I get is:

“Error: Error rendering data template: ValueError: Template error: int got invalid input ‘None’ when rendering template ‘{{state_attr(‘light.tl_’ + repeat.item, ‘brightness’)|int}}’ but no default was specified”

I also tried to put repeat.item in brackets, in quotes etcetera, but nothing seems to be working

The goal here is to read the status from “light.tl_huis_badkamer_spot_1”, and apply this to “light.zb_huis_badkamer_spot_1”, and repeat the same for each light.

Is there any way to fix this? Or am I doomed by creating the same script over 100 times over for each light?

EDIT:
I was already very close with this, the code just seems to be working after all :slight_smile: The “mistake” was due the fact that one of the two templates light didn’t have a Brightness level set yet…

lights do not have the brightness property when they are off. So this will error if that’s the case.

Thanks, this is fixed now. Created a condition which first checks if the light is on.

Topic can be closed.