You can filter through the NPCs by doing something like this:
NPC npcname = npcs.closest(n -> n.getName().equals("npcname") && fightingArea.contains(n) && !n.isHitBarVisible() && n.getHealthPercent() > 0 && n.getInteracting() == null)
I'm pretty sure that would give the NPC which isnt currently under attack or interacting with anyone so you can do what you wish with them after.