How do I set off a chain of command blocks if a players health goes to a certain health. (Java, 1.17)

Setup:

  • scoreboard objectives add Health dummy
  • Tag the entity you want to track. In this answer, the tag myEntity will be used.
  • You should also know how much health your entity has. Use /data get entity @e[tag=myEntity] Health

Command blocks:

Repeating, Unconditional:

execute as @e[tag=myEntity] store result score @s Health run data get entity @s Health

Chain, Unconditional, Always Active:

execute if score @e[tag=myEntity,tag=!health_reached,limit=1] Health matches 20..40

Chain, Conditional, Always Active:

tag @e[tag=myEntity] add health_reached

Chain, Unconditional, Always Active:

say myEntity has between 20 and 40 Health

Your command blocks should look like this: A command block chain, including repeating, chain-unconditional, and chain-conditional command blocks.

You can add more Chain, Unconditional, Always Active blocks to the end of the chain to have more things happen when myEntity reaches 20-40 Health.