Node-RED中DS18B20+树莓派控制空调的逻辑优化求助
Hey there! Let's work through those two annoying issues with your AC control setup. Both problems boil down to missing state tracking and delay logic—here's exactly how to fix them in Node-RED:
Problem 1: AC Turns Off Too Early (Before 20℃)
The root issue here is that you're switching based on absolute temperature thresholds without hysteresis (state memory). Right now, if the temperature bounces between 24-26℃, your AC would toggle on/off constantly. We need to keep the AC running until it drops below 20℃ once it's turned on, and keep it off until it climbs above 25℃ once it's off.
Problem 2: No 10-Minute Delay After Turning Off AC
We need to track when the AC was last turned off, and block any "turn on" commands until 10 minutes have passed—even if the temperature spikes above 25℃ immediately after shutting down.
Step-by-Step Solution Using a Function Node
Here's a complete Function node script that handles both issues. Just drop this into a Function node connected after your DS18B20 temperature input:
// Get current temperature from DS18B20 const currentTemp = msg.payload; // Get existing state from Node-RED flow context let acState = flow.get('ac_state') || 0; // 0 = Off, 1 = On let lastOffTime = flow.get('last_off_time') || 0; // Timestamp of last AC shutdown const delayMs = 10 * 60 * 1000; // 10 minutes in milliseconds const turnOnThreshold = 25; const turnOffThreshold = 20; // Logic to handle AC state if (acState === 1) { // AC is currently ON: only turn off if temp drops to 20℃ or below if (currentTemp <= turnOffThreshold) { acState = 0; lastOffTime = Date.now(); // Record shutdown time msg.payload = 0; // Send "turn off" command } else { // Keep AC running, don't send any command return null; } } else { // AC is currently OFF: check delay and temperature const timeSinceOff = Date.now() - lastOffTime; if (timeSinceOff >= delayMs && currentTemp >= turnOnThreshold) { acState = 1; msg.payload = 1; // Send "turn on" command } else { // Either delay not passed, or temp too low: do nothing return null; } } // Update flow context with new state and time flow.set('ac_state', acState); flow.set('last_off_time', lastOffTime); return msg;
How This Works:
- Hysteresis Handling: The
acStatevariable remembers if the AC is currently on or off. When it's on, it only triggers a shutoff at ≤20℃; when off, it only triggers a startup at ≥25℃. - Delay Protection: The
lastOffTimevariable stores the timestamp when the AC was last turned off. Before allowing a restart, we check if 10 minutes (600,000ms) have passed. - No Unnecessary Commands: If the state doesn't need to change, the script returns
null, so no message is sent to your AC control node—prevents unnecessary toggling.
Setup Tips:
- Make sure your AC control node accepts
1as "turn on" and0as "turn off" (adjust the payload values if your setup uses different signals). - Add a Debug node after the Function node to monitor
msg.payloadand the context variables (ac_state,last_off_time)—this helps you verify the logic is working. - If you need to reset the state (e.g., after a reboot), you can initialize
ac_stateandlast_off_timein the Node-RED settings, or use an Inject node to set them manually on startup.
That should resolve both of your issues! Let me know if you need to tweak any part for your specific AC control setup.
内容的提问来源于stack exchange,提问作者Herman Kruger

