Unloading a set of mixed items is much more a challenge than loading. You need to check for many things at the same time:
- use a constant combinator to define a set of items
- unload up to the point where there are as many items available locally as defined in the cc
- don't try to unload what's not in the train
- use inserter stack size to not unload 12 items when you just want to unload 1
The general idea is this:
- determine what you want locally. Define things to unload with 1 constant combinator per wagon [A]
- determine what you have locally. You can wire all chests and read their content or you can use passive provider chests(*) and read a roboport to cover the whole area [B)
- determine what you have in the train. Read train from the train station. [C)
- determine what you need to unload. Compute [D] := [A]-[B)
- determine what you CAN actually unload (the train might not carry enough of what you need to unload!). Compute the minimum of [C) and [D]. [E] := MIN([D], [C))
- if you always use the EACH operator, you have now in [E] all items you can feed one at a time to one inserter to unload from the train. Use 1 inserter per wagon.
- to feed one item at the time, use the ANY operator to pick some item. Use another combinator to determine the amount into variable [S), so you can feed the item as filter and [S) as stack size to the inserter
(*) why no other logistics chest: no storage chest, because robots will put items in there, filling all stacks with no space to unload from the train. No buffer chest, because you will not request items (instead, inserters put items into it). No active provider chest, because it's useless. No requester chest, because these don't offer their content to the logistic network.
All that subtracting (computing the minimum also involves subtracting), use EACH * -1 in an arithmetic combinator, then use the negated values where appropriate.
I implemented this, see:
viewtopic.php?p=636620#p636620
However, it does a bit more than that, because I added waste disposal, opening/closing the station according to demand and a hysteresis to not call a train unless there are more than half of the required items are missing - don't call a train just for a single repair kit that got used up. Additionally, it feeds logistic robots into the roboport for bootstrap and maintaining their amount in case of loss, so you don't need to do this manually.