On this page

saveLightScene(data: SaveLightSceneParams)

Requires:
@tuya-miniapp/cloud-api≥1.2.0

Description

Create, edit, and save a lighting scene. This API saves the rule configuration of a lighting scene, including scene name, icon, execution actions, and other information.

Parameters

ParameterTypeRequiredDescription
YesRequest parameters: ownerId (Home ID), sceneExpr (scene rule JSON data that contains the complete scene configuration; see the documentation "sceneExpr field description" for SaveExprData field details)

Return Value

Type: Promise<SaveLightSceneResult>
Scene info after saving (includes id, code, name, parentRegionId, ownerId, icon, enabled, status, sceneType, etc.; see the Return Parameters table in the documentation for details)

Referenced Types

interfaceSaveLightSceneResult
PropertyTypeDescription
idnumberPrimary key ID
codestringScene code
namestringName
parentRegionIdstringParent area ID
ownerIdstringHome ID
uidstringUser ID
actualRuleIdstringActual rule ID
iconstringIcon
clickIconstringConstruct the click icon based on the icon field
enablednumberWhether enabled (1: Enabled, 0: Disabled)
statusnumberStatus (0: Deleted, 1: Normal)
sortnumberSort order
sceneType1 | 2 | 3Scene type (1: Basic scene on, 2: Basic scene off, 3: Custom scene)
subSceneTypenumberSubtype; 0 is the default value and has no business meaning
Execution action
Condition; currently used only for distributed Bluetooth lighting scenes
modifyUidstringID of the user who updates the current record
brightPercentnumberBrightness percentage
brightVisiblebooleanWhether the brightness percentage is visible
displayColorstringBackground color
backgroundstringBackground image
scheduleBoundbooleanWhether referenced by a schedule
distributedAbnormalCode0 | 1 | 2 | 100Exception status of a distributed lighting scene (0: Normal, 1: Temporarily saved, 2: Not synchronized due to room device changes, 100: Other exception)
distributeShortAddressnumberShort address of the distributed rule
matchTypenumberCondition match type; currently used only for distributed Bluetooth lighting scenes
gmtCreatenumberCreation time
gmtModifiednumberUpdate time

Valid Values

SaveLightSceneResult.sceneType
ValueDescriptionSince
1--
2--
3--
SaveLightSceneResult.distributedAbnormalCode
ValueDescriptionSince
0--
1--
2--
100--

Examples

Request example

import { saveLightScene } from '@tuya-miniapp/cloud-api';

const params = {
  ownerId: '194137',
  sceneExpr: JSON.stringify({
    actions: [
      {
        actionExecutor: 'lightDevice',
        entityId: 'vdevo176127325226099',
        entityName: 'Debug - Step Dimming - vdevo',
        executorProperty: {
          switch_led: true,
          work_mode: 'scene',
          smb_scene: '0132010100000000000003e80000320101000000000000000a0000',
        },
        extraProperty: {
          parentRegionId: '57939115',
          sceneName: '500 ms fade (warm light)',
          sceneId: '22304912',
          sceneType: 1,
          selectCellBackground: 'https://images.tuyacn.com/light/library/icon/img_scene.png',
        },
      },
    ],
    parentRegionId: 57939115,
    name: '500 ms fade (warm light)',
    displayColor: 'BA7B69',
    sceneType: 3,
    matchType: 1,
    icon: 'https://images.tuyacn.com/smart/rule/light/dianji_default.png',
  }),
};

saveLightScene(params)
  .then(result => {
    console.log('Scene saved successfully:', result);
  })
  .catch(error => {
    console.error('Failed to save scene:', error);
  });

Response example

{
  "id": 123456,
  "code": "light_scene_001",
  "name": "500 ms fade (warm light)",
  "parentRegionId": "57939115",
  "ownerId": "194137",
  "uid": "ay1234567890",
  "icon": "https://images.tuyacn.com/smart/rule/light/dianji_default.png",
  "enabled": 1,
  "status": 1,
  "sort": 1,
  "sceneType": 3,
  "displayColor": "BA7B69",
  "matchType": 1,
  "gmtCreate": 1766558627585,
  "gmtModified": 1766558627585
}

Additional notes

  1. The sceneExpr parameter is a complex JSON string. Build a JavaScript object first, then convert it with JSON.stringify().
  2. Some fields in the response are optional; the actual payload may vary depending on scene type and configuration.