# WhenEvent when using NDSolve doesn't work properly

sol = NDSolve[{x''[t] == -x[t]*t + a[t]*x[t], x[0] == 1, x'[0] == 0,
WhenEvent[x[t] == 0, Tb[t] -> t],
WhenEvent[t > Tb[t] + 0.5, a[t] -> 1],
WhenEvent[t <= Tb[t] + 0.5, a[t] -> 0], Tb[0] == 0, a[0] == 0}, {x,
Tb, a}, {t, 0, 10}, DiscreteVariables -> {Tb, a}]


I'm trying to solve a differential equation with some discrete components (Tb[t],a[t]). Tb[t] is supposed to be a stair function that records the times at which x[t]=0. Tb[t] is working as intended:

a[t] however, is not working as intended. a[t] was supposed to shift between 0 and 1 whenever (t-(Tb[t]+0.5)) is equal to zero, but as we can see per the pictures below, a[t] only does the first shift correctly, and then stagnates at 1 when it should be shifting back and forth.

How can I make this work correctly.

Something like this? I changed the changing of a[t] from working with two events to one depending on a single event.

sol =
NDSolve[
{
x''[t] == -x[t]*t + a[t]*x[t], x[0] == 1, x'[0] == 0,
WhenEvent[x[t] == 0, Tb[t] -> t],
WhenEvent[t - Tb[t] - 0.5 == 0, a[t] -> 1 - a[t]],
Tb[0] == 0,
a[0] == 0
}, {x, Tb, a}, {t, 0, 10}, DiscreteVariables -> {Tb, a}]

Plot[sol[[1, 3, 2]][t], {t, 0, 10}]