# Get current probe state in a macro

**URL:** <https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663>\
**Category:** General Discussion\
**Created:** [September 5, 2024, 2:10pm UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663 "2024-09-05T14:10:51Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![fox88x](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@fox88x](https://klipper.discourse.group/u/fox88x)\
**Post date:** [September 5, 2024, 2:10pm UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/1 "2024-09-05T14:10:51Z")

</div>

Hello, I use a Klicky probe, which is essentially a switch. I’m trying to implement a macro to check the presence of the probe to avoid collisions.  
Here’s what I’ve managed to do so far:

[gcode\_macro PROVA]  
gcode:  
G28  
# Controlla lo stato del probe  
QUERY\_PROBE  
G4 P1000  
QUERY\_PROBE  
G4 P1000  
{% if printer.probe.last\_query|int == 0 %}  
RESPOND MSG=“OPEN”  
{% elif printer.probe.last\_query|int == 1 %}  
RESPOND MSG=“CLOSE”  
{% else %}  
RESPOND MSG=“NON RICONOSCIUTO”  
{% endif %}

When I run the macro, `QUERY_PROBE` gives me the correct state of the probe, but with `printer.probe.last_query|int` I think I get the previous state. Even though I’ve added 2 `QUERY_PROBE` lines, the read state isn’t updated. To get the correct state, I have to run the macro twice.

---

<div class="post-metadata">

**Author:** ![EddyMI3D](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/eddymi3d/32/1981_2.png) [@EddyMI3D](https://klipper.discourse.group/u/EddyMI3D)\
**Post date:** [September 5, 2024, 2:25pm UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/2 "2024-09-05T14:25:22Z")

</div>

So this is a information for other users?

---

<div class="post-metadata">

**Author:** ![fox88x](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@fox88x](https://klipper.discourse.group/u/fox88x)\
**Post date:** [September 5, 2024, 3:09pm UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/3 "2024-09-05T15:09:25Z")

</div>

It would be a request for help; I phrased it poorly.

---

<div class="post-metadata">

**Author:** ![EddyMI3D](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/eddymi3d/32/1981_2.png) [@EddyMI3D](https://klipper.discourse.group/u/EddyMI3D)\
**Post date:** [September 5, 2024, 3:57pm UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/4 "2024-09-05T15:57:21Z")

</div>

You initially posted to the wrong sub-forum (there have been notes)

Please attach the klippy.log to your next post and information on your system.

---

<div class="post-metadata">

**Author:** ![fox88x](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@fox88x](https://klipper.discourse.group/u/fox88x)\
**Post date:** [September 5, 2024, 4:16pm UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/5 "2024-09-05T16:16:54Z")

</div>

It’s a request for help with a macro, should I have posted it?  
[klippy.log](https://klipper.discourse.group/uploads/short-url/5TxIHV8lSFjMzqql3TAkUAAJvDO.log) (5.7 MB)

---

<div class="post-metadata">

**Author:** ![EddyMI3D](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/eddymi3d/32/1981_2.png) [@EddyMI3D](https://klipper.discourse.group/u/EddyMI3D)\
**Post date:** [September 5, 2024, 4:32pm UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/6 "2024-09-05T16:32:03Z")

</div>

Because when you opened the thread, you got this:

![grafik](https://global.discourse-cdn.com/free1/uploads/klipper/original/2X/c/c39ecc5d4dd68008645baa7bfcb709bbd5a37779.png)

---

<div class="post-metadata">

**Author:** ![3dcoded](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/3dcoded/32/20297_2.png) [@3dcoded](https://klipper.discourse.group/u/3dcoded)\
**Post date:** [September 5, 2024, 5:12pm UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/7 "2024-09-05T17:12:36Z")

</div>

This is a limitation of standard macros (the status of printer objects not updating within a single run). [DynamicMacros](https://github.com/3dcoded/DynamicMacros) solves this by allowing printer objects to update mid-macro run.

---

<div class="post-metadata">

**Author:** ![fox88x](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@fox88x](https://klipper.discourse.group/u/fox88x)\
**Post date:** [September 5, 2024, 8:37pm UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/8 "2024-09-05T20:37:18Z")

</div>

"Thank you, with Dynamic Macros I was able to read the current state of the probe. Now I have the problem of passing it to other macros:

In dynamic.cfg:  
[gcode\_macro CHECK\_PROBE]  
description: Check current state of probe  
variable\_probe\_state:“error”  
gcode:  
G28  
QUERY\_PROBE  
{% if printer.probe.last\_query|int == 0 %}  
{% set PROBE\_STATE = “open”|string %}  
{% elif printer.probe.last\_query|int == 1 %}  
{% set PROBE\_STATE = “triggered”|string %}  
{% else %}  
{% set PROBE\_STATE = “error”|string %}  
{% endif %}  
#SET\_GCODE\_VARIABLE MACRO=CHECK\_PROBE variable=probe\_state value=“{PROBE\_STAE}”

SET\_GCODE\_VARIABLE MACRO=CHECK\_PROBE VARIABLE=probe\_state VALUE=‘“{PROBE\_STATE}”’

In macro.cfg  
[gcode\_macro PROVA]  
gcode:  
CHECK\_PROBE  
M118 {printer[“gcode\_macro CHECK\_PROBE”].probe\_state %}

This is just one of the attempts I’ve made, but the macro doesn’t work, can you help me?"

---

<div class="post-metadata">

**Author:** ![3dcoded](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/3dcoded/32/20297_2.png) [@3dcoded](https://klipper.discourse.group/u/3dcoded)\
**Post date:** [September 5, 2024, 9:01pm UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/9 "2024-09-05T21:01:45Z")

</div>

Dynamic Macros uses a `SET_DYNAMIC_VARIABLE` command, not the standard `SET_GCODE_VARIABLE` command. See [Variables - DynamicMacros Documentation](https://3dcoded.github.io/DynamicMacros/features/variables/)

Also, in the future, it makes reading code a lot easier on these forums if you format your code in a code block (it’s the button by the bold and italic buttons that looks like \</\>)

---

<div class="post-metadata">

**Author:** ![fox88x](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@fox88x](https://klipper.discourse.group/u/fox88x)\
**Post date:** [September 6, 2024, 2:07am UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/10 "2024-09-06T02:07:08Z")

</div>

“The result doesn’t change, I’ve tried many times but I can’t pass the `probe_state` variable from the macro in `dynamic.cfg` to the macro in `macros.cfg`.”

dynamic.cfg

```auto
[gcode_macro CHECK_PROBE]
description: Check current state of probe
variable_probe_state: 2
gcode:
  G28
  QUERY_PROBE
  {% if printer.probe.last_query|int == 0 %}
    {% set PROBE_STATE = 0 %}
  {% elif printer.probe.last_query|int == 1 %}
    {% set PROBE_STATE = 1 %}
  {% else %}
    {% set PROBE_STATE = 2 %}
  {% endif %}
  SET_DYNAMIC_VARIABLE MACRO=CHECK_PROBE VARIABLE=probe_state VALUE=1

```

macro.cfg

```auto
[gcode_macro PROVA]
gcode:
    check_probe
    RESPOND MSG={printer.save_variables.probe_state}
    RESPOND MSG={probe_state}
    MESSAGE msg={printer['gcode_macro CHECK_PROBE'].PROBE_STATE}
    {% set settings = get_macro_variables("CHECK_PROBE") %}
    RESPOND MSG="Settings: {settings}"

```

---

<div class="post-metadata">

**Author:** ![fox88x](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@fox88x](https://klipper.discourse.group/u/fox88x)\
**Post date:** [September 6, 2024, 2:11am UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/11 "2024-09-06T02:11:03Z")

</div>

I tried the update method, SAVE\_VARIABLE, etc., I don’t know what else to do. If the code doesn’t give me an error, it just passes an empty variable. Can you help me with the code?

---

<div class="post-metadata">

**Author:** ![3dcoded](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/3dcoded/32/20297_2.png) [@3dcoded](https://klipper.discourse.group/u/3dcoded)\
**Post date:** [September 6, 2024, 3:55pm UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/12 "2024-09-06T15:55:46Z")

</div>

The `get_macro_settings` feature is only available for Dynamic Macros. Can you try moving the `PROVA` macro into your `dynamic.cfg`? Also, try changing this line (in the `PROVA` macro):

```auto
MESSAGE msg={printer['gcode_macro CHECK_PROBE'].PROBE_STATE}

```

to this (`probe_state` shouldn’t be capitalized):

```auto
MESSAGE msg={printer['gcode_macro CHECK_PROBE'].probe_state}

```

---

<div class="post-metadata">

**Author:** ![fox88x](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@fox88x](https://klipper.discourse.group/u/fox88x)\
**Post date:** [September 8, 2024, 10:45am UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/13 "2024-09-08T10:45:36Z")

</div>

I need to pass the current state of the probe to start\_print; the macro ‘prova’ is just an example. Somehow, I need to transfer information between dynamic macros and classic macros. Alternatively, I can convert the start\_print macro into a dynamic one, but I expect other problems. I’ll try, thanks for the help.

---

<div class="post-metadata">

**Author:** ![3dcoded](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/3dcoded/32/20297_2.png) [@3dcoded](https://klipper.discourse.group/u/3dcoded)\
**Post date:** [September 8, 2024, 1:13pm UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/14 "2024-09-08T13:13:18Z")

</div>

You can convert your `PRINT_START` macro to be Dynamic. Almost all my macros are dynamic, including my `PRINT_START` and `PRINT_END`.

If you don’t want to convert it to dynamic, you can get the probe state in the `PRINT_START` macro with this:

```auto
{printer['gcode_macro CHECK_PROBE'].probe_state}

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/klipper/original/1X/64419bf2aac6639e6ef5cef200dcd456b0a01ac3.png) [@system](https://klipper.discourse.group/u/system)\
**Post date:** [November 7, 2024, 1:13pm UTC](https://klipper.discourse.group/t/get-current-probe-state-in-a-macro/18663/15 "2024-11-07T13:13:55Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
