You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node-RED中DS18B20+树莓派控制空调的逻辑优化求助

Fixing Your DS18B20 AC Control Issues on Raspberry Pi (Node-RED)

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 acState variable 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 lastOffTime variable 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:

  1. Make sure your AC control node accepts 1 as "turn on" and 0 as "turn off" (adjust the payload values if your setup uses different signals).
  2. Add a Debug node after the Function node to monitor msg.payload and the context variables (ac_state, last_off_time)—this helps you verify the logic is working.
  3. If you need to reset the state (e.g., after a reboot), you can initialize ac_state and last_off_time in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:38:17