Adding States based on Health Percentages

So I'm trying to figure out how to add a 'Berserk'-like state to a specific Actor when she falls below a certain HP Threshold. I really just want to implement it like this:

if (actor.getHp / actor.getMaxHp < .51) {
actor.addState(Berserk)
} else if (actor.getHp / actor.getMaxHp < .21){
actor.removeState(Berserk)
actor.addState(Super-Berserk)
}

I'm still new to MV so I don't know what the best way to go about this would be. I'd rather not use plugins, but I'm not opposed to it if it's the best way to do it.
I would use a plugin for this. I'd recommend Yanfly's auto passive states:

http://yanfly.moe/2015/10/17/yep-13-auto-passive-states/

Your other option is to have a troop event command check this every turn in battle, but I feel like this would be cumbersome especially if you want to make changes to the hp conditions later on (I don't have a great deal of experience with battle eventing so I don't know exactly how to go about this route, it should be possible though).
MV has very, very little support for that by default, so you can either use a script, or use somewhat-cumbersome eventing.

The simplest way is probably to use Yanfly's Auto Passive States, which has very specific support for doing exactly what you're saying.

The more complex way is to use the events at the bottom of each Troop, and make pages that trigger ~once per turn, while your actor is at the designated HP amount, and add the states to them. Unfortunately, without other plugins, you'd have to copy that setup to every troop manually.

There might be other ways to handle it via events that I'm not familiar with, I suppose, but I wouldn't bet on anything that's very clean.
Your other option is to have a troop event command check this every turn in battle, but I feel like this would be cumbersome especially if you want to make changes to the hp conditions later on (I don't have a great deal of experience with battle eventing so I don't know exactly how to go about this route, it should be possible though).

That's what I was afraid I was going to have to do for the default engine. =(

Yanfly's plugin sounds like the best solution. Thanks for the help guys!
You could do this with events as well.
If it's just for battle, then you'll need 2 event pages.
One for turn start, the other for turn end.
Make sure the span is Turn.
And on 1 page the condition is 1*x Turn.
Ans on 2 page the condition is Turn End.
They'll have the same script:

var actress = $gameActors.actor(x);
if(actress.hpRate() < 0.51){
actress.addState(y);
} else if(actress.hpRate() < 0.21){
actress.removeState(y);
actress.addState(w);
}


Where x is the id of the actor, y is the id of the berserk state and w is the id of the super berserk state.

Note that this will only trigger at the start and end of a turn not in the middle of it.
@Zarsla very good idea with the condition before turn and after turn, did not thought of this yet. Thanks for that.
@Zarsla That's a pretty good way of doing it. Wouldn't it need to be copy/pasted into every troop though?
None of the solutions above appear to have any way to remove the states if the actor's HP is replenished.
@Shaz if you wanted to expand on @Zarsla 's code to remove the states when hp is regenerated it would become:

Code:
var actress = $gameActors.actor(x);
if (actress.hpRate() < 0.51){
actress.addState(y);
}
if (actress.hpRate() < 0.21){
actress.removeState(y);
actress.addState(w);
}
if (actress.hpRate() >= 0.51){
actress.removeState(y);
actress.removeState(w);
}

I would imagine Yanfly's auto passive states does this by default as well - but I could be wrong.

@Dawar You would have to copy this to every troop, or you could use Yanfly's Base Troop Events. This way you only set it up once on a "base" troop and it's copied to all the rest.

http://yanfly.moe/2015/10/18/yep-15-base-troop-events/
@Silva I've been playing around with yanfly's plugin and can confirm that it does check if a state is no longer valid. Though I only tested this for HP I assume it will also work for other conditions.

Original thread: https://forums.rpgmakerweb.com/threads/adding-states-based-on-health-percentages.105744/