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 https://docs.octoprint.org/en/master/plugins/index.html, and specifically https://docs.octoprint.org/en/master/plugins/hooks.html#octoprint-comm-protocol-gcode-phase

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

This is perfect, thanks very much!