Custom gcode based on time

I all,
is possible create codition based on time in custom Gcodes?
For example, I have a water heater, plugged into a smart socket, I have programmed various on and off times.
I would like to execute a switch-off command when printing starts (but only if the water heater is on) and send the switch-on command at the end of printing (but only if it should be on at that time)...
ok, this is possible via bash (i use Gcode System Commands plugin...), but is possible directly from gcode?

Gcode system commands seems to be the way I would solve this issue. Without writing a custom plugin you wouldn't be able to control anything other than the printer with gcode.