Tuya Cloud Controller
Read sensor data and control Tuya IoT devices via scripts/tuya_controller.py. Supports both 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 list 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_list_devices if the device is not listed in the config.
Setup
Add credentials to .env:
CODEBLOCK0
Regional endpoints: EU tuyaeu.com · US tuyaus.com · CN tuyacn.com · IN INLINECODE12
Enable IoT Core service in your Tuya IoT Platform project, and grant devices controllable permission (read-only by default).
Tools
tuya_list_devices
List all Tuya devices linked to the cloud project.
CODEBLOCK1
tuya_read_sensor
Read all sensor data from a Tuya device (temperature, humidity, battery, motion, door state, switch state).
CODEBLOCK2
INLINECODE15 interprets raw API keys:
| Sensor | Raw keys | Notes |
|---|
| Temperature | INLINECODE16 , temp_current, INLINECODE18 | 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 = detected |
| Door |
doorcontact_state | Boolean → Open/Closed |
| State |
state | Boolean → On/Off |
| Soil moisture |
soil_moisture,
humidity,
va_humidity | Percentage as-is |
tuya_control_device
Send 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/ endpoint, which supports Zigbee sub-devices.
CODEBLOCK3
⚠️ 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 | INLINECODE37 | INLINECODE38 |
| Right |
switch_2 |
countdown_2 |
Control each channel independently:
CODEBLOCK4
To register a dual-channel valve in config.py, add both channels as separate entries or use a list:
CODEBLOCK5
Local LAN Tools
Control devices directly over the local network without cloud API calls. No credentials needed for scanning; local_key and ip required for read/control.
scan_local
Scan the local network for Tuya devices via UDP broadcast.
CODEBLOCK6
read_local
Read device status directly over LAN (no cloud round-trip).
CODEBLOCK7
control_local
Send commands to a device directly over LAN. Commands can use integer DP index (dp) or string DP name (code).
CODEBLOCK8
Use integer dp for maximum compatibility. String code requires the device to support it.
API Endpoints (internal)
- - Device list: INLINECODE51
- Device info: INLINECODE52
- Device status: INLINECODE53
- Send commands: INLINECODE54
Dependencies
CODEBLOCK9
Troubleshooting
| Error | Fix |
|---|
| "Data center is not enabled" | Enable IoT Core in Tuya IoT Platform → Service API |
| "Permission denied" |
Subscribe to IoT Core and enable Device Status Notification |
| Device offline |
online: false; soil moisture returns
null |
| Wrong endpoint | Match
TUYA_API_ENDPOINT to your account region |
| Local scan finds nothing | Check firewall; UDP broadcast may be blocked on some networks |
| Local control fails for Zigbee devices | Zigbee sub-devices must be controlled via their gateway (use gateway ID +
cid) |
Tuya Cloud 控制器
通过 scripts/tuya_controller.py 读取传感器数据并控制涂鸦物联网设备。同时支持涂鸦云 API 和本地局域网直接控制。
设备注册表
已知可控设备在 scripts/config.py 中定义为 CONTROLLABLE_DEVICES(可选)。
如果存在该列表,请优先查阅,以将设备名称解析为对应的 device_id。
对于阀门设备,valve 键提供用作命令 code 的 DP 码(例如 switch_1)。
仅当设备未在配置中列出时,才调用 tuyalistdevices。
设置
将凭证添加到 .env:
bash
TUYAACCESSID=youraccessid
TUYAACCESSSECRET=youraccesssecret
TUYAAPIENDPOINT=https://openapi.tuyaeu.com # 默认:tuyaus.com(美国)
区域端点:欧盟 tuyaeu.com · 美国 tuyaus.com · 中国 tuyacn.com · 印度 tuyain.com
在涂鸦物联网平台项目中启用 IoT Core 服务,并授予设备可控权限(默认为只读)。
工具
tuyalistdevices
列出连接到云项目的所有涂鸦设备。
bash
python scripts/tuyacontroller.py listdevices
python scripts/tuyacontroller.py listdevices --outputformat json --outputpath devices.json
tuyareadsensor
读取涂鸦设备的所有传感器数据(温度、湿度、电量、移动检测、门状态、开关状态)。
bash
python scripts/tuyacontroller.py readsensor
python scripts/tuyacontroller.py readsensor id> --outputformat text
parsesensordata() 解释原始 API 键:
| 传感器 | 原始键 | 说明 |
|---|
| 温度 | vatemperature, tempcurrent, tempset | 除以 10(例如 245 → 24.5°C) |
| 湿度 |
vahumidity, humidity_value | 百分比,原样返回 |
| 电量 | battery_percentage, battery | 良好 >80% / 中等 >20% / 低 ≤20% |
| 移动检测 | pir | pir 值 = 检测到移动 |
| 门状态 | doorcontact_state | 布尔值 → 开/关 |
| 状态 | state | 布尔值 → 开/关 |
| 土壤湿度 | soil
moisture, humidity, vahumidity | 百分比,原样返回 |
tuyacontroldevice
向涂鸦设备发送命令(开关、阀门、倒计时定时器)。传递一个 {code, value} 对的 JSON 数组。使用 IoT Core /v1.0/iot-03/ 端点,支持 Zigbee 子设备。
bash
打开或关闭开关/阀门
python scripts/tuya
controller.py controldevice
id> [{code:switch1,value:true}]
python scripts/tuyacontroller.py controldevice id> [{code:switch2,value:false}]
打开阀门并设置固定时长 — 在一次调用中发送开关 ON + 倒计时
countdown1 / countdown2 的值以分钟为单位 — 不要乘以 60
python scripts/tuyacontroller.py controldevice id> [{code:switch1,value:true},{code:countdown_1,value:10}]
⚠️ countdown1 / countdown2 以分钟为单位。10 = 10 分钟,60 = 1 小时。
双通道阀门设备
部分阀门设备提供两个独立通道(例如左右出水口)。每个通道有自己的开关和倒计时 DP:
| 通道 | 开关 DP | 倒计时 DP |
|---|
| 左 | switch1 | countdown1 |
| 右 |
switch2 | countdown2 |
独立控制每个通道:
bash
打开左阀门 5 分钟
python scripts/tuyacontroller.py controldevice id> [{code:switch1,value:true},{code:countdown_1,value:5}]
打开右阀门 10 分钟
python scripts/tuyacontroller.py controldevice id> [{code:switch2,value:true},{code:countdown_2,value:10}]
立即关闭右阀门
python scripts/tuyacontroller.py controldevice id> [{code:switch2,value:false}]
要在 config.py 中注册双通道阀门,可以将两个通道作为单独条目添加,或使用列表:
python
选项 A:两个条目(每个通道一个)
{name: 温室阀门左, deviceid: , valve: switch1, countdown: countdown_1},
{name: 温室阀门右, deviceid: , valve: switch2, countdown: countdown_2},
本地局域网工具
无需云 API 调用,直接在本地网络上控制设备。扫描不需要凭证;读取/控制需要 local_key 和 ip。
scan_local
通过 UDP 广播扫描本地网络中的涂鸦设备。
bash
python scripts/tuyacontroller.py scanlocal
python scripts/tuyacontroller.py scanlocal --timeout 10
python scripts/tuyacontroller.py scanlocal --enrich --outputformat json # 添加云名称/localkeys
read_local
直接通过局域网读取设备状态(无需云往返)。
bash
python scripts/tuyacontroller.py readlocal id> key>
python scripts/tuyacontroller.py readlocal id> key> --version 3.4
control_local
直接通过局域网向设备发送命令。命令可以使用整数 DP 索引(dp)或字符串 DP 名称(code)。
bash
python scripts/tuyacontroller.py controllocal id> key> [{dp:1,value:true}]
python scripts/tuyacontroller.py controllocal id> key> [{code:switch_1,value:true}]
使用整数 dp 可获得最大兼容性。字符串 code 需要设备支持。
API 端点(内部)
- - 设备列表:GET /v1.0/iot-01/associated-users/devices
- 设备信息:GET /v1.0/devices/{deviceid}
- 设备状态:GET /v1.0/iot-03/devices/{deviceid}/status
- 发送命令:POST /v1.0/iot-03/devices/{device_id}/commands
依赖项
bash
pip install tinytuya python-dotenv
故障排除
| 错误 | 解决方法 |
|---|
| Data center is not enabled | 在涂鸦物联网平台 → 服务 API 中启用 IoT Core |
| Permission denied |
订阅 IoT Core 并启用设备状态通知 |
| 设备离线 | online: false;土壤湿度返回 null |
| 端点错误 | 将 TUYAAPIENDPOINT 与您的账户区域匹配 |
| 本地扫描未发现任何设备 | 检查防火墙;某些网络可能阻止 UDP 广播 |
| 本地控制 Zigbee 设备失败 | Zigbee 子设备必须通过其网关控制(使用网关 ID + cid) |