The rules do vary by state. California has a form that you have to sign for the hotel to keep on file. New Jersey requires that you stay at least 90 days. Some states charge no tax on a stay of 30 days or longer, others stop charging the tax after 30 days but still charge for the first 30. Some states require that you occupy the same room for the entire period continuously; others will allow a change in rooms as long as you don't check out.
It is not automatic. Since many hotels have long-term guests very rarely, they may not be aware of their state's limits. You should ask them before you check in and back it up with a copy of the state's tax law (which you can usually find online).
Usually, there are three separate taxes on a hotel room. There is the state hotel occupancy tax, the local hotel occupancy tax, and sales tax. If one of these taxes is no longer required for your stay, one or more of the others still might be collected.
Also, you may need to check on state residency laws. If you take advantage of the tax break, you might establish residency in that state - which could require that you register and insure your car in that state as well as obtain a driver's license there also. The cost of doing that, or the fines for failing to do it, might be more than what you would save.