Factorio's SR Latch Fixed My Flapping Dehumidifier Automation
My basement dehumidifier switched itself on and off 47 times on one damp Tuesday in March. I know the number because the smart plug it hangs off logs every state change, and I only looked because the compressor had started making a sad...

My basement dehumidifier switched itself on and off 47 times on one damp Tuesday in March. I know the number because the smart plug it hangs off logs every state change, and I only looked because the compressor had started making a sad clicking noise every few minutes. The automation behind it was the simplest one I have ever written: if humidity goes above 55 percent, turn the plug on; if it drops below 55 percent, turn it off.
That rule is in half the beginner guides I have read, and it is wrong in a way that slowly damages hardware. The fix came from a game I have sunk an embarrassing number of hours into. Factorio players met this exact problem years ago, built a tiny circuit to solve it, and gave it a name: the SR latch. Here is how it works in the game, how I copied it into Home Assistant, and what the plug log looked like afterwards.
Why a single threshold flaps
The problem is not the dehumidifier or the sensor. It is the rule. Humidity in a room never sits still. With one cut off at 55 percent, the sensor reads 55.1, the plug turns on, the machine pulls the room down to 54.9 in a few minutes, the plug turns off, moisture creeps back to 55.1, and round it goes. Each loop takes a few minutes, so over a day you get dozens of short runs.
Short runs are bad for compressors. Most dehumidifier manuals ask for at least three minutes off before restarting, because the refrigerant pressure needs time to equalise. Short cycling also wears the relay inside the smart plug. A typical consumer plug relay is rated somewhere around 50,000 to 100,000 switching cycles under load. At 47 a day that is still years, but it is years you did not need to spend, and some cheaper plugs are rated far lower.
How Factorio players solved it
In Factorio you build an automated factory, and much of it runs on electricity. A common setup has solar panels charging accumulators during the day, with steam engines burning fuel as a backup. Players wanted the steam engines to run only when the batteries were getting low, so they would not burn fuel needlessly.
The obvious approach, "run steam when accumulators are below 50 percent," has exactly my problem. The batteries hover around 50, and the steam engines flicker on and off constantly. So the community settled on a pattern: start the backup when charge drops below 20 percent, and keep it running until charge climbs above 80 percent. In between those two numbers, keep doing whatever you were already doing.
That "keep doing whatever you were already doing" part is the clever bit. It requires memory. In the game, the circuit network has decider combinators, small boxes that compare a signal against a number and output something. A single decider combinator whose output is wired back into its own input can hold a state. Players call the result an SR latch, short for set and reset, borrowed from electronics. The set condition turns the output on, the reset condition turns it off, and in the gap between them the output stays where it was.
The latch does not care what the number is right now. It cares which edge you crossed last.
Electronics engineers call the same behaviour hysteresis, and your home thermostat has done it for decades. Most thermostats have a deadband of about half a degree either side of the setpoint for exactly this reason. Smart home rule builders, oddly, often do not.
The Home Assistant version
I run Home Assistant, so I had two options. The first was to write the latch myself as two separate automations. One turns the plug on when humidity goes above 58 percent. The other turns it off when humidity drops below 50 percent. Since neither fires inside the 50 to 58 band, the plug simply stays in its last state, which is the latch.
The second option, which I switched to after a week, is the built in generic hygrostat integration. It is designed for humidifiers and dehumidifiers and does the latch for you. You give it a target, a "dry tolerance" and a "wet tolerance," and a minimum cycle duration. Mine looks like this in plain terms:
- Target humidity: 54 percent.
- Wet tolerance: 4, so it switches on at 58.
- Dry tolerance: 4, so it switches off at 50.
- Minimum cycle duration: 10 minutes, so even a weird sensor spike cannot cause a run shorter than that.
The minimum cycle setting has no clean equivalent in a basic Factorio latch, but it is worth adding. My humidity sensor occasionally reports a one second spike of 8 or 9 points when the dryer next door vents, and a 10 minute minimum stops that from triggering anything.
If you are on a different platform, look for "hysteresis," "deadband," "tolerance" or "threshold with on and off values." Some apps hide it under advanced settings. If your platform has none of those, the two automation approach works anywhere that supports simple numeric triggers.
The before and after
Over the seven days before the change, the plug switched an average of 39 times a day, with the longest single run under 14 minutes. Over the seven days after, it averaged 6 switches a day, and runs lasted between 35 minutes and two and a half hours. Average humidity barely moved, 54.6 percent before and 54.1 after. Energy use dropped by about 9 percent, which I put down to fewer startups, though I would not swear the sample is big enough to prove it.
The clicking stopped on the first day.
Where else the latch belongs
Once you see the pattern, it shows up everywhere a smart plug controls something with a motor or a heater. Bathroom extractor fans triggered by humidity. Aquarium heaters. Space heaters in a garage. Pumps for a water butt. Anything where "on above X, off below X" makes physical hardware cycle rapidly. If you are not sure whether a given device is safe on a smart plug in the first place, check the load rating first, and my colleague's guide to what a smart plug can and cannot control covers the limits well.
It is also one of the more common reasons automations seem to "misbehave." A rule that fires correctly forty times a day looks broken from the outside even though it is doing exactly what it was told. If you have one of those, why smart home automations stop working is a useful place to look for other causes too.
What I am not sure about
I picked 50 and 58 by feel. A wider band means fewer cycles but bigger swings, and for a basement storing cardboard boxes, a swing up to 58 is fine. For a room with a piano or wooden instruments, I would tighten it to something like 48 and 53 and accept more cycling. I do not have a formula for the right gap. Factorio players argue about their 20 and 80 numbers too, which makes me feel better.
Check your logs tonight
Open the history for any smart plug that runs a motor, heater or compressor off a sensor reading, and count the state changes over the last 24 hours. If it is more than about a dozen, split your single threshold into an on number and an off number at least 6 points apart, or switch to a hygrostat or thermostat helper with tolerance settings. Then check the count again after three days. If you are just getting started with sensors, the three automations worth setting up first will give you something sensible to latch.
More in Games
Games
Stealth Games Explain Where Your Cameras Stop Seeing
A homeowner sent me a clip from her doorbell camera last autumn. A man walks up her path, takes a parcel and leaves.
Games
What Online Game Offline Modes Teach Smart Home Owners
Ask a smart home owner what they fear and it is rarely a hacker.
Games
How Online Game Minimaps Mirror a Smart Home Status Screen
A game minimap is a small miracle of information design.