Variables for [save_z_home]

You need to use:

    {% set bed_x_max = printer.configfile.settings.stepper_x.position_max %}

[safe_z_home] is not the right place for such. Use [homing_override] for such.