How do you read off the state of the NBT data?
Can command blocks match items on NBT data or chests on their data?
Can command blocks match items on NBT data or chests on their data?
Use an item sorter.
An item will stack with another item if they are identical, which includes NBT data. Using an item sorter you can detect this.
For anyone else thinking about the problem, there are two processes that could be used by item sorting to do processing:
1. You can use the stream of items through a pipeline (if deterministic enough) to encode a bit of strings. You can do this without enchanting, because you only need two item types.
2. You initialize an instruction decoder and have it sort the item based on type, which then triggers a command block to decode the instruction further.