# Macro Creation Tutorial

**URL:** <https://klipper.discourse.group/t/macro-creation-tutorial/30>\
**Category:** Macros\
**Created:** [March 22, 2021, 3:00pm UTC](https://klipper.discourse.group/t/macro-creation-tutorial/30 "2021-03-22T15:00:59Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![mental](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/mental/32/11_2.png) [@mental](https://klipper.discourse.group/u/mental)\
**Post date:** [April 16, 2021, 4:58pm UTC](https://klipper.discourse.group/t/macro-creation-tutorial/30/5 "2021-04-16T16:58:11Z")

</div>

**Part 4: Default Parameters**

One drawback to supplying a parameter arbitrarily in a macro is that it can  
be referenced without a value. This could result in undesirable behavior or  
and error at runtime if it is not handled. One way to check to see if a  
parameter exits is simply to evaluate it.

```auto
	{% if PA %}

```

```auto
	{% if params.PA %}

```

Gcode parameters are accessed in macros through either directly referencing  
the parameter name or by using the `params` collection. There are some  
nuances to using the `params` collection in that, it will only have values that  
are passed in through the gcode command. Whereas directly referencing  
the parameter name can allow us to get a default value for ‘default\_parameter\_PA’

Jinja affords us the use of certain filters for things like rounding and typing (more  
on that later). There is also a `default()` filter which can be used instead of  
declaring a `default_parameter_PA`.

```auto
	{ params.PA|default(.06) }

```

So if we pass the a parameter through for PA and we want to set pressure  
advance in our start print macro, we can check for the parameter and then  
call the `SET_PRESSURE_ADVANCE` command.

```auto
	SET_PRESSURE_ADVANCE ADVANCE={ params.PA|default(.06) }

```

Alternatively you can give it a default value using the `default_parameter`  
config option for the gcode macro.

```auto
	[gcode_macro start_print]
	default_parameter_PA: 0.06
	gcode:

```

For this example, we are adding a `Pressure Advance` setting to our  
start print macro so that the PA can be set based on a particular filament

```auto
	[gcode_macro start_print]
	default_parameter_PA: 0.06
	gcode:
		M109 S{params.TOOL_TEMP} # Heat the tool to temperature and wait
		
		{% if printer.homed_axes != 'XYZ' %}
			G28 #Home All Axes
		{% endif %}
	
		SET_PRESSURE_ADVANCE ADVANCE={PA}
	
		G92 E0 # Reset Extruder
		G1 Z2.0 F3000 # Move Z Axis to travel height
		G1 X0.1 Y20 Z0.2 F5000.0 # Move to start position
		G1 X0.1 Y200.0 Z0.2 F1500.0 E15 # Draw the first line
		G1 X0.4 Y200.0 Z0.2 F5000.0 # Move to side a little
		G1 X0.4 Y20 Z0.3 F1500.0 E30 # Draw the second line
		G92 E0 # Reset Extruder
		G1 Z2.0 F3000 # Move Z Axis up to travel height

```

---

_[View the full topic](https://klipper.discourse.group/t/macro-creation-tutorial/30)._
