Please see the pinned message for how to correctly format code. Without you doing that it’s hard to follow the code.
For triggers… it’s explained here:
When any of the automation’s triggers becomes true (trigger fires), Home Assistant will validate the conditions, if any, and call the action.