Try this code lua:
timeLeftToCastAgain > max time left before casting again buff in seconds
minSpellDuration > To ignore spells like Avenging Wrath (combat spell) in seconds
local timeLeftToCastAgain = 10;
local minSpellDuration = 60 * 2;
for i=1,40 do
local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId = UnitBuff("player", i);
if spellId and unitCaster == "player" then
local expireTimeLeft = expirationTime-GetTime();
if (expireTimeLeft >= 0 and duration > minSpellDuration and expireTimeLeft < timeLeftToCastAgain) then
CastSpellByName(name);
end
end
end
Tell me if don't works or if you don't understand (or if is not what you want).