I think all you need is M99
Here you have a macro using variables, if someone doesn't want variables (to use with older firmware) just replace the values.
To download: Cool_Bed.g
;[Macro Cool_Bed.g] ; Needs RRF3.3 or later because uses variables
; Change these variables to suit your printer
var BedFan = 9 ; Fan number
var ThresholdTemp = 50 ; ºC
var CheckEvery = 1 ; Seconds
if heat.heaters[0].current > {var.ThresholdTemp}
M108 ; Cancel Heating (just in case), Breaks out of an M109 or M190
M140 S0 ; Turn off bed heater (*)
M106 P{var.BedFan} S1.0 ; Turn on BedFan
while true
G4 S{var.CheckEvery} ; Wait 1 sec, to check temperature every second
if heat.heaters[0].current <= {var.ThresholdTemp}
M106 P{var.BedFan} S0.0 ; Turn off BedFan
M99 ; Return from Macro/Subprogram
;END if
;END while
;END if
"*" in line 11, is there anything analogous to M568 A0 for tools but for the bed??