openwrt风扇温度控制,通过米家usb开关控制

openwrt风扇温度控制,通过米家usb开关控制

检测温度并实时测试

#!/bin/bash

# ==========================================
# Home Assistant API 控制脚本
# ==========================================

# 配置参数
HA_URL="http://127.0.0.1:8123"
ENTITY_ID="switch.xxx"
TOKEN="xxx"
ROUTER_TEMP_KEY="xxx"

# 检查是否安装了 jq (用于解析 JSON 返回值)
if ! command -v jq &> /dev/null; then
    echo "错误: 未找到 jq 提取工具。请使用 'sudo apt install jq' 安装。"
    exit 1
fi

##########################################################################################
function urlencode() {
    if ! command -v curl >/dev/null 2>&1; then echo -E "$1"; return; fi
    local str="$1"
    curl -Gso /dev/null --data-urlencode "var=$str" "http://localhost/?" -w "%{url_effective}\n" | sed 's/^.*var=//'
}
function urldecode() { local i="${*//+/ }"; echo -e "${i//%/\\x}"; }

function writeCache(){
  CACHE_KEY="$1" ; CMD_URL="http://example.com/set"
  URL_DATA="k=$(urlencode "$CACHE_KEY")&v=$(urlencode "$2")"
  curl -L -m 5 -s -o /dev/null -d "$URL_DATA" "$CMD_URL"
}
function readCache(){
  CACHE_KEY="$1" ; CMD_URL="http://example.com/get"
  REQ_URL="$CMD_URL?key=$(urlencode "$CACHE_KEY")"
  curl -L --connect-timeout 3 -m 5 -s "$REQ_URL"
}
##########################################################################################

# 获取开关状态
get_status() {
    echo "正在获取状态..."
    local response=$(curl -L --connect-timeout 3 -m 5 -s -X GET \
        -H "Authorization: Bearer ${TOKEN}" \
        -H "Content-Type: application/json" \
        "${HA_URL}/api/states/${ENTITY_ID}")
    
    # 检查 API 是否返回了包含 state 的有效 JSON
    local state=$(echo "${response}" | jq -r '.state')
    
    if [ "$state" == "null" ] || [ -z "$state" ]; then
        echo "获取状态失败,API 返回内容: ${response}"
    else
        echo "当前开关状态: ${state}"
    fi
}
get_status_api() {
    local response=$(curl -L --connect-timeout 3 -m 5 -s -X GET \
        -H "Authorization: Bearer ${TOKEN}" \
        -H "Content-Type: application/json" \
        "${HA_URL}/api/states/${ENTITY_ID}")
    
    # 检查 API 是否返回了包含 state 的有效 JSON
    local state=$(echo "${response}" | jq -r '.state')
    
    if   [ "$state"x == "on"x ]; then
        echo -E "1"
    elif [ "$state"x == "off"x ]; then
        echo -E "0"
    else
        echo -E "-1"
    fi
}

# 打开开关
turn_on() {
    echo "正在发送打开指令..."
    local response=$(curl -L --connect-timeout 3 -m 5 -s -X POST \
        -H "Authorization: Bearer ${TOKEN}" \
        -H "Content-Type: application/json" \
        -d "{\"entity_id\": \"${ENTITY_ID}\"}" \
        "${HA_URL}/api/services/switch/turn_on")
    
    # Home Assistant 调用服务成功通常返回被改变的实体状态 JSON 数组
    echo "指令已发送。"
    # 可选:操作后立即查询一次最新状态
    # get_status
}

# 关闭开关
turn_off() {
    echo "正在发送关闭指令..."
    local response=$(curl -L --connect-timeout 3 -m 5 -s -X POST \
        -H "Authorization: Bearer ${TOKEN}" \
        -H "Content-Type: application/json" \
        -d "{\"entity_id\": \"${ENTITY_ID}\"}" \
        "${HA_URL}/api/services/switch/turn_off")
    
    echo "指令已发送。"
    # 可选:操作后立即查询一次最新状态
    # get_status
}


while true;do
    cur_temp=`readCache "$ROUTER_TEMP_KEY"`
    echo "$cur_temp"

    # 1 or 0 , -1 unknow
    status_code=`get_status_api`
    echo $status_code

    # 如果温度获取失败,跳过
    if [ "$cur_temp"x == ""x ]; then
        if [ "$status_code" != "1" ]; then
            turn_on
        fi
        continue
    fi
    if [[ ! "$cur_temp" =~ ^[0-9]+$ ]]; then
        if [ "$status_code" != "1" ]; then
            turn_on
        fi
        continue
    fi


    # 如果温度大于 50000 (50°C)
    if [ "$cur_temp" -gt 57000 ]; then
        # 只有在当前状态不是开启(1)时,才调用开启接口,避免频繁重复调用
        if [ "$status_code" != "1" ]; then
            echo "温度高于 57度,正在开启开关..."
            turn_on
        fi
    # 如果温度小于 45000 (45°C)
    elif [ "$cur_temp" -lt 50000 ]; then
        # 只有在当前状态不是关闭(0)时,才调用关闭接口
        if [ "$status_code" != "0" ]; then
            echo "温度低于 50度,正在关闭开关..."
            turn_off
        fi
    fi; sleep 1
done

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://zwc365.com/2026/05/19/openwrt-feng-shan-wen-du-kong-zhi--tong-guo-mi-jia-usb-kai-guan-kong-zhi

Buy me a cup of coffee ☕.