The code I suggested is based on using the sensor you have as a filament monitor.
If that is not the case and you simply have it connected to an input then you will need to replace any reference to sensors.filmentMonitors[var.whichSensor]status with something like sensors.gpIn[var.whichSensor].value ,where whichSensor is your gpIn value for the switch
You would also not need anything referring to M591