Help: Skill that heals caster for each enemy affected by state

I'm looking to create a skill that heals the caster based on the number of enemies/allies affected by a state.

Context: the party is fighting a ghost that uses attacks that cause fear effects. When it's low HP it uses "feast on fear" which heals it Xhp for each character in the battle suffering from a fear effect.

I have access to all the Yanfly plugins, but I'm unsure of how to proceed creating or implementing a skill/state relationship something like this.

Triggering the use of the skill is straight forward enough I think. I'd use Yanfly's Battle AI plugin and Buff States Core at the very least.

Does anyone have any suggestions?
You can do this in the damage formula box:

b.isStateAffected(id of fear state) ? healing amount : 0;
You can do this in the damage formula box:

b.isStateAffected(id of fear state) ? healing amount : 0;
Thank you for your swift response!

So I'd use that formula and make the skill target all enemies, that makes sense I think. I'll need to test it when I get back home.

Just to expand on the idea what about yanflies state categories? Any idea how I might implement this if I wanted the skill to heal the caster based on number of state categories applied instead of a specific state?
That will make the ghost heal a set amount if the target of its attack (?) is affected by fear.

To make a healing skill that the ghost uses on itself that heals per party member afflicted, try this for the damage formula:
Code:
var heal=0; for (var i=0; i<$gameParty.aliveMembers().length; i++) {if ($gameParty.aliveMembers()[i].isStateAffected(X)) heal+=Y;}; heal;

Replace X with the id of your fear state and Y with how much you want each fearful person to contribute to the healing.
Doh, yeah, had a brain fart there where I forgot that if you make a skill affect all enemies you can't set it to do HP healing. 😛

Also yeah, there's a javascript call to get the number of states a battler has that are a specific category.

Original thread: https://forums.rpgmakerweb.com/threads/help-skill-that-heals-caster-for-each-enemy-affected-by-state.134825/