Section 8A. In any action under this chapter to recover possession of any premises rented or leased for dwelling purposes, brought pursuant to a notice to quit for nonpayment of rent, or where the tenancy has been terminated without fault of the tenant or occupant, the tenant or occupant shall be entitled to raise, by defense or counterclaim, any claim against the plaintiff relating to or arising out of such property, rental, tenancy, or occupancy for breach of warranty, for a breach of any material provision of the rental agreement, or for a violation of any other law. The amounts which the tenant or occupant may claim hereunder shall include, but shall not be limited to, the difference between the agreed upon rent and the fair value of the use and occupation of the premises, and any amounts reasonably spent by the tenant or occupant pursuant to section one hundred and twenty-seven L of chapter one hundred and eleven and such other damages as may be authorized by any law having as its objective the regulation of residential premises.
Whenever any counterclaim or claim of defense under this section is based on any allegation concerning the condition of the premises or the services or equipment provided therein, the tenant or occupant shall not be entitled to relief under this section unless: (1) the owner or his agents, servants, or employees, or the person to whom the tenant or occupant customarily paid his rent knew of such conditions before the tenant or occupant was in arrears in his rent; (2) the plaintiff does not show that such conditions were caused by the tenant or occupant or any other person acting under his control; except that the defendant shall have the burden of proving that any violation appearing solely within that portion of the premises under his control and not by its nature reasonably attributable to any action or failure to act of the plaintiff was not so caused; (3) the premises are not situated in a hotel or motel, nor in a lodging house or rooming house wherein the occupant has maintained such occupancy for less than three consecutive months; and (4) the plaintiff does not show that the conditions complained of cannot be remedied without the premises being vacated; provided, however, that nothing in this clause shall be construed to deprive the tenant or occupant of relief under this section when the premises are temporarily vacated for purposes of removal or covering of paint, plaster, soil or other accessible materials containing dangerous levels of lead pursuant to section one hundred and ninety-seven of chapter one hundred and eleven.
Proof that the premises are in violation of the standard of fitness for human habitation established under the state sanitary code, the state building code, or any other ordinance, by-law, rule or regulation establishing such standards and that such conditions may endanger or materially impair the health, safety or well-being of a person occupying the premises shall create a presumption that conditions existed in the premises entitling the tenant or occupant to a counterclaim or defense under this section. Proof of written notice to the owner or his agents, servants, or employees, or to the person to whom the tenant or occupant customarily paid his rent, of an inspection of the premises, issued by the board of health, or in the city of Boston by the commissioner of housing inspection, or by any other agency having like powers of inspection relative to the condition of residential premises, shall create a presumption that on the date such notice was received, such person knew of the conditions revealed by such inspection and mentioned in such notice. A copy of an inspection report issued by any such agency, certified under the penalties of perjury by the official who inspected the premises, shall be admissible in evidence and shall be prima facie evidence of the facts stated therein.
There shall be no recovery of possession pursuant to this chapter pending final disposition of the plaintiff's action if the court finds that the requirements of the second paragraph have been met. The court after hearing the case may require the tenant or occupant claiming under this section to pay to the clerk of the court the fair value of the use and occupation of the premises less the amount awarded the tenant or occupant for any claim under this section, or to make a deposit with the clerk of such amount or such installments thereof from time to time as the court may direct, for the occupation of the premises. In determining said fair value, the court shall consider any evidence relative to the effect of any conditions claimed upon the use and occupation of residential premises. Such funds may be expended for the repair of the premises by such persons as the court after a hearing may direct, including if appropriate a receiver appointed as provided in section one hundred and twenty-seven H of chapter one hundred and eleven. When all of the conditions found by the court have been corrected, the court shall direct that the balance of funds, if any, remaining with the clerk be paid to the landlord. Any tenant or occupant intending to invoke the provisions of this section may, after commencement of an action under this chapter by the landlord, voluntarily deposit with the clerk any amount for rent or for use and occupation which may be in dispute, and such payments shall be held by the clerk subject to the provisions of this paragraph.
There shall be no recovery of possession under this chapter if the amount found by the court to be due the landlord equals or is less than the amount found to be due the tenant or occupant by reason of any counterclaim or defense under this section. If the amount found to be due the landlord exceeds the amount found to be due the tenant or occupant, there shall be no recovery of possession if the tenant or occupant, within one week after having received written notice from the court of the balance due, pays to the clerk the balance due the landlord, together with interest and costs of suit, less any credit due the tenant or occupant for funds already paid by him to the clerk under this section. In such event, no judgment shall enter until after the expiration of the time for such payment and the tenant has failed to make such payment. Any such payment received by the clerk shall be held by him subject to the provisions of the preceding paragraph.
Any provision of any rental agreement purporting to waive the provisions of this section shall be deemed to be against public policy and void. The provisions of section two A and of section eighteen of chapter one hundred and eighty-six shall apply to any tenant or occupant who invokes the provisions of this section.