找回密码
 立即注册

微信扫码登录

搜索
查看: 112|回复: 3

[流程系列] 【阿木制作】分享一个根据季节,天气、温湿度条件的智能浇花流

[复制链接]

63

主题

230

回帖

2235

积分

论坛技术达人

积分
2235
金钱
1937
HASS币
20
发表于 昨天 12:29 | 显示全部楼层 |阅读模式
本帖最后由 lambilly 于 2025-11-18 12:32 编辑

分享一个根据季度,天气、温湿度条件的智能浇花流

【浇花条件(非雨天)】

1.夏季/秋季:

第一次: 07:30 必浇
第二次: 13:00 仅在温度>33℃时浇
第三次: 18:00 仅在温度>30℃时浇

2.冬季/春季:

第一次: 08:00 必浇
第二次: 13:00 仅在湿度<50%时浇

3.浇花时长:

温度>25℃时浇水25秒,每天浇花
温度>15℃时浇水20秒,星期二、四、六、日浇花
温度>0℃时浇水15秒,星期一、三、五浇花


屏幕截图_18-11-2025_122550_home.lamnas.top.jpeg
主函数代码:
// ====================== [配置常量] ======================
const plantGuides = global.get('plantGuides');

// 温度规则配置(阈值单位:℃;延迟单位:ms)
const WATER_RULES = [
    { threshold: 25,  delay: 25000,  wateringDays: [0,1,2,3,4,5,6] },  // 温度>25℃时浇水25秒,每天浇花
    { threshold: 15,  delay: 20000,  wateringDays: [0, 2, 4, 6] },     // 温度>15℃时浇水20秒,隔天浇花(星期0、2、4、6)
    { threshold: 0,   delay: 15000,  wateringDays: [1, 3, 5] }         // 温度>0℃时浇水15秒,星期1、3、5浇花
];

// ====================== [核心逻辑] ======================
const {
    weather: weather_en,
    weather_data: {
        attributes: {
            temperature,
            humidity,
            condition_cn: weather_cn
        }
    }
} = msg || {};

// >>>>> 时间调度逻辑 <<<<<
const now = new Date();
const currentDate = now.toISOString().split('T')[0]; // 获取当前日期(YYYY-MM-DD)
const currentHour = now.getHours();        // 当前小时(0-23)
const currentMinute = now.getMinutes();    // 当前分钟(0-59)
const currentMonth = now.getMonth() + 1;  // 当前月份(1-12)
const currentWeek = now.getDay();          // 当前星期(0-6)
const WEEK_CN_MAP = {
    0: "星期日",
    1: "星期一",
    2: "星期二",
    3: "星期三",
    4: "星期四",
    5: "星期五",
    6: "星期六",
};

// >>>>> 使用统一温度规则判断浇水和延迟 <<<<<
const matchedTemp = WATER_RULES.find(rule => temperature > rule.threshold);
const isWateringDay = matchedTemp ? matchedTemp.wateringDays.includes(currentWeek) : false;
const waterDelay = matchedTemp ? matchedTemp.delay : 0;

// 判断是否处于预设浇水时段
let isWateringTime = false;
if ([4, 5, 6, 7, 8, 9, 10].includes(currentMonth)) {
    // 夏季/秋季:  
    isWateringTime = (currentHour === 7 && currentMinute === 30) ||
        (currentHour === 13 && currentMinute === 0 && temperature > 33) ||
        (currentHour === 18 && currentMinute === 0 && temperature > 30);
} else {
    // 春季/冬季:
    isWateringTime = (currentHour === 8 && currentMinute === 0) ||
        (currentHour === 16 && currentMinute === 30 && humidity < 50);
}

// >>>>> 环境条件判断 <<<<<
const isRainyDay = ['rainy', 'stormy', 'thunderstorm'].includes(weather_en) || weather_cn.includes('雨');
const isHighHumidity = humidity > 90;
const shouldWater = !isRainyDay && !isHighHumidity && isWateringDay;

// >>>>> 最终决策逻辑 <<<<<
const waterSwitch = (shouldWater && matchedTemp) ? 1 : 0;

// ====================== [输出控制] ======================
if (!isWateringTime) {
    return [null, null];
}

const outputMsg = {
    topic: msg.topic,
    delay: waterDelay,
    weather_cn: weather_cn ?? '未知',
    weather_en: weather_en ?? "未知",
    water_switch: waterSwitch,
    week_cn: WEEK_CN_MAP[currentWeek],
    temperature,
    humidity,
    ...plantGuides
};

// 使用数组索引直接返回
return waterSwitch === 1 ? [outputMsg, null] : [null, outputMsg];
流下载:
游客,如果您要查看本帖隐藏内容请回复

回复

使用道具 举报

68

主题

448

回帖

2185

积分

金牌会员

积分
2185
金钱
1669
HASS币
50
发表于 昨天 14:24 | 显示全部楼层
研究研究
回复

使用道具 举报

4

主题

143

回帖

1688

积分

金牌会员

积分
1688
金钱
1541
HASS币
0
发表于 昨天 15:03 | 显示全部楼层
看看 学习学习
回复

使用道具 举报

0

主题

282

回帖

3502

积分

论坛元老

积分
3502
金钱
3220
HASS币
0
发表于 昨天 15:10 | 显示全部楼层
这个不错,谢谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian ( 晋ICP备17001384号-1 )

GMT+8, 2025-11-19 13:51 , Processed in 0.515038 second(s), 8 queries , MemCached On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表