Automatic spell cast on low health? (issues with yanfly's counter control)

Hi everyone.
I've been looking for a way to create a skill that's automatically cast on low health (25% for example). I've been trying with yanfly's counter control to no avail so far.
The mechanics are as follows: I created a piece of armor with a 100% ex counterattack effect that has a counter skill notetag set to it. The skill itself is a Protect spell with no cost that adds the 'Protect state'. The skill also turns on a switch so that it can only be triggered once per battle (the switch is turned off at the end of each battle). I'm following the notetags included in the plugin notes but I cannot get it to trigger the counterattack when the ally's HP falls below 25% (it only triggers a normal counterattack). The notetags are as follows:
Spoiler: notetags code
<Counter Name: Protección>
<Counter Condition>
switch 120 off
defender hp <= defender.mhp * 0.25
</Counter Condition>
<Follow action>
change switch 120: on
</Follow action>
I wasn't really sure if the "defender" in the notetags is the one who is countering or a possible target for a counter, so I tried with attacker hp but it didn't work either. I've read somewhere that some notetags for this plugin were not working properly so I purchased the latest plugin version (1.10), but it's still not working. The problem lies in the conditions, if I get rid of them the counter works, just not under the conditions that I want.
I'd like this to remain as a counter as much as possible since it allows an instant cast right after the battler's health falls below this critical threshold, if that makes any sense.
Hi you,

Have you check the yanfly Buffs & states Core plugin ?
There is a notetag <Custom Respond Effect> that allow you to run a piece of code when the battler affected by a state gets hit.
Maybe you can have better results using it.

Best,

Lay.
I'll see what I can do but the battler is not supposed to be affected by a state when triggering the counter.
The mechanics you explained doesn't really correspond to what a counter is.

make your actor that will receive the damages affected by a passive state. This can be done using the yanfy Passive State plugin. If an equiped armor is required, no panic, you can associate passive states to armor and weapon too.

in custom respond, check the current actor HP.
if its below 25%, then do what the shield is supposed to do.
If you want to create a specific state for this shield, you can still add this state to the user in the respond.
Ok I managed to do it with both methods, each has its pros and cons. I know this is not a proper counter since it doesn't hit back, but I was interested in the triggering conditions (multi-hit, magical hit, element hit, etc) for creating other similar pieces of armor.
- With counter control: for some reason the attacker/defender hp condition was never met (probably I made some mistake), so instead I used Eval as follows and it worked flawlessly:
Spoiler
<Counter Condition>
SWITCH 120 OFF
Eval: defender.hpRate() <= 0.90
</Counter Condition>
I used hp <= 90% just to test it faster. The downside is that whenever this counter isn't triggered, a normal counter is triggered (a regular attack). I think I could tune it up further so that normal counters aren't triggered with this piece of armor (perhaps with a condition and a variable?). It wouldn't be that difficult since there's only one character that can equip this piece of armor.

- With passive states: I'm already using passive states so I only needed to create a new piece of armor and the state itself. It was really straightforward. The state triggers as soon as the conditions are met just like a counter, and not in the next turn, which is awesome. The only con is that it's not "cast" as a spell, the state just appears (that's of course, just a cosmetic issue). I'm aware that you can make it appear with an animation if you use custom conditions (I was using them for limit breaks), so I'll be experimenting with that (I'm quite a noob at coding).

All in all, I think I'll be using the passive states in the end.
Thanks a lot!
great !
If you have troubles in coding what you want, you can PM me. I will do my best.

enjoy your game making,

Lay.

Original thread: https://forums.rpgmakerweb.com/threads/automatic-spell-cast-on-low-health-issues-with-yanflys-counter-control.121080/