Plugin to take action on certain gcode commands?

Is there a plugin that allows me to take action when certain gcode commands?

For example, when M140 (set bed temperature) and G28 (home) are executed I wish to perform some GPIO output. In this case, to turn on and off a small LED I have near the printhead so I can examine that the printhead is clean.

I'm sorry to ask such a simple question, but Reddit, Google, and forum searches are not giving me any clues!

See, and specifically

If you are looking for a ready-made plugin, you could have a look at GCODE System Commands

This is perfect, thanks very much!