(1) In an action for possession based upon nonpayment of the rent or in an action for rent where the tenant is in possession, the tenant may counterclaim for any amount which he or she may recover under the rental agreement or the Uniform Residential Landlord and Tenant Act. In that event, the court from time to time may order the tenant to pay into court all or part of the rent accrued and thereafter accruing and shall determine the amount due to each party. The party to whom a net amount is owed shall be paid first from the money paid into court, and the balance by the other party. If no rent remains due after application of this section, judgment shall be entered for the tenant in the action for possession. If the defense or counterclaim by the tenant is without merit and is not raised in good faith, the landlord may recover reasonable attorney's fees.
(2) In an action for rent where the tenant is not in possession, the tenant may counterclaim as provided in subsection (1) of this section but the tenant is not required to pay any rent into court.