Simplify Script with for loop?

Hi,

I have setup a script sequence that reads a MQTT text message, converts it to an array and switches a total of 12 power ports according to status ON or OFF:

The complete YAML script looks like this:

alias: Update PDU States via MQTT 3
sequence:
  - service: mqtt.publish
    data:
      topic: de/gudesystems/epc/00:19:32:01:79:ee/cmd
      payload: "{\"type\": \"cli\", \"cmd\": \"port all state 1 show\"}"
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - alias: MQTT Text to Array
    variables:
      pdu_states: "{{ states('text.pdu_port_states').split(',') }}"
  - if:
      - condition: template
        value_template: |
          {% if pdu_states[0][-2:] == "FF" %}
            true
          {% else %}
            false
          {% endif %}           
    then:
      - service: switch.turn_off
        target:
          entity_id: switch.pdu_port_1_switch
        data: {}
    else:
      - if:
          - condition: template
            value_template: |
              {% if pdu_states[0][-2:] == "ON" %}
                true
              {% else %}
                false
              {% endif %}
        then:
          - service: switch.turn_on
            target:
              entity_id: switch.pdu_port_1_switch
            data: {}
mode: single
icon: mdi:power-socket

Now I basically need to repeat this part 11 times for all 12 ports and with each repetition I need to count one up for “pdu_states[0 +1 ][-2:]” and “switch.pdu_port_1 +1 _switch”:

  - if:
      - condition: template
        value_template: |
          {% if pdu_states[0][-2:] == "FF" %}
            true
          {% else %}
            false
          {% endif %}           
    then:
      - service: switch.turn_off
        target:
          entity_id: switch.pdu_port_1_switch
        data: {}
    else:
      - if:
          - condition: template
            value_template: |
              {% if pdu_states[0][-2:] == "ON" %}
                true
              {% else %}
                false
              {% endif %}
        then:
          - service: switch.turn_on
            target:
              entity_id: switch.pdu_port_1_switch
            data: {}

How to do that elegantly?

It seems to work using the repeat action! :smiley:

alias: Update PDU States via MQTT Query
sequence:
  - service: mqtt.publish
    data:
      topic: de/gudesystems/epc/00:19:32:01:79:ee/cmd
      payload: "{\"type\": \"cli\", \"cmd\": \"port all state 1 show\"}"
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - alias: MQTT Text to Array
    variables:
      pdu_states: "{{ states('text.pdu_port_states').split(',') }}"
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - repeat:
      for_each:
        - switch.pdu_port_1_switch
        - switch.pdu_port_2_switch
        - switch.pdu_port_3_switch
        - switch.pdu_port_4_switch
        - switch.pdu_port_5_switch
        - switch.pdu_port_6_switch
        - switch.pdu_port_7_switch
        - switch.pdu_port_8_switch
        - switch.pdu_port_9_switch
        - switch.pdu_port_10_switch
        - switch.pdu_port_11_switch
        - switch.pdu_port_12_switch
      sequence:
        - if:
            - condition: template
              value_template: |
                {% if pdu_states[repeat.index-1][-2:] == "FF" %}
                  true
                {% else %}
                  false
                {% endif %}           
          then:
            - service: switch.turn_off
              target:
                entity_id: "{{ repeat.item }}"
              data: {}
          else:
            - if:
                - condition: template
                  value_template: |
                    {% if pdu_states[repeat.index-1][-2:] == "ON" %}
                      true
                    {% else %}
                      false
                    {% endif %}
              then:
                - service: switch.turn_on
                  target:
                    entity_id: "{{ repeat.item }}"
                  data: {}
mode: single
icon: mdi:power-socket