Templating a long automation

Hi guys, I have the following automation. it is pretty long and the only thing changed between each action the number of the timer in the topic. can someone help me template it so it will be shorter and will support adding more timers in the future? something like a ‘for’ loop that runs from 1 to 4…

Thanks in advanced for anyone who tries to help.

Naor

- alias: "Toggle The Timers"
  id: 'gardentimerstoggle'
  initial_state: true
  trigger:
    - platform: state
      entity_id: 
        - input_boolean.raindelay
  action:
    - service: mqtt.publish
      data_template:
        topic: sonoff4chpro1/cmnd/timer1
        payload: >
          {% if is_state( 'input_boolean.raindelay' , 'on') %}
            {"Arm":0}
          {% elif is_state( 'input_boolean.raindelay' , 'off')  %}
            {"Arm":1}
          {% endif %}
    - service: mqtt.publish
      data_template:
        topic: sonoff4chpro1/cmnd/timer2
        payload: >
          {% if is_state( 'input_boolean.raindelay' , 'on') %}
            {"Arm":0}
          {% elif is_state( 'input_boolean.raindelay' , 'off')  %}
            {"Arm":1}
          {% endif %}
    - service: mqtt.publish
      data_template:
        topic: sonoff4chpro1/cmnd/timer3
        payload: >
          {% if is_state( 'input_boolean.raindelay' , 'on') %}
            {"Arm":0}
          {% elif is_state( 'input_boolean.raindelay' , 'off')  %}
            {"Arm":1}
          {% endif %}
    - service: mqtt.publish
      data_template:
        topic: sonoff4chpro1/cmnd/timer4
        payload: >
          {% if is_state( 'input_boolean.raindelay' , 'on') %}
            {"Arm":0}
          {% elif is_state( 'input_boolean.raindelay' , 'off')  %}
            {"Arm":1}
          {% endif %}

You can make a new template sensor to keep “Arm” value (0 or 1) and use that instead of the same template for every payload. This makes it shorter but you still need four service calls. I’m not sure if there is much simpler way to reduce that. You can probably use script with arguments for that.

thanks! If it stays long, I won’t change it for now…