<p>You can't do this entirely in start gcode templates, but you <strong>can</strong> do it by first adding a custom setting to the json file for your printer profile or the base <code>fdmprinter.def.json</code> file most profiles derive from. It would look something like (copied from my copy of <code>fdmprinter.def.json</code>'s <code>material_bed_temperature_layer_0</code> definition:</p>
<pre><code>            &quot;material_bed_temperature_preheat&quot;:
            {
                &quot;label&quot;: &quot;Build Plate Preheat Temperature&quot;,
                &quot;description&quot;: &quot;The temperature used for preheating the heated build plate.&quot;,
                &quot;unit&quot;: &quot;°C&quot;,
                &quot;type&quot;: &quot;float&quot;,
                &quot;resolve&quot;: &quot;max(extruderValues('material_bed_temperature_layer_0'))&quot;,
                &quot;default_value&quot;: 40,
                &quot;value&quot;: &quot;resolveOrValue('material_bed_temperature_layer_0 - 20')&quot;,
                &quot;minimum_value&quot;: &quot;-273.15&quot;,
                &quot;minimum_value_warning&quot;: &quot;max(build_volume_temperature, max(extruderValues('material_bed_temperature')))&quot;,
                &quot;maximum_value_warning&quot;: &quot;130&quot;,
                &quot;maximum_value&quot;: &quot;200&quot;,
                &quot;enabled&quot;: &quot;machine_heated_bed and machine_gcode_flavor != \&quot;UltiGCode\&quot;&quot;,
                &quot;settable_per_mesh&quot;: false,
                &quot;settable_per_extruder&quot;: false,
                &quot;settable_per_meshgroup&quot;: false
            },
</code></pre>
<p>Alternatively, you could set <code>enabled</code> for it to <code>false</code> so it won't show up in settings, avoid the nice description and stuff, and name it something like <code>material_bed_temperature_layer_0_minus_20</code>.</p>
<p>In either case you can then use the new setting in your start gcode.</p>

CC BY-SA 4.0
2023-03-31T16:05:21.710Z
Awesome! I had found a forum thread about using these expressions, except it was completely devoid of context, no info what file, where to put the code, some scary restrictions on default_value, and so I hoped there is a way. And there is, thank you!
SF.
CC BY-SA 4.0
2023-03-31T15:54:29.823Z
2023-03-31T15:54:29.823Z
R.. GitHub STOP HELPING ICE
<p>None of the above examples work, because the settings parser is more complex than that.</p>
<p>So you can achieve preheated temperatures by adding the following modification to the <code>fdmprinter.def.json</code> file:</p>
<pre><code>&quot;material_bed_temperature_pre&quot;:
    {
        &quot;label&quot;: &quot;Build Plate Preheat Temperature&quot;,
        &quot;description&quot;: &quot;The temperature used for preheating the heated build plate.&quot;,
        &quot;unit&quot;: &quot;°C&quot;,
        &quot;type&quot;: &quot;float&quot;,
        &quot;resolve&quot;: &quot;material_bed_temperature_pre&quot;,
        &quot;default_value&quot;: &quot;material_bed_temperature_layer_0 - 8&quot;,
        &quot;value&quot;: &quot;material_bed_temperature_layer_0 - 8&quot;,
        &quot;minimum_value&quot;: &quot;0&quot;,
        &quot;minimum_value_warning&quot;: &quot;max(build_volume_temperature, max(extruderValues('material_bed_temperature')))&quot;,
        &quot;maximum_value_warning&quot;: &quot;130&quot;,
        &quot;maximum_value&quot;: &quot;200&quot;,
        &quot;enabled&quot;: &quot;false&quot;,
        &quot;settable_per_mesh&quot;: false,
        &quot;settable_per_extruder&quot;: false,
        &quot;settable_per_meshgroup&quot;: false
    },
</code></pre>
<p>A probable reason why the previous suggestions fail is that the code completion will see the inclusion as some sort of conflict/default as it is not listed in the settings (&quot;enabled = false&quot;). When there is conflict, it attempts to resolve it based on the &quot;resolve&quot; field, equating to the layer_0 setting in an incorrect example.</p>
<p>So the value can be something else than what is listed in the &quot;value&quot; category depending on circumstances.
With this addition, the value has no other choice than to compute the preheated temperature based on layer_0 temperature.</p>
<p>For nozzle temperatures, I use fixed value which does not yet cause oozing of most materials during the small mismatch of sync period.</p>
<p>The setting in the G-code generator is as follows:</p>
<pre><code>M190 S{material_bed_temperature_pre} ;Initial bed temperature, wait
M140 S{material_bed_temperature} ; start heating the bed
M104 S180 ;Hotend initial target, no wait
G28 ;Home
G1 Z15.0 F1200 ;Move the platform down 15mm
M104 S{material_print_temperature} ;Hotend actual target, no wait
M190 S{material_bed_temperature} ;Target bed temperature, wait
M109 S{material_print_temperature} T0 ; wait for hotend
</code></pre>
<p>So first we wait for the bed to reach preheat temperatures, which takes some time.
Then, the bed temperature is set to target and the nozzle is to preheat levels, but in the background.</p>
<p>During this, we moved to the home position.</p>
<p>Finally, in sequence ultimate target for the nozzle is no wait, and then we stay until the bed temperature is final. To make sure the nozzle is at the final temperature (if it was not reached during bed heat up), we wait for it until it goes to actual printing.</p>

CC BY-SA 4.0
2023-12-02T13:25:36.923Z
2023-12-02T15:03:57.443Z
2023-12-02T15:03:57.443Z
3dprinterman
<p>Cura doesn't allow math operations in G-code scripts, if you want to do that you should use Slic3r or PrusaSlicer.</p>
<p>See e.g. <a href="https://community.ultimaker.com/topic/26539-keywords-arithmetic-in-cura/#comment-232352" rel="nofollow noreferrer">this question</a> or <a href="https://community.ultimaker.com/topic/28652-g-code-custom-formula-in-the-cura-start-code/#comment-242497" rel="nofollow noreferrer">this question</a> asked on the UltiMaker community.</p>

CC BY-SA 4.0
2023-03-31T15:47:49.303Z
This isn't entirely true. You can add new virtual settings to the json settings file which are specified as formuals in terms of other settings. I'll elaborate in an answer.
R.. GitHub STOP HELPING ICE
CC BY-SA 4.0
2023-03-31T16:51:13.847Z
@R..GitHubSTOPHELPINGICE Please do, this has been a long awaited missing feature in Cura. Although, `virtual settings to the json settings file` doesn't sound too  user-friendly as in using the arithmetic in your start G-code.
0scar
CC BY-SA 4.0
2023-03-31T04:46:41.340Z
2023-03-31T21:29:56.643Z
2023-03-31T21:29:56.643Z
0scar
<p>My printer's hot end heats up way faster than the bed. Still, it takes its sweet time, and Cura's default startup sequence: heat the bed, wait for bed to reach target temperature, heat hot-end, wait to reach hot-end temperature, proceed - takes considerably more that it should.</p>
<pre><code>M140 S{material_bed_temperature_layer_0}      ;Start heating bed
M190 S{material_bed_temperature_layer_0}      ;Wait for bed to reach temp
M104 S{material_print_temperature_layer_0}    ;Start heating extruder 
M109 S{material_print_temperature_layer_0}    ;Wait for extruder to reach temp 
</code></pre>
<p>Sure I could start heating both to target temperature simultaneously and then wait for both, but then I'll be baking unmoving filament in the nozzle for much longer than necessary. What I'd like to do is to start heating the nozzle when the bed is about 20 °C short of its target temperature - that causes them to reach their target temperatures nearly simultaneously. Something along the lines of:</p>
<pre><code>M140 S{material_bed_temperature_layer_0}      ;Start heating bed
M190 S{material_bed_temperature_layer_0 - 20} ;Wait for bed to be 20 degrees before target
M104 S{material_print_temperature_layer_0}    ;Start heating extruder 
M190 S{material_bed_temperature_layer_0}      ;Wait for bed to reach temp
M109 S{material_print_temperature_layer_0}    ;Wait for extruder to reach temp 
</code></pre>
<p>Except <code>S{material_bed_temperature_layer_0 - 20}</code> isn't recognized as a valid substitution.</p>
<p>How can I achieve the desired effect?</p>

<ultimaker-cura><g-code><heated-bed>
Math in Cura start G-code expressions?
SF.
# Math in Cura start G-code expressions?

(Asked by: username_0 on 2023-03-30)

My printer's hot end heats up way faster than the bed. Still, it takes its sweet time, and Cura's default startup sequence: heat the bed, wait for bed to reach target temperature, heat hot-end, wait to reach hot-end temperature, proceed - takes considerably more that it should.

```
M140 S{material_bed_temperature_layer_0}      ;Start heating bed
M190 S{material_bed_temperature_layer_0}      ;Wait for bed to reach temp
M104 S{material_print_temperature_layer_0}    ;Start heating extruder 
M109 S{material_print_temperature_layer_0}    ;Wait for extruder to reach temp 

```

Sure I could start heating both to target temperature simultaneously and then wait for both, but then I'll be baking unmoving filament in the nozzle for much longer than necessary. What I'd like to do is to start heating the nozzle when the bed is about 20 °C short of its target temperature - that causes them to reach their target temperatures nearly simultaneously. Something along the lines of:

```
M140 S{material_bed_temperature_layer_0}      ;Start heating bed
M190 S{material_bed_temperature_layer_0 - 20} ;Wait for bed to be 20 degrees before target
M104 S{material_print_temperature_layer_0}    ;Start heating extruder 
M190 S{material_bed_temperature_layer_0}      ;Wait for bed to reach temp
M109 S{material_print_temperature_layer_0}    ;Wait for extruder to reach temp 

```

Except `S{material_bed_temperature_layer_0 - 20}` isn't recognized as a valid substitution.

How can I achieve the desired effect?

**Tags:** ultimaker-cura, g-code, heated-bed


---

## 3 answers

### Answer by username_1 on 2023-03-31. Score: 4 | Accepted answer

You can't do this entirely in start gcode templates, but you **can** do it by first adding a custom setting to the json file for your printer profile or the base `fdmprinter.def.json` file most profiles derive from. It would look something like (copied from my copy of `fdmprinter.def.json`'s `material_bed_temperature_layer_0` definition:

```
            "material_bed_temperature_preheat":
            {
                "label": "Build Plate Preheat Temperature",
                "description": "The temperature used for preheating the heated build plate.",
                "unit": "°C",
                "type": "float",
                "resolve": "max(extruderValues('material_bed_temperature_layer_0'))",
                "default_value": 40,
                "value": "resolveOrValue('material_bed_temperature_layer_0 - 20')",
                "minimum_value": "-273.15",
                "minimum_value_warning": "max(build_volume_temperature, max(extruderValues('material_bed_temperature')))",
                "maximum_value_warning": "130",
                "maximum_value": "200",
                "enabled": "machine_heated_bed and machine_gcode_flavor != \"UltiGCode\"",
                "settable_per_mesh": false,
                "settable_per_extruder": false,
                "settable_per_meshgroup": false
            },

```

Alternatively, you could set `enabled` for it to `false` so it won't show up in settings, avoid the nice description and stuff, and name it something like `material_bed_temperature_layer_0_minus_20`.

In either case you can then use the new setting in your start gcode.

> **Comments:**
> **username_0**: Awesome! I had found a forum thread about using these expressions, except it was completely devoid of context, no info what file, where to put the code, some scary restrictions on default_value, and so I hoped there is a way. And there is, thank you!  

---

### Answer by username_2 on 2023-12-02. Score: 2

None of the above examples work, because the settings parser is more complex than that.

So you can achieve preheated temperatures by adding the following modification to the `fdmprinter.def.json` file:

```
"material_bed_temperature_pre":
    {
        "label": "Build Plate Preheat Temperature",
        "description": "The temperature used for preheating the heated build plate.",
        "unit": "°C",
        "type": "float",
        "resolve": "material_bed_temperature_pre",
        "default_value": "material_bed_temperature_layer_0 - 8",
        "value": "material_bed_temperature_layer_0 - 8",
        "minimum_value": "0",
        "minimum_value_warning": "max(build_volume_temperature, max(extruderValues('material_bed_temperature')))",
        "maximum_value_warning": "130",
        "maximum_value": "200",
        "enabled": "false",
        "settable_per_mesh": false,
        "settable_per_extruder": false,
        "settable_per_meshgroup": false
    },

```

A probable reason why the previous suggestions fail is that the code completion will see the inclusion as some sort of conflict/default as it is not listed in the settings ("enabled = false"). When there is conflict, it attempts to resolve it based on the "resolve" field, equating to the layer\_0 setting in an incorrect example.

So the value can be something else than what is listed in the "value" category depending on circumstances.
With this addition, the value has no other choice than to compute the preheated temperature based on layer\_0 temperature.

For nozzle temperatures, I use fixed value which does not yet cause oozing of most materials during the small mismatch of sync period.

The setting in the G-code generator is as follows:

```
M190 S{material_bed_temperature_pre} ;Initial bed temperature, wait
M140 S{material_bed_temperature} ; start heating the bed
M104 S180 ;Hotend initial target, no wait
G28 ;Home
G1 Z15.0 F1200 ;Move the platform down 15mm
M104 S{material_print_temperature} ;Hotend actual target, no wait
M190 S{material_bed_temperature} ;Target bed temperature, wait
M109 S{material_print_temperature} T0 ; wait for hotend

```

So first we wait for the bed to reach preheat temperatures, which takes some time.
Then, the bed temperature is set to target and the nozzle is to preheat levels, but in the background.

During this, we moved to the home position.

Finally, in sequence ultimate target for the nozzle is no wait, and then we stay until the bed temperature is final. To make sure the nozzle is at the final temperature (if it was not reached during bed heat up), we wait for it until it goes to actual printing.


---

### Answer by username_3 on 2023-03-31. Score: 1

Cura doesn't allow math operations in G-code scripts, if you want to do that you should use Slic3r or PrusaSlicer.

See e.g. [this question](https://community.ultimaker.com/topic/26539-keywords-arithmetic-in-cura/#comment-232352) or [this question](https://community.ultimaker.com/topic/28652-g-code-custom-formula-in-the-cura-start-code/#comment-242497) asked on the UltiMaker community.

> **Comments:**
> **username_1**: This isn't entirely true. You can add new virtual settings to the json settings file which are specified as formuals in terms of other settings. I'll elaborate in an answer.  
> **username_3**: @username_1 Please do, this has been a long awaited missing feature in Cura. Although, `virtual settings to the json settings file` doesn't sound too  user-friendly as in using the arithmetic in your start G-code.  

---

