Script with an array and setting brightness based on the count in repeat

Hello to those, smartwr than me…

I am rather new to Home Assistant and try to create a wake-up light. In a 5 minute span, I want to increase the light to 100%. I thought of doing it this way:

alias: sceneWakeupLight
sequence:
  - variables:
      lightInterval=:
        - 1
        - 2
        - 3
        - 4
        - 5
        - 6
        - 7
        - 8
        - 9
        - 10
        - 12
        - 14
        - 16
        - 18
        - 20
        - 22
        - 24
        - 26
        - 28
        - 30
        - 33
        - 37
        - 40
        - 45
        - 50
        - 60
        - 70
        - 80
        - 90
        - 100
  - repeat:
      count: 30
      sequence:
        - if:
            - condition: device
              type: is_on
              device_id: 391f3751471816008f541105f3bbefee
              entity_id: 1d4cf07b1d9df5946d5f68020b51b7ff
              domain: light
          then:
            - type: turn_on
              device_id: 391f3751471816008f541105f3bbefee
              entity_id: 1d4cf07b1d9df5946d5f68020b51b7ff
              domain: light
              brightness_pct: "{{lightInterval[count]|int}}"
            - delay:
                hours: 0
                minutes: 0
                seconds: 10
                milliseconds: 0
mode: single
icon: mdi:sun-clock

This code fails trying to save on
brightness_pct: “{{lightInterval[count]|int}}”
However I can’t google my way out of it.

Any help would be appreciated. (Ps, I thought when I began with HA, the devices and entities were on a name basis, now they show as id’s)

With help of chatGPT I altered the code, but it still fails:

alias: sceneOpstaanLicht
sequence:
  - variables:
      lichtInterval:
        - 1
        - 2
        - 3
        - 4
        - 5
        - 6
        - 7
        - 8
        - 9
        - 10
        - 12
        - 14
        - 16
        - 18
        - 20
        - 22
        - 24
        - 26
        - 28
        - 30
        - 33
        - 37
        - 40
        - 45
        - 50
        - 60
        - 70
        - 80
        - 90
        - 100
  - repeat:
      count: 30
      sequence:
        - if:
            - condition: device
              type: is_on
              device_id: 391f3751471816008f541105f3bbefee
              entity_id: 1d4cf07b1d9df5946d5f68020b51b7ff
              domain: light
          then:
            - type: turn_on
              device_id: 391f3751471816008f541105f3bbefee
              entity_id: 1d4cf07b1d9df5946d5f68020b51b7ff
              domain: light
              brightness_pct: "{{lightInterval[loop.index0]|int}}"
            - delay:
                seconds: 10
mode: single
icon: mdi:sun-clock

Forget chatGPT. Its only “intelligence” is stolen from those who believe it has any :wink:


  - variables:
      lightInterval=:

Why the “=”? remove


              brightness_pct: "{{lightInterval[count]|int}}"

The repeat index variable is repeat.index, not count

I suggest you try using the Light integration’s transition option.

If your light is unable to support this feature natively, then I suggest you use this script:

1 Like

You, however, respond to the original version, not the one corrected by ChatGPT. You indeed give the same suggestions.

The error remains

Because the chatGPT one is nonsense :wink:

I told you the obvious errors. I’m not planning to debug that automation on your behalf. Likely, you need lightInterval[repeat.index-1], here.

Your automation uses a Device Action which doesn’t support templates.

Change it to a light.turn_on service call which supports templates.

Regarding the template, isn’t the referenced variable’s name lichtInterval as opposed to lightInterval?

In addition, as pointed out by koying, the template is using the wrong index variable. repeat.index is for a script’s repeat whereas loop.index0 is for a Jinja template’s for-loop.

I currently have this code that fails, where I incorperated the answers + changed the type to a float to be sure.


Please note that lichtInterval is the name of the variable I translated it here earlier.

When trying your suggestion to make it light.turn_on, I get this result.

You overlooked to incorporate the one where I explained that you should use a Service Call because a Device Action doesn’t support templates.

          then:
            - service: light.turn_on
              target:
                entity_id: 1d4cf07b1d9df5946d5f68020b51b7ff
              data:
                brightness_pct: "{{lightInterval[repeat.index-1]|int}}"

NOTE

A Device Action tends to represent an entity_id using a long alphanumeric string (1d4cf07b1d9df5946d5f68020b51b7ff). I used the same string in the example above but, for improved legibility, you may wish to change the long string to the human-readable version of your light’s entity_id. My guess it may look something like light.opstaan

In addition, repeat.index starts counting from 1 whereas the list in lichtInterval starts with 0. Therefore you should subtract 1 from repeat.index.

Thank you for getting me on the right path. It is still a big change for me from Domoticz to HA. What I learned today will help me to make more complicated scripts. So again, thanks, also to all others that helped me

P.s. it is the UI that names the entities by ID nowadays. But yes, I will rename it in the script. Thats a good idea.

When you create a Device Action.

It uses the human-readable version of entity_id when you create a Service Call.