Authorization: Bearer {Api-key}TUYA_API_KEY. Base URL is auto-detected from API key prefix. See references/api-conventions.md for the prefix-to-region mapping table. You can override by setting TUYA_BASE_URL.references/scripts/tuya_api.pyscripts/tuya_device_mq_client.py (real-time WebSocket subscription)Set the following environment variable before use:
bash
export TUYA_API_KEY="your-tuya-api-key"
# TUYA_BASE_URL is optional — auto-detected from API key prefix
# Override only if needed: export TUYA_BASE_URL="https://openapi.tuyaus.com"
The same TUYA_API_KEY is used for both the REST API and WebSocket message subscription. The WebSocket URI is auto-detected from the API key prefix (same 7 data centers as the REST API). See references/device-message.md for the full mapping table.
The skill will not load if the TUYA_API_KEY environment variable is missing.
Always prefer Method 1 (Command Line) — single command, no boilerplate code. It handles authentication, URL resolution, JSON serialization, and error handling automatically.
bash
python3 {baseDir}/scripts/tuya_api.py <command> [params...]
# Examples:
python3 {baseDir}/scripts/tuya_api.py homes
python3 {baseDir}/scripts/tuya_api.py devices
python3 {baseDir}/scripts/tuya_api.py devices --home 5053559
python3 {baseDir}/scripts/tuya_api.py devices --room 123456
python3 {baseDir}/scripts/tuya_api.py device_detail <device_id>
python3 {baseDir}/scripts/tuya_api.py model <device_id>
python3 {baseDir}/scripts/tuya_api.py control <device_id> '{"switch_led":true}'
python3 {baseDir}/scripts/tuya_api.py rename <device_id> "New Name"
python3 {baseDir}/scripts/tuya_api.py weather 39.90 116.40
python3 {baseDir}/scripts/tuya_api.py sms "Your message"
python3 {baseDir}/scripts/tuya_api.py voice "Your message"
python3 {baseDir}/scripts/tuya_api.py mail "Subject" "Content"
python3 {baseDir}/scripts/tuya_api.py push "Subject" "Content"
python3 {baseDir}/scripts/tuya_api.py stats_config
python3 {baseDir}/scripts/tuya_api.py stats_data <dev_id> <dp_code> <type> <start> <end>
python3 {baseDir}/scripts/tuya_api.py ipc_pic_fetch <device_id> <consent> [pic_count] [home_id]
python3 {baseDir}/scripts/tuya_api.py ipc_video_fetch <device_id> <duration> <consent> [home_id]
CLI validation rules:
devices supports only one scope flag at a time: --home <id> or --room <id>control requires properties_json to be a valid JSON object (not array/string)weather validates coordinate range: latitude [-90, 90], longitude [-180, 180]stats_data validates start/ end format yyyyMMddHH and max 24-hour windowipc_pic_fetch args: <device_id> <consent> [pic_count] [home_id] — consent 1 = decrypted URLipc_video_fetch args: <device_id> <duration> <consent> [home_id] — duration in seconds (1-60)python3 {baseDir}/scripts/tuya_api.py --help for command help and examplesUse when you need to chain multiple API calls or do complex logic in a single script:
python
import sys
sys.path.insert(0, "{baseDir}/scripts")
from tuya_api import TuyaAPI
api = TuyaAPI()
homes = api.get_homes()
devices = api.get_all_devices()
detail = api.get_device_detail("device_id_here")
result = api.issue_properties("device_id_here", {"switch_led": True, "bright_value": 500})
weather = api.get_weather(lat="39.90", lon="116.40")
# IPC cloud capture — take a snapshot and get decrypted URL
capture = api.ipc_ai_capture_pic_allocate_and_fetch("device_id_here")
Use when you need real-time device event monitoring (property changes, online/offline status):
python
import asyncio
import os
import sys
sys.path.insert(0, "{baseDir}/scripts")
from tuya_device_mq_client import TuyaDeviceMQClient
async def main():
# Uses TUYA_API_KEY for auth; WebSocket URI auto-detected from key prefix
client = TuyaDeviceMQClient(
api_key=os.environ["TUYA_API_KEY"],
device_ids=None, # None = all devices; or pass a list of device IDs
)
@client.on_property_change
async def on_prop(device_id, properties):
for prop in properties:
t = TuyaDeviceMQClient.format_timestamp(prop["time"])
print(f"[{t}] Device {device_id}: {prop['code']} = {prop['value']}")
@client.on_online_status
async def on_status(device_id, status, timestamp_ms):
t = TuyaDeviceMQClient.format_timestamp(timestamp_ms)
print(f"[{t}] Device {device_id} is now {status}")
await client.connect()
asyncio.run(main())
Important: The WebSocket client runs server-side only. It reuses the same
TUYA_API_KEY— no separate credentials needed. The WebSocket URI is auto-detected from the key prefix (same 7 data centers as the REST API). Notification throttling (minimum 30-minute cooldown) is mandatory when triggering notifications from device events. Seereferences/device-message.mdfor message format details and more examples.
ModuleCapabilitiesReferenceHome ManagementList all homes, list rooms in a homereferences/home-and-space.mdDevice QueryAll devices, devices by home/room, single device detail (including current property states)references/device-query.mdDevice ControlQuery device Thing Model, issue property commandsreferences/device-control.mdDevice ManagementRename devicereferences/device-management.mdWeather ServiceCurrent and forecast weatherreferences/weather.mdNotificationsSMS, voice call, email, App pushreferences/notifications.mdData StatisticsHourly statistics config query, statistics value queryreferences/statistics.mdIPC Cloud CaptureCloud snapshot and short video capture for IPC camerasreferences/ipc-cloud-capture.mdDevice Message SubscriptionReal-time WebSocket subscription for device property changes and online/offline eventsreferences/device-message.mdError HandlingError codes and recovery strategiesreferences/error-handling.mdAPI ConventionsRequest/response format, data center mappingreferences/api-conventions.md
When the user says things like "turn on the living room light" or "set the AC temperature to 26 degrees":
category_name or device namecategory_name first, then by device name fuzzy matchresult is null: the device does not exist or you have no permission — inform the user and stoponline is false: the device is offline — tell the user "Device XX is currently offline, please check its power and network connection" and do not proceed furtherresult is valid and online is trueproperties field contains current values of each functional property (e.g. switch state, brightness, temperature)Important: The result.model field is a JSON string that must be parsed again (e.g. json.loads(result["model"])) to obtain the property definitions
Check each property's accessMode:
ro (read-only): cannot be controlled, only queried — inform the user "this property is read-only"wr (write-only): can be controlled but current value cannot be readrw (read-write): can be both controlled and queriedTurn on/off → find a bool-type switch property (e.g. switch_led, switch)
Adjust brightness → find a value-type brightness property
Adjust temperature → find a value-type temp property
If the device does not support the requested function, inform the user and list supported functions
Relative adjustments — When the user says "a bit brighter", "lower the temperature by 2 degrees", etc.:
properties in the device detail (Step 2)min, max, step from the Thing Model typeSpec (Step 3)stepValidate value range: Before issuing, confirm the target value is within the typeSpec min/max range
api.issue_properties(device_id, {property_code: value})
properties JSON string serialization automaticallyproperties field must be a JSON string, not a JSON object. You must double-serialize: {"properties": "{\"switch_led\":true}"}propertiesObtain coordinates:
latitude / longitude fields{"Value": "30.3"} — you must extract the .Value field (e.g. home["latitude"]["Value"])references/weather.md)Determine which weather attributes to query (default: temperature, humidity, weather condition)
Call the weather query API
Translate the returned data into a human-readable description
Time inference: Convert the user's natural language to yyyyMMddHH format:
2024010100 = January 1, 2024 00:00When the user asks "Is the living room light on?" or "What's the AC set to?":
properties values, cross-reference with the Thing Model property names/descriptions, and translate to natural language (e.g. "switch_led": true → "the light is currently on")When the user says "Turn off all lights" or "Set all ACs to 26 degrees":
category_name or device name keywordonline status (skip offline devices and note them), then execute Workflow 1 Steps 3-6When the user asks to "take a photo with the camera" or "record a short video from the camera":
Locate the IPC device — same as Workflow 1 Step 1, filter by camera category
Determine capture type:
PIC (optional pic_count, 1-5)VIDEO (optional video_duration_seconds, 1-60, default 10)Execute capture — Use the all-in-one helper methods:
api.ipc_ai_capture_pic_allocate_and_fetch(device_id, pic_count=1)api.ipc_ai_capture_video_allocate_and_fetch(device_id, video_duration_seconds=5)Return the result — Extract the URL from the resolve result:
resolve["decrypt_image_url"]resolve["decrypt_video_url"] (cover image may be null if still uploading)status is still NOT_READY after all retries, inform the user that the device may be slow to upload and suggest trying again laterWhen the user asks "What's in front of my camera?", "Is there anyone at the door?", or "Describe what the camera sees":
Capture a snapshot — Follow Workflow 8 Steps 1-3 to take a PIC capture
Get the image URL — Extract resolve["decrypt_image_url"] from the capture result. If the resolve failed or returned NOT_READY, inform the user and stop
Download the image — Fetch the image content from the decrypted URL
Send to AI vision model — Pass the image to the AI large model for visual understanding. Describe the image content in natural language based on the user's question:
Return the description — Respond to the user with the visual analysis result in conversational language
When the user asks to "monitor device changes in real time", "watch for property updates", or "notify me when a device goes offline":
Determine scope — Ask which devices to monitor (all or specific device IDs). If specific, locate devices using Workflow 1 Step 1
Determine event types — Property changes ( on_property_change), online/offline status ( on_online_status), or both
Write the subscription script — Using TuyaDeviceMQClient from scripts/tuya_device_mq_client.py:
api_key=os.environ["TUYA_API_KEY"] (WebSocket URI auto-detected from key prefix)await client.connect() to start listeningApply throttling — If the subscription triggers notifications or device control actions, implement a cooldown mechanism (minimum 30-minute interval for notifications)
Cross-reference with REST API — Property codes from WebSocket events correspond to Thing Model codes. Use api.get_device_model(device_id) to look up property names and value ranges when needed
When the user asks to "turn on the hallway light when the door opens" or "send me a notification when the AC turns off":
Identify trigger and action — Parse the trigger device, trigger condition (property code + value), and the action to execute
Locate devices — Use Workflow 1 Step 1 to find both the trigger device and the action device
Write the automation script — Combine TuyaDeviceMQClient for event listening with TuyaAPI for device control:
api.issue_properties() to control the action deviceVerify — Confirm the trigger condition and action mapping with the user before running
yyyyMMddHH, and the time range cannot exceed 24 hours per requestreferences/api-conventions.md for detailsTUYA_API_KEY value in output2 for usage/validation errors, and 1 for runtime/API/network errorsOnly basic data type properties are currently supported for device control:
TypeDescriptionExampleboolBoolean on/offTurn light on/off, turn AC on/off, turn plug on/offenumEnumeration selectionSwitch AC mode (auto/cold/hot), set fan speed (low/mid/high)value (Integer)Numeric valueAdjust brightness (0-1000), set temperature (16-30)stringString valueSet device display text
The following operations involve sensitive actions or complex data types and are NOT supported:
raw, bitmap, struct, or array typeSpec are not supported for issuing commandsIf the user requests any of these unsupported operations, clearly inform them that the operation is not available through this skill and suggest using the Tuya App directly.
This skill sends data to the Tuya Open Platform:
Data TypeSent ToPurposeRequiredApi-keyUser-configured base_urlAPI authenticationRequiredDevice IDUser-configured base_urlDevice query and controlRequiredControl commandsUser-configured base_urlDevice property issuanceRequiredApi-keyAuto-detected WebSocket URIReal-time event subscription authenticationRequired for message subscription