[Terminal Response] check for multiple line regex

Hey, first of all, thank you for your work and willing to help!

I wanted to use the terminal response plug in for a multi-line terminal check. Unfortunately I missed that it could do only single line regex definitions.

I use a 3dChameleon setup (like MMU with endstop trigger to pull filament out of the hotend and trigger again to put it back in) with gcode actions.

If i change the filament color, the filament gets out of the head, the gcode requests

M118 E1 filament outside check start
M119; endstop status
M118 E1 filament outside check end

afterwards it gets back in and request the status again,

M118 E1 filament inside check start
M119; endstop status
M118 E1 filament inside check end

So, I want to ask if anyone could imagine to or how to check the following states:

The regex definition works in regex101.com (but this is useless?),

Outside regex
filament outside check start[\s\S]*?filament_extruder1: open[\s\S]*?filament outside check end
Inside regex
filament inside check start[\s\S]*?filament_extruder1: TRIGGERED[\s\S]*?filament inside check end

Terminal output which should action a pause command:

Outside:

Recv: echo:filament outside check start
Recv: ok
Send: N3101 M119*25
Recv: Reporting endstop status
Recv: x_min: open
Recv: x_max: open
Recv: y_min: open
Recv: y_max: open
Recv: z_min: open
Recv: z_max: open
Recv: z_probe_proximity_switch: open
Recv: z_probe_left_optocoupler: open
Recv: z_probe_right_optocoupler: open
Recv: filament_extruder0: open
Recv: filament_extruder1: open
Recv: ok
Send: N3102 M118 E1 filament outside check end*35
Recv: echo:filament outside check end
Recv: ok

Inside:

Recv: echo:filament inside check start
Recv: ok
Send: N3109 M119*17
Recv: Reporting endstop status
Recv: x_min: open
Recv: x_max: open
Recv: y_min: open
Recv: y_max: open
Recv: z_min: open
Recv: z_max: open
Recv: z_probe_proximity_switch: open
Recv: z_probe_left_optocoupler: open
Recv: z_probe_right_optocoupler: open
Recv: filament_extruder0: open
Recv: filament_extruder1: TRIGGERED
Recv: ok
Send: N3110 M118 E1 filament inside check end*73
Recv: echo:filament inside check end

I am sorry if i miss something, I'm not very experienced with Linux like machines.
Thank you!
Chris

You might be able to do this just changing the M118 echo commands to M118 action commands. What is the action you are trying to achieve when you get each of those echoes, or do you want to just react to the final "filament inside check end"?

1 Like

Thank you for reading :slight_smile:

I want to end in a @pause state if each condition is not met as expected.

If the filament outside check fails with filament open of the extruder 1 it would mean to pause the print.
Same for the filament inside check, it fails if the extruder 1 is triggered and the print should be paused then.

Action commands are interesting but i was not able to figure out or understand what to do or how to achieve things.- Maybe the target was missing so far..

THX!

Ah, so when you start to add logic to the process (if response to command is this) you are getting into the need for a custom plugin. Basically the plugin would monitor the received queue for the echo:filament outside check start, collect the responses following until the outside check in is received, and if triggered is seen for either of the extruders it sends the pause command to OctoPrint. It would be possible to do this in a single file plugin.

Ideally would be a multi-line regex for this purpose, it would be unbelievable strong.

Chatgpt mentioned a custom plugin (as well as a script) but I think I can't do this without response from real people.

Unfortunately I am no real coder, may you point me in the right direction, please?

Don't know why I was so afraid of a custom plugin, it seems chatgpt can do this easier than a manual to insert a script into [Terminal response]..

Here is the code and plugin from chatgpt, may you have a look (I don't want to kill my octoprint instance a second time)?
FilamentPausePlugin.zip (2.2 KB)

It's actually not that far off for something that would potentially work. it is missing the hook for processing gcode, so it wouldn't actually do anything if you were to install it. Here's a quick doctored up version of the plugin that you should be able to just copy/paste the url into plugin manager > get more > ...from URL and click install. I think it will work, but didn't pay too close attention to the if/elseif logic.

https://gist.githubusercontent.com/jneilliii/6870c51414e496762e8b4d70f813e78c/raw/f09da2a47c91ec9ca022e11cef0ff31fe58bcd06/FilamentPausePlugin.py
1 Like

Thank you very much for your help.
I was a bit testing today and was unfortunately not able to achieve a pause state through the filament triggers..

You mentioned in your first post that a gcode action M118 A1 would be a idea to do this check.- Is it enough to send a M117 filament inside check start or do i need to action M118 A1 or echo?

Still fidling around a bit, didnt found the log s about this plugin but i will do some more searching in the evening..

It wouldn't do the check part, but you could force it to do pause and resume. The docs at Action Commands — OctoPrint master documentation might explain it better than I can.

like M118 A1 pause or M118 A1 resume but I think the issue you are going to contend with is printer buffer and movement. You could use M400 at the beginning to insure that all movement commands have completed before continuing.

I'm curious what caused it to not work. You can add self._logger.info("received command blah") in the code to see what might and might not be working as expected in the if/else logic. any errors in octoprint.log at all to verify the plugin is loading, etc.

1 Like

Hey, i am not able to do these troubleshooting because it seems not loading at startup.

2025-05-18 15:17:17,030 - octoprint.plugin.core - ERROR - Error loading plugin FilamentPausePlugin
Traceback (most recent call last):
  File "/opt/octopi/oprint/lib/python3.11/site-packages/octoprint/plugin/core.py", line 1339, in _import_plugin
    module = _load_module(spec)
             ^^^^^^^^^^^^^^^^^^
  File "/opt/octopi/oprint/lib/python3.11/site-packages/octoprint/plugin/core.py", line 74, in _load_module
    spec.loader.exec_module(sys.modules[spec.name])
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/pi/.octoprint/plugins/FilamentPausePlugin.py", line 6, in <module>
    octoprint.plugin.HookPlugin
AttributeError: module 'octoprint.plugin' has no attribute 'HookPlugin'
2025-05-18 15:17:17,032 - octoprint.plugin.core - ERROR - Error loading plugin filament_pause_plugin
Traceback (most recent call last):
  File "/opt/octopi/oprint/lib/python3.11/site-packages/octoprint/plugin/core.py", line 1339, in _import_plugin
    module = _load_module(spec)
             ^^^^^^^^^^^^^^^^^^
  File "/opt/octopi/oprint/lib/python3.11/site-packages/octoprint/plugin/core.py", line 74, in _load_module
    spec.loader.exec_module(sys.modules[spec.name])
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/pi/.octoprint/plugins/filament_pause_plugin.py", line 7, in <module>
    octoprint.plugin.HookPlugin
AttributeError: module 'octoprint.plugin' has no attribute 'HookPlugin'

Here is my systeminfobundle (Pi5 with latest Octopi-Official-Package 1.1.0 Bookworm):
octoprint-systeminfo-20250518152841.zip (64.6 KB)
and the installed FilamentPausePlugin.py Code:

import octoprint.plugin

class FilamentPausePlugin(octoprint.plugin.StartupPlugin):

    def on_after_startup(self):
        self._logger.info("FilamentPause Plugin aktiviert")

    def __init__(self):
        self._filament_missing = False
        self._filament_present = False

    def process_received_line(comm, line, *args, **kwargs):
        if "filament inside check end" in line and self._filament_missing:
            self._trigger_pause("Kein Filament erkannt (inside check)!")
            self._logger.info("Kein Filament erkannt (inside check)!")
        elif "filament outside check end" in line and self._filament_present:
            self._trigger_pause("Filament wurde nicht entfernt (outside check)!")
            self._logger.info("Filament wurde nicht entfernt (outside check)!")
        elif "filament_extruder1: TRIGGERED" in line:
            self._filament_missing = True
            self._logger.info("filament state: missing")
        elif "filament_extruder1: open" in line:
            self._filament_present = True
            self._logger.info("filament state: inside")
        elif "filament inside check start" in line:
            self._filament_missing = False
            self._logger.info("filament check start: inside")
        elif "filament outside check start" in line:
            self._filament_present = False
            self._logger.info("filament check start: outside")
        return line

    def _trigger_pause(self, message):
        self._logger.info("Sende Pause und Hinweis: %s" % message)
        self._printer.pause_print()

__plugin_name__ = "FilamentPause"
__plugin_pythoncompat__ = ">=3,<4"
__plugin_implementation__ = FilamentPausePlugin()
__plugin_hooks__ = {
    "octoprint.comm.protocol.gcode.received": __plugin_implementation__.process_received_line
}

The only thing i can say, its successfully installed because i can see it in the plugin manager..
There is a fresh startup log, hopefully you could help.

Thank you for your support!

Apologies, missed a little something in that code. Try reinstalling with this url.

https://gist.githubusercontent.com/jneilliii/6870c51414e496762e8b4d70f813e78c/raw/dc3ceb77796a1153bd26dda3a17402e910307d8a/FilamentPausePlugin.py
1 Like

Thank you so much! :pray:
It works! :tada:

Glad it worked out. You probably want to change all the self._logger.info calls to self._logger.debug so it's not constantly writing to the SD card for when those gcode lines occur. Then you can switch debug logging on/off in OctoPrint's logging section of settings.

Thank you for that hint.

I drew a settingspage in the meantime and added functionality.
Now i am looking for how to get a prompt when the machine is triggered to pause but i don´t know how to do this correctly, may you point me in the right direction please?
A prompt like "Printer pause because failed inside check" or "outside check failed" is my current goal..

Thank you very much!

Here's example from one of my plugins. Server side to send the message:

Client side to receive and pop-up message:

1 Like

Thank you for all your help. I will go with your M117Popup plugin, it´s not exactly like a prompt (size matters on my touchpanel :wink: ) but its way better than investigating even more days and tinkering around with things i hardly or even not understand.

Would you like to publish the code?- I see you developed "hundreds" of plugins, wow. Great work!
filamentpause_FInal.zip (2.3 KB)