Tuya Cloud — Tuya 云
v1.0通过Tuya Cloud API或本地LAN读取传感器数据并控制Tuya IoT设备。当用户想要列出设备、读取温度、湿度、土壤湿度、电池或其他传感器数据,或向开关和阀门发送命令时使用。需要在.env中设置TUYA_ACCESS_ID、TUYA_ACCESS_SECRET,并可选设置TUYA_API_ENDPOINT。
运行时依赖
安装命令
点击复制技能文档
Tuya Cloud 控制器
Read sensor data and control Tuya IoT devices via scripts/tuya_控制器.py. Supports 机器人h Tuya Cloud API and direct local LAN control.
Device registry
Known controllable devices are defined in scripts/config.py as CONTROLLABLE_DEVICES (OPTIONAL). Always consult this 列出 first if exist to resolve a device name to its device_id. For valve devices the valve key gives the DP code ( e.g. switch_1) to use as the command code. Only call tuya_列出_devices if the device is not 列出ed in the config.
设置up
添加 凭证s to .env:
TUYA_访问_ID=your_访问_id TUYA_访问_SECRET=your_访问_secret TUYA_API_端点=https://openAPI.tuyaeu.com # default: tuyaus.com (US)
Regional 端点s: EU tuyaeu.com · US tuyaus.com · CN tuyacn.com · IN tuyAIn.com
Enable IoT Core 服务 in your Tuya IoT 平台 project, and grant devices controllable 权限 (read-only by default).
工具s tuya_列出_devices
列出 all Tuya devices linked to the cloud project.
python scripts/tuya_控制器.py 列出_devices python scripts/tuya_控制器.py 列出_devices --输出_格式化 json --输出_path devices.json
tuya_read_24h_avg
Return the 24-hour average soil moisture (humidity) for a sensor, computed from its Tuya DP 记录 历史.
python scripts/tuya_控制器.py read_24h_avg
输出 is a JSON object:
{ "avg_humidity": 34.2, // mean of all va_humidity 记录 entries over 24 h; null if no data "count": 47, // number of 记录 entries found "min_humidity": 28.0, "max_humidity": 41.0 }
If count is 0 all fields are null — this h应用ens when the device has no 记录 历史 yet or the Tuya 记录s API is rate-limited. The garden-irrigation 技能 falls back to the current reading in that case.
tuya_read_sensor
Read all sensor data from a Tuya device (temperature, humidity, battery, motion, door 状态, switch 状态).
python scripts/tuya_控制器.py read_sensor python scripts/tuya_控制器.py read_sensor --输出_格式化 text
解析_sensor_data() interprets raw API keys:
Sensor Raw keys Notes Temperature va_temperature, temp_current, temp_设置 Divided by 10 (e.g. 245 → 24.5°C) Humidity va_humidity, humidity_value Percentage as-is Battery battery_percentage, battery Good >80% / Medium >20% / Low ≤20% Motion pir "pir" value = 检测ed Door doorcontact_状态 Boolean → Open/Closed 状态 状态 Boolean → On/Off Soil moisture soil_moisture, humidity, va_humidity Percentage as-is tuya_control_device
发送 commands to a Tuya device (switch, valve, countdown timer). Pass a JSON array of {"code", "value"} pAIrs. Uses the IoT Core /v1.0/iot-03/ 端点, which supports Zigbee sub-devices.
# Turn switch/valve on or off python scripts/tuya_控制器.py control_device '[{"code":"switch_1","value":true}]' python scripts/tuya_控制器.py control_device '[{"code":"switch_2","value":false}]'
# Open valve for a fixed duration — 发送 switch ON + countdown in ONE call # countdown_1 / countdown_2 values are in MINUTES — do NOT multiply by 60 python scripts/tuya_控制器.py control_device '[{"code":"switch_1","value":true},{"code":"countdown_1","value":10}]'
⚠️ countdown_1 / countdown_2 are in minutes. 10 = 10 min, 60 = 1 hour.
Dual-channel valve devices
Some valve devices expose two independent channels (e.g. left and right outlet). Each channel has its own switch and countdown DP:
Channel Switch DP Countdown DP Left switch_1 countdown_1 Right switch_2 countdown_2
Control each channel independently:
# Open left valve for 5 minutes python scripts/tuya_控制器.py control_device '[{"code":"switch_1","value":true},{"code":"countdown_1","value":5}]'
# Open right valve for 10 minutes python scripts/tuya_控制器.py control_device '[{"code":"switch_2","value":true},{"code":"countdown_2","value":10}]'
# Close right valve immediately python scripts/tuya_控制器.py control_device '[{"code":"switch_2","value":false}]'
To register a dual-channel valve in config.py, 添加 机器人h channels as separate entries or use a 列出:
# Option A: two entries (one per channel) {'name': 'Greenhouse valve left', 'device_id': '', 'valve': 'switch_1', 'countdown': 'countdown_1'}, {'name': 'Greenhouse valve right', 'device_id': '', 'valve': 'switch_2', 'countdown': 'countdown_2'},
Local LAN 工具s
Control devices directly over the local network without cloud API calls. No 凭证s needed for 扫描ning; local_key and ip required for read/control.
扫描_local
扫描 the local network for Tuya devices via UDP broadcast.
python scripts/tuya_控制器.py 扫描_local python scripts/tuya_控制器.py 扫描_local --timeout 10 python scripts/tuya_控制器.py 扫描_local --enrich --输出_格式化 json # 添加 cloud names/local_keys (requires cloud 凭证s)
read_local
Read device 状态 directly over LAN (no cloud round-trip).
python scripts/tuya_控制器.py read_local python scr